不知道为什么
用 exec('use northwind go ') 也不报错,但就是没有效果。
可以这样
sp_executesql N'select * from '+@db+'..products where productID > @ID',
N'@ID int',
@ID=10
用 exec('use northwind go ') 也不报错,但就是没有效果。
可以这样
sp_executesql N'select * from '+@db+'..products where productID > @ID',
N'@ID int',
@ID=10
go
select db_name()
exec('use msdb')
select db_name()
use master
go
select db_name()是可以得到正确结果的。
只是你的后面会告诉你没有use madb这个存储过程罢了。应该可以动态的选择数据库才对。