要求如下:
在存储过程中如何创建一个临时表,而且表的列数由存储过程的参数动态确定。比如参数为3,则表中需创建FCol1,FCol2,FCol3三个字段,若参数为5,则需要创建FCol1,FCol2,FCol3,FCol4,FCol5五个字段
这样的SQL语句这么写?在线等待ing
------解决方案--------------------
- SQL code
declare @i int,@j int,@s varchar(1000)set @i=5set @j=1set @s='create table t ('while @j<[email protected]beginset @[email protected]+'fil'+cast(@j as varchar)+' '+ ' varchar(10) ,'set @[email protected]+1endset @s=stuff(@s,len(@s),1,'')set @[email protected]+')'--print @sexec (@s)select * from t/*fil1 fil2 fil3 fil4 fil5 ---------- ---------- ---------- ---------- ---------- (所影响的行数为 0 行)*/
------解决方案--------------------
可以不加的
例:
EXEC sp_executesql @selectStr,[email protected] varchar(50) output',@cdepCode output