-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerQueryToSQL.pq
More file actions
474 lines (473 loc) · 20.2 KB
/
Copy pathPowerQueryToSQL.pq
File metadata and controls
474 lines (473 loc) · 20.2 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
let
PowerQueryToSQLFunction =
(//
PQ_DATASOURCE as table, //
UPLOAD_SQL_SERVER as text, //
UPLOAD_SQL_DATABASE as text, //
UPLOAD_SQL_TABLE as text, //
UPLOAD_SQL_BATCH_ROWS as number, //
UPLOAD_SQL_IS_START as text, //
UPLOAD_SQL_DDL as text, //
UPLOAD_SQL_DML as text//
) as table =>
let
// Set source as a PQ Source
COMBINE_COL_VALUE = PQ_DATASOURCE,
// SET a temp table name
PQ_GUID =
let
__TBL_NAME =
// "tempdb."
"#"
//& UPLOAD_SQL_TABLE
& Text.Replace(Text.NewGuid(), "-", "")
in
__TBL_NAME,
// Log Start Time
START_TIME = DateTime.LocalNow(),
// COMBINE ALL TABLES AND GETS ORIGINAL COLUMNS NAME
TBL_COL_NAMES = Table.ColumnNames(COMBINE_COL_VALUE),
// Prepare Type TXT to convert
TBL_COLS_TYPE_TXT =
List.Transform(
TBL_COL_NAMES,
each
{
_,
type text
}
),
// GROUP DATA into Batch for Large Table
CREATE_BATCH_ID =
Table.AddIndexColumn(
COMBINE_COL_VALUE,
"BATCH_ID",
1,
1,
Int64.Type
),
// BASED on BatchRows Settings
CREATE_BATCH_GROUP =
Table.AddColumn(
CREATE_BATCH_ID,
"BATCH",
each Number.RoundUp([BATCH_ID] / UPLOAD_SQL_BATCH_ROWS)
),
// Group Rows into Table Object
CREATE_BATCH_TABLE =
Table.Group(
CREATE_BATCH_GROUP,
{"BATCH"},
{
{
"ROWS",
each Table.RowCount(_),
Int64.Type
},
{
"TBL",
each _
}
}
),
// CONCAT ALL COLS TO ONE COLS
// Using Locale en-GB to have Date Format in 'dd/MM/yyyy'
// In INSERT_STATEMENT I use SET DATEFORMAT DMY so Locale en-GB is necessary
// Use Before Batch SQL Script to setup DDL correctly becasue
// all String set to Unicode, all conversion will rely on implicit SQL Server.
// Concat using custom delimiter @@,N@@ to clean Single Quote first then convert it back.
CONCAT_COLS =
Table.AddColumn(
CREATE_BATCH_TABLE,
"CONCAT_COLS",
each
Table.CombineColumns(
Table.TransformColumnTypes(
[TBL],
TBL_COLS_TYPE_TXT,
"en-GB"
),
TBL_COL_NAMES,
Combiner.CombineTextByDelimiter(
"@@,N@@",
QuoteStyle.None
),
"SQL_CONCAT_COLS"
)
),
// Clean single quote to double quote for SQL Syntax Correction
CLEAN_SINGLE_QUOTE =
Table.AddColumn(
CONCAT_COLS,
"CLEAN_SINGLE_QUOTE",
each
let
__COL = [CONCAT_COLS]
in
Table.ReplaceValue(
__COL,
"'",
"''",
Replacer.ReplaceText,
{"SQL_CONCAT_COLS"}
)
),
// Convert back @@,N@@ to ',N'
CONVERT_DELIMITER =
Table.AddColumn(
CLEAN_SINGLE_QUOTE,
"CONVERT_DELIMITER",
each
let
__COL = [CLEAN_SINGLE_QUOTE]
in
Table.ReplaceValue(
__COL,
"@@,N@@",
"',N'",
Replacer.ReplaceText,
{"SQL_CONCAT_COLS"}
)
),
// ADD SELECT SYNTAX AND QUOTE
ADD_SELECT_SYNTAX =
Table.AddColumn(
CONVERT_DELIMITER,
"ADD_SELECT",
each
let
__COL = [CONVERT_DELIMITER]
in
Table.AddColumn(
__COL,
"ADD_SELECT",
each
" SELECT N'"
& [SQL_CONCAT_COLS]
& "' #(lf) "
)
),
// Convert all N'' to NULL value
CLEAN_NULL_VALUE =
Table.AddColumn(
ADD_SELECT_SYNTAX,
"CLEAN_NULL",
each
let
__COL = [ADD_SELECT]
in
Table.ReplaceValue(
__COL,
"N''",
"NULL",
Replacer.ReplaceText,
{"ADD_SELECT"}
)
),
// Temp Data Table Before Creat a Huge SQL Files
TMP_CLEAN_NULL_VALUE = Table.Buffer(CLEAN_NULL_VALUE),
// Combine all Rows into one SQL Query
INSERT_STATEMENT =
Table.AddColumn(
TMP_CLEAN_NULL_VALUE,
"SQL_QUERY",
each
let
__TBL = [CLEAN_NULL],
__BATCH_NO = [BATCH],
__ROWS = [ROWS]
in
if UPLOAD_SQL_IS_START = "YES" then
(
" SET DATEFORMAT DMY ; #(lf)"
& " INSERT INTO "
& PQ_GUID
& "#(lf)"
& Text.Combine(
__TBL[ADD_SELECT],
" UNION ALL #(lf)"
)
& "#(lf)"
& " INSERT INTO @LOG_PERF ([PROCEDURE], [AFFECTED ROWS], [TABLE]) SELECT 'INSERT TMP TABLE', "
& Text.From(__ROWS)
& ",'"
& PQ_GUID
& "' ;#(lf)"
)
else
" SELECT 0 AS UPLOAD_TIME ;#(lf)"
),
// Filter out unnecessary columns
FILTER_STATEMENT_COLS =
Table.SelectColumns(
INSERT_STATEMENT,
{
"BATCH",
"ROWS",
"SQL_QUERY"
}
),
CACHE_SQL_QUERIES = Table.Buffer(FILTER_STATEMENT_COLS),
// Buffer Table so that excel wont send another batchs
// Using List.Generate to ensure Batch is sent by orders
// ACCU_EXEC_TIME make sure previous query is done
EXEC_BATCHS =
let
// TEMP Final Batch Number
__FINAL_BATCH_NO = List.Max(CACHE_SQL_QUERIES[BATCH]),
__SQL_CONNECTION =
Sql.Database(
UPLOAD_SQL_SERVER,
UPLOAD_SQL_DATABASE
),
// Clean-up any leftover temptable because of disconnection
// Make sure only lastest queries running
__INIT =
"#(lf)
DECLARE @LOOP_DROP_TBL VARCHAR (255) ;
DECLARE LOOP_DROP_TBL CURSOR FOR
SELECT
'DROP TABLE IF EXISTS ' + TABLE_SCHEMA + '.' + TABLE_NAME + ''
FROM
INFORMATION_SCHEMA.TABLES
WHERE
(TABLE_SCHEMA + '.' + TABLE_NAME) LIKE '"
& UPLOAD_SQL_TABLE
& "________________________________' ;
OPEN LOOP_DROP_TBL ;
WHILE 1 = 1
BEGIN
FETCH LOOP_DROP_TBL
INTO
@LOOP_DROP_TBL ;
IF @@fetch_status != 0
BREAK ;
EXEC (@LOOP_DROP_TBL) ;
END ;
CLOSE LOOP_DROP_TBL ;
DEALLOCATE LOOP_DROP_TBL ;
#(lf)",
__DDL_EXEC =
" DECLARE @LOG_PERF TABLE
(
ID INT IDENTITY(1, 1),
[PROCEDURE] VARCHAR(200) NOT NULL,
TIMING DATETIME2 NOT NULL DEFAULT GETDATE(),
[TABLE] VARCHAR(200) NULL,
[AFFECTED ROWS] INT NULL
) ;
INSERT INTO @LOG_PERF ([PROCEDURE], [AFFECTED ROWS]) SELECT 'Starting', 0 ;#(lf)"
& UPLOAD_SQL_DDL
& "#(lf)"
& " INSERT INTO @LOG_PERF ([PROCEDURE], [AFFECTED ROWS], [TABLE]) SELECT 'EXEC DDL', 0, '"
& UPLOAD_SQL_TABLE
& "' ;#(lf)"
& " SELECT * INTO "
& PQ_GUID
& " FROM "
& UPLOAD_SQL_TABLE
& " WHERE 1 = 0 ;#(lf)",
__DML_EXEC =
"#(lf)"
& " BEGIN TRAN #(lf)"
& UPLOAD_SQL_DML
& "#(lf)"
& " INSERT INTO "
& UPLOAD_SQL_TABLE
& " WITH(TABLOCK) #(lf)"
& " SELECT * FROM "
& PQ_GUID
& "#(lf)"
& " INSERT INTO @LOG_PERF ([PROCEDURE], [AFFECTED ROWS], [TABLE]) SELECT 'EXEC DML', (SELECT COUNT(*) FROM "
& PQ_GUID
& "), '"
& UPLOAD_SQL_TABLE
& "' ;#(lf)"
& " DROP TABLE IF EXISTS "
& PQ_GUID
& "#(lf)"
& " COMMIT TRAN #(lf)"
& "
SELECT
ENDING.[PROCEDURE],
DATEDIFF( ms, STARTING.TIMING, ENDING.TIMING ) AS EXEC_TIME,
STARTING.TIMING AS START_TIME,
ENDING.TIMING AS END_TIME,
ENDING.[TABLE] AS TBL,
ENDING.[AFFECTED ROWS] AS ROWS
FROM
@LOG_PERF AS STARTING
INNER JOIN
@LOG_PERF AS ENDING ON ENDING.ID = STARTING.ID + 1 ;#(lf)",
__SQL_COMBINE =
if UPLOAD_SQL_IS_START = "YES" then
(
__INIT
& ";#(lf)"
& __DDL_EXEC
& ";#(lf)"
& Text.Combine(
CACHE_SQL_QUERIES[SQL_QUERY],
";#(lf)"
)
& ";#(lf)"
& __DML_EXEC
)
else
" SELECT 'SET IS_START TO YES TO BEGIN UPLOAD' WARNING "
in
// __SQL_COMBINE
Value.NativeQuery(
__SQL_CONNECTION,
__SQL_COMBINE,
null,
[EnableFolding = false]
)
in
EXEC_BATCHS,
// Add documentation
PowerQueryToSQLType =
type function (//
PQ_DATASOURCE as
(
type table
meta
[
Documentation.FieldCaption = "Source Table",
Documentation.FieldDescription = "The table to upload",
Documentation.SampleValues = {
"Queries Name"
}
]
), //
UPLOAD_SQL_SERVER as
(
type text
meta
[
Documentation.FieldCaption = "Target SQL Server Address",
Documentation.FieldDescription = "Ex: localhost; 192.168.1.1; ...",
Documentation.SampleValues = {
"localhost"
}
]
), //
UPLOAD_SQL_DATABASE as
(
type text
meta
[
Documentation.FieldCaption = "Target SQL Database Name",
Documentation.FieldDescription = "Ex: AdventureWorksDW2020; TailspinToys2020-US ...",
Documentation.SampleValues = {
"AdventureWorksDW2020"
}
]
), //
UPLOAD_SQL_TABLE as
(
type text
meta
[
Documentation.FieldCaption = "Target SQL Schema and Table Name",
Documentation.FieldDescription = "Ex: dbo.Date; dbo.Sales ...",
Documentation.SampleValues = {
"dbo.Sales"
}
]
), //
UPLOAD_SQL_BATCH_ROWS as
(
type number
meta
[
Documentation.FieldCaption = "Batch Rows for Split Large Table to Upload.",
Documentation.FieldDescription = "Ex: 100, 1000, 10000, ... Optimal is 1000",
Documentation.SampleValues = {
1000
}
]
), //
UPLOAD_SQL_IS_START as
(
type text
meta
[
Documentation.FieldCaption = "Functions Start",
Documentation.FieldDescription = "Ex: YES, NO",
Documentation.AllowedValues = {
"NO",
"YES"
},
Documentation.SampleValues = {
"YES"
}
]
), //
UPLOAD_SQL_DDL as
(
type text
meta
[
Documentation.FieldCaption = "DDL Statements. ",
Documentation.FieldDescription = "DDL Statemnents to be executed",
Documentation.SampleValues = {
" DROP TABLE... CREATE TABLE... "
},
Formatting.IsMultiLine = true,
Formatting.IsCode = true
]
), //
UPLOAD_SQL_BATCH_DML as
(
type text
meta
[
Documentation.FieldCaption = "DML Statements ",
Documentation.FieldDescription = "DML Statemnents to be executed",
Documentation.SampleValues = {
" TRUNCATE TABLE... DELETE FROM... "
},
Formatting.IsMultiLine = true,
Formatting.IsCode = true
]
)) as table
meta
[
Documentation.Name = "PowerQueryToSQL",
Documentation.Description = "Automate EXEC DML&DDL Before Upload PQ Object to SQL Server",
Documentation.LongDescription =
"
For ADVANCED USER only. This function will not replace SSIS or any kind of insert bulk, but it will:
<br> - Ultilize settings per table in excel workbook through Queries&Connection Task Pane.
<br> - Easy maintain DML&DDL before upload
<br> - Upload directly while Excel Workbook is opened.
<br>Limitation:
<br> - Unexpected import Data when user hover queries in Queries&Connection Task Pane
<br> - Slow compared to insert Bulk
",
Documentation.Category = "Table",
Documentation.Author = "Kit Cognac",
Documentation.Examples = {
[
Description = "Replace values on all columns",
Code =
"PowerQueryToSQL(
SOURCE_TABLE,
""localhost"",
""AdventureWorksDW2020"",
""dbo.Sales"",
1000,
""NO"",
"" CREATE TABLE dbo.Sales "",
"" TRUNCATE TABLE dbo.Sales ""
)",
Result = "Update Later."
]
}
]
in
Value.ReplaceType(
PowerQueryToSQLFunction,
PowerQueryToSQLType
)