-
Notifications
You must be signed in to change notification settings - Fork 1
/
GenerateDapperInsert.sql
37 lines (32 loc) · 1.19 KB
/
GenerateDapperInsert.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
26
27
28
29
30
31
32
33
34
35
36
37
select 'public void Add' + table_name + '(' + table_name + ' ' + lower(table_name) + ')' + CHAR(13) +
'{' + CHAR(13) +
' using (var connection = new SqlConnection(_connectionString))' + CHAR(13) +
' {' + CHAR(13) +
' string sql = ' + CHAR(13) +
' @"INSERT INTO [' + table_name + '] (' + columns + ' )' + CHAR(13) +
' VALUES (' + sqlParameters + ' )";' + CHAR(13) +
CHAR(13) +
' var param = new' + CHAR(13) +
' {' + CHAR(13) +
dotNetParameters + CHAR(13) +
' };' + CHAR(13) +
CHAR(13) +
' connection.Execute(sql, param);' + CHAR(13) +
' }' + CHAR(13) +
'}'
from (
SELECT t.table_name,
STUFF((SELECT ', [' + c.column_name + ']'
FROM INFORMATION_SCHEMA.columns c
WHERE c.table_name = t.table_name
FOR XML PATH('')), 1, 1, '') AS columns,
STUFF((SELECT ', @' + c.column_name
FROM INFORMATION_SCHEMA.columns c
WHERE c.table_name = t.table_name
FOR XML PATH('')), 1, 1, '') AS sqlParameters,
STUFF((SELECT ', ' + c.column_name + ' = ' + lower(table_name) + '.' + c.column_name
FROM INFORMATION_SCHEMA.columns c
WHERE c.table_name = t.table_name
FOR XML PATH('')), 1, 1, '') AS dotNetParameters
FROM INFORMATION_SCHEMA.tables t
) info