SqlServer动态生成临时表

if object_id('[tmp_ProcessInfo]') is not null drop table [tmp_ProcessInfo]
CREATE TABLE tmp_ProcessInfo (FInterID int,FName varchar(255))
--insert tmp_ProcessInfo
--select 0,'a' union all
--select 1,'b' 
--select * from tmp_ProcessInfo

declare @queryStr varchar(255),@sql varchar(1000)
set @queryStr='and FType=0  and FProcessInfoID in (0,53,122,150)'

set @queryStr=replace(@queryStr,'and FType=0','')
set	@queryStr=replace(@queryStr,'FProcessInfoID','FInterID')
	set @sql='insert tmp_ProcessInfo select FInterID,FName from t_cy_ProcessInfo where 1=1 ' + @queryStr
	set @sql=@sql + ' union all select 0,'''''
print @sql

insert tmp_ProcessInfo select FInterID,FName from t_cy_ProcessInfo where 1=1   and FInterID in (0,53,122,150) union all select 0,''

select * from tmp_ProcessInfo
原文地址:https://www.cnblogs.com/nikyxxx/p/1694733.html