使用临时表的另外一种方式

假如由于某种原因,不能直接使用select  id, name, classid into #tmp1 from ...... 这样的语句,而必须要先把该语句放在一个字符串中,然后再调用exec(..)来执行,如果这时刚好也要把查询结果放在一个临时表中,那么相应的sql语句就要象下面这样:

1..
2declare @s varchar(2000)
3create table #tmp1(id int, name varchar(20),classid int)
4set @s='insert into #tmp1 select id, name, classid from .'
5exec(@s)


 注意临时表一定要先定义;而且一定要使用insert into ....的形式,而不是上面的 select .... into 的形式

原文地址:https://www.cnblogs.com/strinkbug/p/581004.html