帮我写一个SQL SERVER中通用的把查询的数据转换为insert into语句的存储过程
时间: 2024-02-03 12:12:55 浏览: 83
生成insert语句的存储过程
下面是一个SQL Server中通用的将查询结果转换为INSERT INTO语句的存储过程:
```
CREATE PROCEDURE dbo.GenerateInsertStatements
@table_name nvarchar(128),
@where_clause nvarchar(max) = '',
@include_column_headers bit = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @column_list nvarchar(max);
DECLARE @select_list nvarchar(max);
DECLARE @insert_list nvarchar(max);
DECLARE @sql nvarchar(max);
-- Get column list
SELECT @column_list = COALESCE(@column_list + ',', '') + QUOTENAME(name)
FROM sys.columns
WHERE object_id = OBJECT_ID(@table_name);
-- Build SELECT statement
SET @select_list = 'SELECT ' + @column_list + ' FROM ' + @table_name;
IF @where_clause <> ''
SET @select_list = @select_list + ' WHERE ' + @where_clause;
-- Build INSERT INTO statement
SET @insert_list = 'INSERT INTO ' + @table_name + '(' + @column_list + ') VALUES ';
SET @insert_list = @insert_list + '(' + STUFF((SELECT ', ' + QUOTENAME(name)
FROM sys.columns
WHERE object_id = OBJECT_ID(@table_name)
FOR XML PATH('')), 1, 2, '') + ')';
-- Build final SQL statement
SET @sql = '';
IF @include_column_headers = 1
SET @sql = @sql + 'PRINT ''' + @insert_list + '''';
SET @sql = @sql + 'SELECT ' + STUFF((SELECT ', ' + 'CAST(' + QUOTENAME(name) + ' AS NVARCHAR(MAX))'
FROM sys.columns
WHERE object_id = OBJECT_ID(@table_name)
FOR XML PATH('')), 1, 2, '') + ' FROM (' + @select_list + ') AS tbl';
-- Execute final SQL statement
EXEC sp_executesql @sql;
END;
```
使用方法:
1. 执行以下语句创建存储过程:
```
EXEC dbo.sp_executesql N'
CREATE PROCEDURE dbo.GenerateInsertStatements
@table_name nvarchar(128),
@where_clause nvarchar(max) = '',
@include_column_headers bit = 1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @column_list nvarchar(max);
DECLARE @select_list nvarchar(max);
DECLARE @insert_list nvarchar(max);
DECLARE @sql nvarchar(max);
-- Get column list
SELECT @column_list = COALESCE(@column_list + '', '') + QUOTENAME(name)
FROM sys.columns
WHERE object_id = OBJECT_ID(@table_name);
-- Build SELECT statement
SET @select_list = ''SELECT '' + @column_list + '' FROM '' + @table_name;
IF @where_clause <> ''''
SET @select_list = @select_list + '' WHERE '' + @where_clause;
-- Build INSERT INTO statement
SET @insert_list = ''INSERT INTO '' + @table_name + ''('' + @column_list + '') VALUES '';
SET @insert_list = @insert_list + ''('' + STUFF((SELECT '', '' + QUOTENAME(name)
FROM sys.columns
WHERE object_id = OBJECT_ID(@table_name)
FOR XML PATH('''')), 1, 2, '''') + '')'';
-- Build final SQL statement
SET @sql = '''';
IF @include_column_headers = 1
SET @sql = @sql + ''PRINT '''''' + @insert_list + '''''''';
SET @sql = @sql + ''SELECT '' + STUFF((SELECT '', '' + ''CAST('' + QUOTENAME(name) + '' AS NVARCHAR(MAX))''
FROM sys.columns
WHERE object_id = OBJECT_ID(@table_name)
FOR XML PATH('''')), 1, 2, '''') + '' FROM ('' + @select_list + '') AS tbl'';
-- Execute final SQL statement
EXEC sp_executesql @sql;
END;
'
```
2. 然后,使用以下语法调用存储过程:
```
EXEC dbo.GenerateInsertStatements 'table_name', 'where_clause', include_column_headers
```
其中,'table_name'是要生成INSERT INTO语句的表名,'where_clause'是可选的WHERE子句,'include_column_headers'是一个布尔值,指定是否在结果中包括列标题。
阅读全文