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

  • Home
  • SEARCH
  • 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 730979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:58:26+00:00 2026-05-14T06:58:26+00:00

How can I insert values from a comma-separated input parameter with a stored procedure?

  • 0

How can I insert values from a comma-separated input parameter with a stored procedure?
For example:

exec StoredProcedure Name 17,'127,204,110,198',7,'162,170,163,170'

you can see that I have two comma-separated value lists in the parameter list. Both will have the same number of values: if the first has 5 comma-separated values, then the second one also has 5 comma-separated values.

  • 127 and 162 are related
  • 204 and 170 are related

…and same for the others.

How can I insert these two values?
One comma-separated value is inserted, but how do I insert two?

  • 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-14T06:58:26+00:00Added an answer on May 14, 2026 at 6:58 am

    You need a way to split and process the string in TSQL, there are many ways to do this. This article covers the PROs and CONs of just about every method:

    “Arrays and Lists in SQL Server 2005 and Beyond, When Table Value Parameters Do Not Cut it” by Erland Sommarskog

    You need to create a split function. This is how a split function can be used:

    SELECT
        *
        FROM YourTable                               y
        INNER JOIN dbo.yourSplitFunction(@Parameter) s ON y.ID=s.Value
    

    I prefer the number table approach to split a string in TSQL but there are numerous ways to split strings in SQL Server, see the previous link, which explains the PROs and CONs of each.

    For the Numbers Table method to work, you need to do this one time table setup, which will create a table Numbers that contains rows from 1 to 10,000:

    SELECT TOP 10000 IDENTITY(int,1,1) AS Number
        INTO Numbers
        FROM sys.objects s1
        CROSS JOIN sys.objects s2
    ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
    

    Once the Numbers table is set up, create this split function:

    CREATE FUNCTION [dbo].[FN_ListToTableRows]
    (
         @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
        ,@List     varchar(8000)--REQUIRED, the list to split apart
    )
    RETURNS TABLE
    AS
    RETURN 
    (
        ----------------
        --SINGLE QUERY-- --this will return empty rows, and row numbers
        ----------------
        SELECT
            ROW_NUMBER() OVER(ORDER BY number) AS RowNumber
                ,LTRIM(RTRIM(SUBSTRING(ListValue, number+1, CHARINDEX(@SplitOn, ListValue, number+1)-number - 1))) AS ListValue
            FROM (
                     SELECT @SplitOn + @List + @SplitOn AS ListValue
                 ) AS InnerQuery
                INNER JOIN Numbers n ON n.Number < LEN(InnerQuery.ListValue)
            WHERE SUBSTRING(ListValue, number, 1) = @SplitOn
    );
    GO 
    

    You can now easily split a CSV string into a table and join on it. To accomplish your task, set up a test table to insert into:

    create table YourTable (col1 int, col2 int)
    

    then create your procedure:

    CREATE PROCEDURE StoredProcedureName
    (
         @Params1  int
        ,@Array1   varchar(8000)
        ,@Params2  int
        ,@Array2   varchar(8000)
    )
    AS 
    
    INSERT INTO YourTable
            (col1, col2)
        SELECT
            a1.ListValue, a2.ListValue
            FROM dbo.FN_ListToTableRows(',',@Array1)            a1
                INNER JOIN dbo.FN_ListToTableRows(',',@Array2)  a2 ON a1.RowNumber=a2.RowNumber
    GO
    

    test it out:

    exec StoredProcedureName 17,'127,204,110,198',7,'162,170,163,170'
    select * from YourTable
    

    OUTPUT:

    (4 row(s) affected)
    col1        col2
    ----------- -----------
    127         162
    204         170
    110         163
    198         170
    
    (4 row(s) affected)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.