我正在尝试创建一个允许将变量用作表名的过程。
示例:
Select Temp1, Temp2
From @var
@var具有正确的表名,但每次迭代都会通过while循环进行更改。
-- Above sets up all Variables
WHILE @Count >= @I
BEGIN
SET @ObjectID = (SELECT ObjectID FROM @TmpTable WHERE ID = @I)
SET @TableName = (SELECT TableName FROM @TmpTable WHERE ID = @I)
IF @I = 8
BEGIN
SELECT *
INTO ##TMPTable
FROM @TableName
-- Additional Processing
END
END
答案 0 :(得分:0)
DECLARE
@SQLString varchar(max),
@TempTableName varchar(100)
SET @TempTableName = '##TMPTable' + cast( @@spid AS varchar(5))
WHILE @Count >= @I
BEGIN
SET @ObjectID = (SELECT ObjectID FROM @TmpTable WHERE ID = @I)
SET @TableName = (SELECT TableName FROM @TmpTable WHERE ID = @I)
IF @I = 8
BEGIN
set @SQLString = '
SELECT *
INTO ' + @TempTableName + '
FROM ' + @TableName
EXECUTE ( @SQLString )
EXECUTE ( 'SELECT * from ' + @TempTableName)
-- Additional Processing
END
END