Home TQL statement for inserting json integer array to table rows
Reply: 1

TQL statement for inserting json integer array to table rows

Aamir
1#
Aamir Published in 2018-02-12 17:52:48Z

How do I insert a JSON integer array to a table column in SQL server. Suppose I have the following json variable:

declare @jsonArray as varchar(max);
set @jsonArray = '[1,2,3,4]';

How do I insert the integer values into a table's integer type column using some of the SQL server JSON statements/functions?

Pawan Kumar
2#
Pawan Kumar Reply to 2018-02-12 17:57:46Z

You can use openjson to split the JSON data-

declare @jsonArray as varchar(max);
set @jsonArray = '[1,2,3,4]';
SELECT Value FROM OPENJSON(@jsonArray)

Value
-----------
1
2
3
4

(4 rows affected)

Please use like below

declare @jsonArray as varchar(max);
set @jsonArray = '[1,2,3,4]';
INSERT INTO yourTableName(ColumnName)
SELECT Value FROM OPENJSON(@jsonArray)

Read more from - https://msbiskills.com/2018/01/22/new-t-sql-features-in-sql-server-2016-xii-openjson-function-sql-server/

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30834 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO