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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:44:01+00:00 2026-05-15T19:44:01+00:00

Sorry for the long post, but most of it is code spelling out my

  • 0

Sorry for the long post, but most of it is code spelling out my scenario:

I’m trying to execute a dynamic query (hopefully through a stored proceedure) to retrieve results based on a variable number of inputs.

If I had a table:

(dbo).(People)
ID   Name   Age
1    Joe    28
2    Bob    32
3    Alan   26
4    Joe    27

I want to allow the user to search by any of the three columns, no problem:

DECLARE @ID int, @Name nvarchar(25), @Age int
SET @ID = 1
SET @Name = 'Joe'
SET @Age = null

SELECT *
FROM dbo.People
WHERE
(ID = @ID or @ID is null) AND
(Name like @Name or @Name is null) AND
(Age = @Age or @Age is null)

And I retrieve the result that I want.

Now, if I want to search for multiple fields in a column, I can do that no problem:

DECLARE @text nvarchar(100)
SET @text = '1, 3'

DECLARE @ids AS TABLE (n int NOT NULL PRIMARY KEY)

--//parse the string into a table
DECLARE @TempString nvarchar(300), @Pos int
SET @text = LTRIM(RTRIM(@text))+ ','
SET @Pos = CHARINDEX(',', @text, 1)
IF REPLACE(@text, ',', '') <> ''
BEGIN
    WHILE @Pos > 0
    BEGIN
        SET @TempString = LTRIM(RTRIM(LEFT(@text, @Pos - 1)))
        IF @TempString <> '' --just: IF @TempString != ''
        BEGIN
            INSERT INTO @ids VALUES (@TempString)
        END
        SET @text = RIGHT(@text, LEN(@text) - @Pos)
        SET @Pos = CHARINDEX(',', @text, 1)
    END
END


SELECT *
FROM   dbo.People
WHERE  
ID IN (SELECT n FROM @ids)

Now, my issue is I can’t seem to figure out how to combine the two since I can’t put:

WHERE
(Name like @Name or @Name is null) AND
(Id IN (SELECT n FROM @ids) or @ids is null)

Because @ids will never be null (since it’s a table)

Any help would be greatly appreciated!

Thanks in advance…and let me know if I can clarify anything

  • 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-15T19:44:02+00:00Added an answer on May 15, 2026 at 7:44 pm

    Try:

    (Id IN (SELECT n FROM @ids) OR NOT EXISTS (SELECT * FROM @ids))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry for this long post. The question is however small but requires full detail.
Sorry for long winded post. I am trying to understand UIScrollView and running into
sorry for the long question. I am trying to ajax post to collect a
Sorry for the long post, but this forum always asks for use cases :-).
I am sorry if the post is too long, but I would be happy
Sorry for this long post....But i have a headache from this task. I have
this post is a long one sorry for that but the problem is complex
Sorry for this long post, but is there perhaps a simply equivalent to this
Sorry for a long question and not a very descriptive title, but my problem
Firstly sorry for the long piece of code pasted below. This is my first

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.