摘要:
下文讲述一种在存储过程中,执行同服务器中不同数据库的脚本的方法分享,如下所示:
USE 关键字功能:
USE 关键字用于切换当前脚本运行的数据库,切换上下文。
那么如何在存储过程中使用USE 关键字,实现存储过程中 跨数据库执行脚本呢?
例:
存储过程中使用USE 关键字
存储过程中,使用USE关键字的方法
exec(‘use databasename; update tableName set … where keyId …’)
———————————————————
–当使用Exec(use )语句后,
–use只在当前exec语句结束前有效,所以必须将相关语句放入exec中一起运行。
———————————————————
create proc pr_test as begin exec('use databasename; update tableName set ... where keyId ...') end go exec pr_test go