I’m wondering if there’s a way to use ‘insert into’ on a list of values. I’m trying to do this:
insert into tblMyTable (Col1, Col2, Col3)
values('value1', value2, 'value3')
So, what I’m trying to say is that value2 will be an array of strings. I’m going to put this in C# but the SQL statement is all I need really. I know I could just use a foreach and loop through my array but I figured there might be a better way sort of like the SELECT statement here: SQL SELECT * FROM XXX WHERE columnName in Array. It seems like a single query would be much more efficient than one at a time.
I’m using SQL Server 2008 R2. Thanks fellas!
You can use this type of insert statement
The ‘value’, ‘value3’ and ‘abc,def,ghi,jkl’ are the 3 varchar parameters you need to set in C# SQLCommand.
This is the supporting function required.
The parameters used are:
solution is above, alternatives below
Or, if you fancy, a purely CTE approach not backed by any split function (watch comments with <<<)
XML approach
In C# code, you would only use 4 lines starting with “insert tblMyTable …” and parameterize the @xmlstr variable.