Vinod
Vinod

Reputation: 31

Insert Array of values into columns of the table in 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

Upvotes: 3

Views: 50898

Answers (3)

Neo
Neo

Reputation: 135

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)

Upvotes: 2

Harsh Sharma
Harsh Sharma

Reputation: 930

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)

Upvotes: 6

Hila DG
Hila DG

Reputation: 738

INSERT INTO TableName
VALUES 
(1,2,3,4),(5,6,7,8),(9,10,11,12),(13,14,15,16)

Upvotes: 1

Related Questions