-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathTablesFromJSON.sql
More file actions
35 lines (34 loc) · 1.1 KB
/
TablesFromJSON.sql
File metadata and controls
35 lines (34 loc) · 1.1 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
CREATE PROCEDURE TablesFromJSON @TheJSON NVARCHAR(MAX)
/**
summary: >
This procedure returns a table for every one found in a JSON
string
Author: Phil Factor
Revision: 1.0
date: 1 Nov 2020
example:
- EXECUTE TablesFromJSON @TheJSON= N'[
{"name":"Phil", "email":"PhilipFactor@geeMail.com"},
{"name":"Geoff", "email":"Geoff2435@geeMail.com"},
{"name":"Mo", "email":"MoHussain34@geeMail.com"},
{"name":"Karen", "email":"KarenAlott34@geeMail.com"},
{"name":"Bob", "email":"bob32@geeMail.com"}
]'
returns: >
expression
**/
AS
DECLARE @expressions TABLE (id INT IDENTITY, TheExpression NVARCHAR(MAX));
INSERT INTO @expressions (TheExpression)
SELECT expression FROM OpenJSONExpressions(@TheJSON);
DECLARE @RowCount INT = -1, @ii INT = 1, @expressionToExcecute NVARCHAR(MAX);
WHILE @RowCount <> 0
BEGIN
SELECT @expressionToExcecute = TheExpression FROM @expressions WHERE id = @ii;
SELECT @RowCount = @@RowCount;
SELECT @ii = @ii + 1;
IF @RowCount > 0
EXECUTE sp_executesql @expressionToExcecute, N'@JSON NVARCHAR(MAX)',
@JSON = @TheJSON;
END;
GO