1
当我写这样的动态SQL?SQL Server动态SQL无法获得值
set @dynamicSQL='select '[email protected]+'= name from '[email protected]+'.sys.sysfiles where groupid=0'
它不能正常工作,但是当我这样写:
select @logName=name from sys.sysfiles where groupid=0;
它可以正常工作,而像这样:
set @dynamicSQL='select name from '[email protected]+'.sys.sysfiles where groupid=0'
它可以工作normal.But为什么?我会批准如果有人告诉我。
当语句执行时,你传递给@logName –
的值是什么,它在它自己的shell中执行,它与脚本运行的shell是分开的;这些shell之间没有任何通信是可能的,除非它涉及存储在表中的数据(即,一个shell将数据写入表中,然后在另一个shell中检索数据):您的第一个查询尝试从子shell中检索信息,但这是不可能的,因为该子shell的输出无法恢复。这有帮助吗? –
对于从动态查询中简单地检索变量,此线程可能会有所帮助:http://stackoverflow.com/questions/3840730/getting-result-of-dynamic-sql-into-a-variable – 2013-10-08 03:49:24