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 718939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:35:07+00:00 2026-05-14T05:35:07+00:00

I am creating a stored procedure to which I want to pass as variable

  • 0

I am creating a stored procedure to which I want to pass as variable a comma delimited list of Ids. I want to use the Ids into a select statement, something like:

Create Procedure up_TEST
@Ids VARCHAR(MAX)
AS
SELECT * FROM ATable a
WHERE a.Id IN(@Ids)

Obviously I get the error that @Ids is a varchar and not an INT but how can I convert the comma delimited list?

  • 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-14T05:35:07+00:00Added an answer on May 14, 2026 at 5:35 am

    For those cases I use this table function, which you can adapt to your needs:

    CREATE FUNCTION dbo.f_params_to_list (@par VARCHAR(500))
    returns @result TABLE (value VARCHAR(30))
    AS  
    begin
         DECLARE @TempList table
              (
              value VARCHAR(30)
              )
    
         DECLARE @Value varchar(30), @Pos int
    
         SET @par = LTRIM(RTRIM(@par))+ ','
         SET @Pos = CHARINDEX(',', @par, 1)
    
         IF REPLACE(@par, ',', '') <> ''
         BEGIN
              WHILE @Pos > 0
              BEGIN
                   SET @Value = LTRIM(RTRIM(LEFT(@par, @Pos - 1)))
                   IF @Value <> ''
                   BEGIN
                        INSERT INTO @TempList (value) VALUES (@Value) --Use Appropriate conversion
                   END
                   SET @par = RIGHT(@par, LEN(@par) - @Pos)
                   SET @Pos = CHARINDEX(',', @par, 1)
    
              END
         END    
         INSERT @result
         SELECT value 
            FROM @TempList
         RETURN
    END    
    

    In your stored procedure you would use it like this:

    Create Procedure up_TEST
    @Ids VARCHAR(MAX)
    AS
    SELECT * FROM ATable a
    WHERE a.Id IN(SELECT value FROM dbo.f_params_to_list(@Ids))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If you're creating a temporary table within a stored procedure and want to add
I am creating a stored procedure in Sql Server 2008 database. I want to
I am creating a stored procedure which has to create a table which name
I am creating a stored procedure in MySQL 5.1.40 which needs to run a
I am creating a report with the data which calls the stored procedure and
I'm creating a stored procedure to return search results where some of the parameters
I'm a newbie when it comes to SQL. When creating a stored procedure with
In a stored procedure, when is #Temptable created in SQL Server 2005? When creating
I have a stored procedure in SQL 2005. The Stored Procedure is actually creating
I am creating a stored procedure to do a search through a table. I

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.