forked from bernatagulloesbrina/PowerQuery
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtableToSqlInsertStatement
More file actions
23 lines (19 loc) · 914 Bytes
/
Copy pathtableToSqlInsertStatement
File metadata and controls
23 lines (19 loc) · 914 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
let
GenerateSQL = (tableName as text, inputTable as table) =>
let
// Get the column names from the table
columnNames = Table.ColumnNames(inputTable),
// Create the column list for the SQL statement
columnList = Text.Combine(List.Transform(columnNames, each "[" & _ & "]"), ", "),
// Create the list of value rows for the SQL statement
valueRows = List.Transform(Table.ToRows(inputTable), each
"(" & Text.Combine(List.Transform(_, each "'" & Text.Replace(Text.From(_), "'", "''") & "'"), ", ") & ")"
),
// Combine the value rows into a single string
valuesList = Text.Combine(valueRows, ", " & "#(lf)"),
// Generate the final SQL statement
sqlStatement = "INSERT INTO [" & tableName & "] (" & columnList & ") VALUES " & "#(lf)" & valuesList & ";"
in
sqlStatement
in
GenerateSQL