-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.sql
25 lines (21 loc) · 862 Bytes
/
Main.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
DECLARE @TableName sysname = 'DocumentGroupSign';
DECLARE @HeadersTable TABLE(ColumnId int,ColumnName nvarchar(300));
INSERT INTO @HeadersTable
select column_id, replace(col.name, ' ', '_') ColumnName
from sys.columns col join sys.types typ on col.system_type_id = typ.system_type_id AND col.user_type_id = typ.user_type_id
where object_id = object_id(@TableName)
DECLARE @Result varchar(max) = ''
DECLARE @TheSelect nvarchar(max) = 'DECLARE @TheOutput varchar(MAX) = '' '';
SELECT @TheOutput = @TheOutput +'+'
'+ 'REPLACE(' + '''db.'+ @TableName +'.insert( '' + '+'
';
SELECT @TheSelect = @TheSelect +
'CONCAT(''' + ColumnName + ': "'', '+ColumnName+', ''",'') +
'
from @HeadersTable
SET @TheSelect =@TheSelect + '' + ''');'', '',);'', '');' + '
'')
FROM [' + @TableName + ']
PRINT @TheOutput';
set @Result = @Result + @TheSelect
EXEC (@Result)