Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3755162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:39:22+00:00 2026-05-19T09:39:22+00:00

I’m wondering if there’s a way to use ‘insert into’ on a list of

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:39:23+00:00Added an answer on May 19, 2026 at 9:39 am

    You can use this type of insert statement

    insert into tblMyTable (Col1, Col2, Col3)
    select 'value1', value, 'value3'
    from dbo.values2table('abc,def,ghi,jkl',',',-1) V
    

    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.

    CREATE function dbo.values2table
    (
    @values varchar(max),
    @separator varchar(3),
    @limit int -- set to -1 for no limit
    ) returns @res table (id int identity, [value] varchar(max))
    as
    begin
    declare @value varchar(50)
    declare @commapos int, @lastpos int
    set @commapos = 0
    select @lastpos = @commapos, @commapos = charindex(@separator, @values, @lastpos+1)
    while @commapos > @lastpos and @limit <> 0
    begin
        select @value = substring(@values, @lastpos+1, @commapos-@lastpos-1)
        if @value <> '' begin
            insert into @res select ltrim(rtrim(@value))
            set @limit = @limit-1
        end
        select @lastpos = @commapos, @commapos = charindex(@separator, @values, @lastpos+1)
    end
    select @value = substring(@values, @lastpos+1, len(@values))
    if @value <> '' insert into @res select ltrim(rtrim(@value))
    return
    end
    GO
    

    The parameters used are:

    1. ‘,’ = delimiter
    2. -1 = all values in the array, or N for just first N items

    solution is above, alternatives below

    Or, if you fancy, a purely CTE approach not backed by any split function (watch comments with <<<)

    ;WITH T(value,delim) AS (
         select 'abc,def,ghi', ','   --- <<< plug in the value array and delimiter here
    ),  CTE(ItemData, Seq, I, J) AS (
        SELECT
            convert(varchar(max),null),
            0,
            CharIndex(delim, value)+1,
            1--case left(value,1) when ' ' then 2 else 1 end
        FROM T
        UNION ALL
        SELECT
            convert(varchar(max), subString(value, J, I-J-1)),
            Seq+1,
            CharIndex(delim, value, I)+1, I
        FROM CTE, T
        WHERE I > 1 AND J > 0
        UNION ALL
        SELECT
            SubString(value, J, 2000),
            Seq+1,
            CharIndex(delim, value, I)+1, 0
        FROM CTE, T
        WHERE I = 1 AND J > 1
    )
    
    --- <<< the final insert statement
    insert into tblMyTable (Col1, Col2, Col3)
    SELECT 'value1', ItemData, 'value3'
    FROM CTE
    WHERE Seq>0
    

    XML approach

    -- take an XML param
    declare @xml xml
    set @xml = '<root><item>abc</item><item>def</item><item>ghi</item></root>'
    
    insert into tblMyTable (Col1, Col2, Col3)
    SELECT 'value1', n.c.value('.','varchar(max)'), 'value3'
    FROM @xml.nodes('/root/item') n(c)
    
    -- heck, start with xml string
    declare @xmlstr nvarchar(max)
    set @xmlstr = '<root><item>abc</item><item>def</item><item>ghi</item></root>'
    
    insert tblMyTable (Col1, Col2, Col3)
    SELECT 'value1', n.c.value('.','varchar(max)'), 'value3'
    FROM (select convert(xml,@xmlstr) x) y
    cross apply y.x.nodes('/root/item') n(c)
    

    In C# code, you would only use 4 lines starting with “insert tblMyTable …” and parameterize the @xmlstr variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.