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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:32:03+00:00 2026-06-14T22:32:03+00:00

I have a stored procedure that returns me a set of data based on

  • 0

I have a stored procedure that returns me a set of data based on 2 input parameters. One of the parameter is optional so I am using

WHERE 
(tbl_Process.ProjectID = @ProjectID)
AND 
(tbl_AnalysisLookup.AnalysisCodeID = 7)
AND
(tbl_ProcessSubStep.ProcessID = ISNULL(@ProcessID,tbl_ProcessSubStep.ProcessID))

The @ProcessID is optional parameter so the user may/may not provide it.
Now I need to change my stored procedure to accommodate multiple ProcessId’s i.e. the user can now select a list of multiple ProcessId’s, Single ProcessID or No ProcessID and the stored proc should handle all these scenarios. What is the best way to achieve this without using Dynamic queries unless absolutely required.
In a nutshell, I wanted my stored proc to handle optional parameters with multiple values(WHERE IN Clause). The solution and relative link to the webpage I got it from has been provided below. It’s a very good article and will help you to choose the right solution based on your requirements.

  • 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-06-14T22:32:04+00:00Added an answer on June 14, 2026 at 10:32 pm

    I have finally figured out how to achieve this. There are a couple of ways to do this, what I am using now is a function to split a string of ProcessID’s based on delimiter and Then Inserting them into a table. Then using that table in my stored proc. Here is the code and the link to the webpage.

    http://www.codeproject.com/Articles/58780/Techniques-for-In-Clause-and-SQL-Server

    CREATE FUNCTION [dbo].[ufnDelimitedBigIntToTable]
    (
    @List varchar(max), @Delimiter varchar(10)
    )
    RETURNS @Ids TABLE
    (Id bigint) AS
    BEGIN
    DECLARE @list1 VARCHAR(MAX), @Pos INT, @rList VARCHAR(MAX)
    SET @List = LTRIM(RTRIM(@List)) + @Delimiter
    SET @pos = CHARINDEX(@Delimiter, @List, 1)
    WHILE @pos > 0
        BEGIN
        SET @list1 = LTRIM(RTRIM(LEFT(@List, @pos - 1)))
        IF @list1 <> ''
            INSERT INTO @Ids(Id) VALUES (CAST(@list1 AS bigint))
        SET @List = SUBSTRING(@List, @pos+1, LEN(@List))
        SET @pos = CHARINDEX(@Delimiter, @list, 1)
    END
    RETURN 
    END
    

    Once made, the table-function can be used in a query:

     Collapse | Copy Code
    CREATE PROCEDURE [dbo].[GetUsingDelimitedFunctionTable]
    @Ids varchar(max)
    AS
    BEGIN
    SET NOCOUNT ON
    SELECT s.Id,s.SomeString 
    FROM SomeString s (NOLOCK)
    WHERE EXISTS ( SELECT *
                   FROM ufnDelimitedBigIntToTable(@Ids,',') Ids
                   WHERE s.Id = Ids.id )
    END
    

    The Link also provides more ways to achieve this.

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

Sidebar

Related Questions

I have a MYSQL stored procedure SP1() that returns a result set. I want
I have a stored procedure that returns data in the format of: Date |
I have a stored procedure that takes no parameters, and it returns two fields.
I have a stored procedure that returns a result set (4 columns x n
I have a stored procedure that returns a set of rows for each ActivityUnitId
I have a stored procedure that returns a result with 250!!! columns. But I
I have the following iBatis mapping for an Oracle Stored Procedure that returns a
Let's say I have a stored procedure in Microsoft SQL Server 2005 that returns
I have a function that returns a date from a stored procedure, and it
I have a stored procedure that has the parameter: @desk VARCHAR(50) I want to

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.