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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:49:11+00:00 2026-05-17T01:49:11+00:00

I have a procedure with a (slightly more complex) version of the below: CREATE

  • 0

I have a procedure with a (slightly more complex) version of the below:

CREATE PROC sp_Find_ID (
    @Match1 varchar(10),
    @Match2 varchar(10)
) AS

DECLARE @ID int

SELECT @ID = ID
FROM Table1
WHERE Match1 = @Match1
    AND Coalesce(Match2,@Match2,'') = Coalesce(@Match2,Match2,'')

SELECT @ID ID

Essentially Match1 is a mandatory match, but Match2 is both optional on the input to the procedure, and on the table being searched. The 2nd match succeeds where the input and/or the table Match2 values are null, or where they’re both the same (not null) value.

My question is: Is there a more efficient (or even more readable) way of doing this?

I’ve used this method a few times, and I feel slightly sullied each time (subjective dirtiness admittedly).

  • 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-17T01:49:11+00:00Added an answer on May 17, 2026 at 1:49 am

    Is there a more efficient (or even more readable) way of doing this?

    The example you provided, using COALESCE/etc is non-sargable. You need to separate things so only what needs to be present in the query is run:

    DECLARE @ID int
    
    IF @Match2 IS NOT NULL
    BEGIN
    
      SELECT @ID = t.id
        FROM TABLE1 t
       WHERE t.match1 = @Match1
         AND (t.match2 = @Match2 OR t.match2 IS NULL)
    
    END
    ELSE
    BEGIN
    
      SELECT @ID = t.id
        FROM TABLE1 t
       WHERE t.match1 = @Match1
    
    END
    
    SELECT @ID ID
    

    If you want this to occur in a single SQL statement, dynamic SQL is the only real alternative. I highly recommend reading The curse and blessing of dynamic SQL before reading further:

    DECLARE @SQL NVARCHAR(MAX)
        SET @SQL = N' SELECT @ID = t.id
                        FROM TABLE1 t
                       WHERE t.match1 = @Match1 '
    
        SET @SQL = @SQL + CASE 
                            WHEN @Match2 IS NOT NULL THEN
                              ' AND (t.match2 = @Match2 OR t.match2 IS NULL) ' 
                            ELSE 
                              ' '
                          END
    
    BEGIN
    
      EXEC sp_executesql @SQL,
                         N'@ID INT OUTPUT, @Match1 VARCHAR(10), @Match2 VARCHAR(10)',
                         @ID, @Match1, @Match2
    
    END
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a stored procedure that looks like: CREATE PROCEDURE dbo.usp_TestFilter @AdditionalFilter BIT =
I have a procedure with a lot of i := i +1; in it
We have a Procedure in Oracle with a SYS_REFCURSOR output parameter that returns the
I have stored procedure: ALTER PROCEDURE [dbo].[k_ShoppingCart_DELETE] @cartGUID nvarchar AS DELETE FROM [dbo].[k_ShoppingCart] WHERE
Which command will executed first,If a stored procedure have individual multiple select commands;
I have a strange, sporadic issue. I have stored procedure that returns back 5
I have a stored procedure in SQL 2005. The Stored Procedure is actually creating
I have a stored procedure that consists of a single select query used to
I have a stored procedure with the following header: FUNCTION SaveShipment (p_user_id IN INTEGER,
I have a stored procedure that returns multiple tables. How can I execute and

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.