动态SQL-通过INSERT INTO x SELECT语句插入变量

时间:2018-08-17 08:24:27

标签: sql sql-server sql-server-2012 dynamic-sql

我想问一下如何使用INSERT INTO x SELECT语句通过动态SQL将变量插入表中。

我有下表:

 |-------------------|-----------------|--------------|-----------------|
 |     TableName     |     ColName     |     Value    |  SQL_Statement  |
 |-------------------|-----------------|--------------|-----------------|

通过此查询,我获得了列的内容:

INSERT INTO #ReturnTable(Value) SELECT TreeHolder FROM prm.Schm_Root WHERE ParentTreeHolderId = 'DD040D31-4591-4658-A02E-A6ED00AB64F2';

但是我需要填满整个桌子。请考虑其他值是变量,而不是SQL查询。

SELECT @TableSchema = TableSchema FROM #TableNames WHERE Id = @Counter;
SELECT @TableName = TableName FROM #TableNames WHERE Id = @Counter;
SELECT @ColName = ColName FROM #TableNames WHERE Id = @Counter;
SET @SQL_Statement = 'SELECT ' + @ColName + ' FROM ' + @TableSchema + '.' + @TableName + ' WHERE ' + @ColName + ' = ' + '''''' + CAST(@GuidArgument AS NVARCHAR(50)) + '''''' + ';';

现在我有这个查询,可以填满表格:

SET @SQL_String = N'INSERT INTO #ReturnTable SELECT
''' + @TableName + ''',
''' + @ColName + ''',
''' + @SQL_Statement + ''',
'' + Value + '',
   (SELECT ' +
       @ColName + '
    FROM ' +
       @TableSchema + '.' + @TableName + '
    WHERE ' +
       @ColName + ' = ''' + CAST(@GuidArgument AS NVARCHAR(50)) + '
'')';

EXECUTE sp_executesql @SQL_String
PRINT @SQL_String;

我需要做的是将该查询从INSERT INTO ? VALUE重写为INSERT INTO ? SELECT格式。

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您想插入SQL执行语法字符串,它会生成ReturnTable表。

我会让子查询SQL执行语法保存在变量中。因为这样会更加清楚您需要做什么。

声明一个新变量@SQL_excuteStatement,以保存执行语法。

@SQL_Statement携带原始SQL字符串。

set @SQL_Statement =  'SELECT ' + @ColName + 
     ' FROM ' + @TableSchema + '.' + @TableName + 
     ' WHERE ' + @ColName + ' = '+'''''' +  CAST(@GuidArgument AS NVARCHAR(50)) + '''''';

并使用select ... from表代替select中的子查询

有适合​​您的样品。

DECLARE @SQL_String NVARCHAR(MAX)
DECLARE @TableSchema NVARCHAR(MAX)
DECLARE @TableName NVARCHAR(MAX)
DECLARE @ColName NVARCHAR(MAX)
DECLARE @Counter int = 1
DECLARE @SQL_Statement NVARCHAR(MAX)
DECLARE @GuidArgument INT = 1

CREATE TABLE TableNames(
     ID INT,
     TableSchema NVARCHAR(100),
     TableName NVARCHAR(100),
     ColName  NVARCHAR(100)
);


CREATE TABLE ReturnTable(
     TableName NVARCHAR(100),
     ColName  NVARCHAR(100),
     SQL_Statement NVARCHAR(max),
     value nvarchar(max)
);


INSERT INTO TableNames VALUES (1,'dbo','T','val');

CREATE TABLE T(val INT);
INSERT INTO T VALUES (1)

SELECT @TableSchema = TableSchema FROM TableNames WHERE Id = @Counter;
SELECT @TableName = TableName FROM TableNames WHERE Id = @Counter;
SELECT @ColName = ColName FROM TableNames WHERE Id = @Counter;


set @SQL_Statement =  'SELECT ' + @ColName + 
 ' FROM ' + @TableSchema + '.' + @TableName + 
 ' WHERE ' + @ColName + ' = '+ '''''' +  CAST(@GuidArgument AS NVARCHAR(50)) + '''''';


SET @SQL_String =  N'INSERT INTO ReturnTable (TableName,ColName,SQL_Statement,value) 
SELECT '''+ @TableName + ''','''+ @ColName + ''','''+ @SQL_Statement + '''' + ',' + QUOTENAME(@ColName) +
' FROM ' + QUOTENAME(@TableSchema) + '.' + QUOTENAME(@TableName) + '' +
' WHERE ' + @ColName + ' = ''' +  CAST(@GuidArgument AS NVARCHAR(50)) + '''';

EXECUTE sp_executesql @SQL_String

sqlfiddle

注意

我建议您在insert into之后使用空白列

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;