1 DECLARE @table SYSNAME
2 SELECT @table = ‘替换为相应的表名‘
3 DECLARE @insert_sql VARCHAR(MAX)
4 SELECT @insert_sql =
5 CASE
6 WHEN @insert_sql IS NULL
7 THEN ‘‘‘INSERT INTO ‘ + @table + ‘ SELECT ‘‘+‘
8 ELSE @insert_sql + ‘+‘‘,‘‘+‘
9 END
10 + ‘CASE WHEN ‘ + name + ‘ IS NULL THEN ‘‘NULL‘‘ ELSE ‘ +
11 CASE
12 WHEN RIGHT(TYPE_NAME(system_type_id),4) IN (‘CHAR‘,‘TEXT‘)
13 THEN ‘‘‘‘‘‘‘‘‘+‘ + name + ‘+‘‘‘‘‘‘‘‘‘
14 WHEN TYPE_NAME(system_type_id) IN (‘DATETIME‘,‘UNIQUEIDENTIFIER‘)
15 THEN ‘‘‘‘‘‘‘‘‘+CAST(‘ + name + ‘ AS VARCHAR(100))+‘‘‘‘‘‘‘‘‘
16 ELSE ‘CAST(‘ + name + ‘ AS VARCHAR(100))‘
17 END
18 +‘ END‘
19 FROM sys.columns
20 WHERE object_id=OBJECT_ID(@table)
21 EXEC(‘SELECT ‘ + @insert_sql + ‘ FROM ‘ + @table )