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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:32:35+00:00 2026-06-04T15:32:35+00:00

Following is sample query. CREATE PROCEDURE GetModel ( @brandids varchar(100), — brandid=1,2,3 @bodystyleid varchar(100)

  • 0

Following is sample query.

CREATE PROCEDURE GetModel
(
    @brandids varchar(100), -- brandid="1,2,3"
    @bodystyleid varchar(100) -- bodystyleid="1,2,3"

)
AS
   select * from model
   where brandid in (@brandids) -- use a UDF to return table for comma delimited string
   and bodystyleid in (@bodystyleid)

My requirement is that if @brandids or @bodystyleid is blank, query should return all rows for that condition.

Please guide me how to do this? Also suggest how to write this query to optimize performance.

  • 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-04T15:32:37+00:00Added an answer on June 4, 2026 at 3:32 pm

    You’ll need dynamic SQL or a split function for this anyway, since IN ('1,2,3') is not the same as IN (1,2,3).

    Split function:

    CREATE FUNCTION dbo.SplitInts
    (
       @List       VARCHAR(MAX),
       @Delimiter  CHAR(1)
    )
    RETURNS TABLE
    AS
       RETURN ( SELECT Item = CONVERT(INT, Item) FROM ( 
         SELECT Item = x.i.value('(./text())[1]', 'int') FROM ( 
           SELECT [XML] = CONVERT(XML, '<i>' + REPLACE(@List, @Delimiter, '</i><i>') 
           + '</i>').query('.') ) AS a CROSS APPLY [XML].nodes('i') AS x(i)) AS y
         WHERE Item IS NOT NULL
       );
    

    Code becomes something like:

    SELECT m.col1, m.col2 FROM dbo.model AS m
    LEFT OUTER JOIN dbo.SplitInts(NULLIF(@brandids, ''), ',') AS br
    ON m.brandid = COALESCE(br.Item, m.brandid)
    LEFT OUTER JOIN dbo.SplitInts(NULLIF(@bodystyleid, ''), ',') AS bs
    ON m.bodystyleid = COALESCE(bs.Item, m.bodystyleid)
    WHERE (NULLIF(@brandids, '') IS NULL OR br.Item IS NOT NULL)
    AND (NULLIF(@bodystyleid, '') IS NULL OR bs.Item IS NOT NULL);
    

    (Note that I added a lot of NULLIF handling here… if these parameters don’t have a value, you should be passing NULL, not “blank”.)

    Dynamic SQL, which will have much less chance of leading to bad plans due to parameter sniffing, would be:

    DECLARE @sql NVARCHAR(MAX);
    
    SET @sql = N'SELECT columns FROM dbo.model
    WHERE 1 = 1 '
    + COALESCE(' AND brandid IN (' + @brandids + ')', '')
    + COALESCE(' AND bodystyleid IN (' + @bodystyleid + ')', '');
    
    EXEC sp_executesql @sql;
    

    Of course as @JamieCee points out, dynamic SQL could be vulnerable to injection, as you’ll discover if you search for dynamic SQL anywhere. So if you don’t trust your input, you’ll want to guard against potential injection attacks. Just like you would if you were assembling ad hoc SQL inside your application code.

    When you move to SQL Server 2008 or better, you should look at table-valued parameters (example here).

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

Sidebar

Related Questions

I have following rather simple query select count(*) from tbl t1, tbl t2 For
I have following procedure: CREATE PROCEDURE getProjectTeams(IN p_idProject INTEGER) BEGIN SELECT idTeam, name, workersCount,
In the following sample, I build a query to do a bulk insert into
Lets suppose that I have the following simple query var q = from p
I want to translate following simple sql query into Linq to NHibernate: SELECT NewsId
With a table of the following structure and sample data: TableActivity ------------- Type VARCHAR(8)
I receive input from the server in the following manner (sample input data): [1284336000]:
Hey guys I have the following sample. I have navigation which is loaded from
I have the following sample database set up - CREATE TABLE IF NOT EXISTS
The following simple query takes a very long time (several minutes) to execute. 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.