Sam
Sam

Reputation:

SQL Insert rows from two corresponding comma delimited sets of strings

I would like to take two separate strings of value pairs delimited by commas and insert each pair into a row in the database.

For example:

X = "1,2,3"     Y = "A,B,C"    =>     X  |  Y   
                                     ---------             
                                      1  |  A
                                      2  |  B
                                      3  |  C

I am using MSSQL 2008, but solutions for any database would be greatly appreciated. Also if there is a better method to handle inserting these sets of data other than just writing a SQL query please explain in detail.

Upvotes: 0

Views: 962

Answers (2)

onupdatecascade
onupdatecascade

Reputation: 3366

In SQL Server parse the lists using a method like this: http://www.sommarskog.se/arrays-in-sql-2005.html. Tried and true, works great.

Upvotes: 1

hobodave
hobodave

Reputation: 29303

Sam

INSERT INTO foo (X,Y) VALUES (1,'A'),(2,'B'),(3,'C');

In response to:

Also if there is a better method to handle inserting these sets of data other than just writing a SQL query please explain in detail.

There is no way to insert data into a SQL database other than a SQL query.

Upvotes: 0

Related Questions