3

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
1
  • Are you asking this from a programming language point of view, or do you just want to conceptually add this data directly into SQL Server? Commented Jun 8, 2017 at 4:32

3 Answers 3

6

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Is there a way to do this "transposed"? I.e. provide all the values to go in each column, rather than each row? I ask because I already have large arrays for each column and seems dumb to loop through them all just to restructure the data in terms of rows.
2
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)

8 Comments

I want to insert 1 in column1 ,2 in column2, 3 in column3, 4 in column4........... not like this
it does exactly the same. try it... @Vinod
i am geting like(1,2,3,4), (5,6,7,8), (9,10,11,12), (13,14,15,16) but i need 1 in column1 ,2 in column2, 3 in column3, 4 in column4...........
if youre running the exact code as above, youll get exact output as shown in your question.
this is what i executed -Select REPLACE( REPLACE('(1,2,3,4;5,6,7,8;9,10,11,12;13,14,15,16;)', ';', '), ('), ', ()', '') and i got (1,2,3,4), (5,6,7,8), (9,10,11,12), (13,14,15,16)
|
1
INSERT INTO TableName
VALUES 
(1,2,3,4),(5,6,7,8),(9,10,11,12),(13,14,15,16)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.