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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:20:56+00:00 2026-05-14T02:20:56+00:00

Is there any alternate way to create stored procedure without putting all query in

  • 0

Is there any alternate way to create stored procedure without putting all query in one long string if criteria of WWHERE clause can be different.

Suppose I have Orders table I want to create stored procedure on this table and there are three column on which I wnat to filter records.

1- CustomerId, 2- SupplierId, 3- ProductId.

If user only give CustomerId in search criteria then query should be like following

SELECT * FROM Orders WHERE Orders.CustomerId = @customerId

And if user only give ProductId in search criteria then query should be like following

SELECT * FROM Orders WHERE Orders.ProductId = @productId

And if user only all three CustomerId, ProductId, and SupplierId is given then all three Ids will be used in WHERE to filter.

There is also chance that user don’t want to filter record then query should be like following

SELCT * FROM Orders

Whenever I have to create this kind of procedure I put all this in string and use IF conditions to check if arguments (@customeId or @supplierId etc) has values.

I use following method to create procedure

DECLARE @query      VARCHAR(MAX)
DECLARE @queryWhere VARCHAR(MAX)

SET @query = @query + 'SELECT * FROM Orders '

IF (@originationNumber IS NOT NULL)
BEGIN
    BEGIN
        SET @queryWhere =@queryWhere + ' Orders.CustomerId = ' + CONVERT(VARCHAR(100),@customerId)
    END
END

IF(@queryWhere <> '')
BEGIN
   SET @query = @query+' WHERE ' + @queryWhere
END

EXEC (@query)

Thanks.

  • 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-14T02:20:57+00:00Added an answer on May 14, 2026 at 2:20 am

    You could pass NULL for fields that you don’t want to include in your WHERE-clause and check for NULL in the query:

    Select customerId, productId, supplierId
    From Orders
    Where ( customerId = @customerId Or @customerId IS NULL )
      And ( productId = @productId Or @productId IS NULL )
      And ( supplierId= @supplierId Or @supplierId IS NULL )
    

    Don’t use SELECT *, always list the columns you actually need.

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

Sidebar

Related Questions

Is there any way to check whether a file is locked without using a
Is there any better alternative for doing string formatting in VC6, with syntax checking
Is there any query which can return me the number of revisions made to
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox
Is there any alternative image manipulation library for .net? I would prefer something that
Is there any alternative for WPF (windows presentation foundation) in python? http://msdn.microsoft.com/en-us/library/aa970268.aspx#Programming_with_WPF
Is there any good alternative to ASpell? It's nice open source, but haven't been
Is there an alternative to history.go(-1) for FireFox and Safari. Any Help would be
Is there any free or commercial component written in .NET (no COM interop) that
Is there any efficiency difference in an explicit vs implicit inner join? For example:

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.