动态执行Sql语句与临时表的问题(对象名无效)

exec('selectIDENTITY(int,1,1)  as ID,fnum,into  #yuecu  from  store_store where  '+@Parameter)  
select  XX,XX1,XX2  from  #yuecu 
.  
这样创建一个临时表以后我再想从临时表中取数据提示对象名#yuecu 无效,晕,如果我不是使用exec()执行sql语句的话就没有任何问题,WHY?  

临时表有作用域的,上面的临时表只在EXEC內部有效。

解决办法:

改用全局临时表
exec('selectIDENTITY(int,1,1)  as ID,fnum,into  ##yuecu  from  store_store where  '+@Parameter)  

select * from  ##yuecu  

原文地址:https://www.cnblogs.com/xiang/p/532580.html