Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Array of values into columns of the table in SQL Server

Tags:

sql-server

I have an array

(1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16;)

I want to insert this array of values into the table like

Column1 |  Column2 | Column3 | Column4
-----------------------------------------
    1   |   2      |   3     |    4
    5   |   6      |   7     |    8
    9   |  10      |  11     |   12
   13   |  14      |  15     |   16
like image 974
Vinod Avatar asked Dec 10 '25 12:12

Vinod


2 Answers

Try this:

INSERT INTO TableName (Column1,Column2,Column3,Column4) 
VALUES (1,2,3,4),(5,6,7,8),(9,10,11,12),(13,14,15,16)
like image 72
Harsh Sharma Avatar answered Dec 12 '25 02:12

Harsh Sharma


DECLARE @array varchar(max) = '(1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16;)'
SET @array = REPLACE( REPLACE(@array, ';', '), ('), ', ()', '')
DECLARE @SQLQuery VARCHAR(MAX) = 'INSERT INTO TABLENAME (Column1,Column2,Column3,Column4) VALUES ' + @array
EXEC (@SQLQuery)
like image 41
Neo Avatar answered Dec 12 '25 01:12

Neo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!