SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Filtering by StatusId


Filtering by StatusId

Author
Message
jignesh209
jignesh209
SSC Veteran
SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)SSC Veteran (270 reputation)

Group: General Forum Members
Points: 270 Visits: 185
I have a Stored Procedure which returns data based on the StatusId.

DECLARE @StatusId INT=2

SELECT * FROM Employees
WHERE StatusId=ISNULL(@StatusId,StatusId)

If the StatusId is null,then it returns all the rows from the table Employees.
And if StatusId is passed,the query returns data based on the StatusId.

When @StatusId is passed as NULL,does the above query work as self-join?

Also,comparing the above query with the one mentioned below,which query is more appropriate(best practice).

When,I compared the query plan,both output are same.

SELECT * FROM Employees e
WHERE
(
CASE
WHEN @StatusId IS NULL THEN 1
WHEN @StatusId=e.StatusId THEN 1
ELSE 0
END
)=1

Thanks.
dwain.c
dwain.c
One Orange Chip
One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)One Orange Chip (28K reputation)

Group: General Forum Members
Points: 28533 Visits: 6431
I believe this article by Gail Shaw will provide you the answers you seek:
Catch-all Queries


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
GilaMonster
GilaMonster
SSC Guru
SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)SSC Guru (370K reputation)

Group: General Forum Members
Points: 370950 Visits: 46965
jerry209 (3/10/2013)
Also,comparing the above query with the one mentioned below,which query is more appropriate(best practice).


Both will perform pretty horrid on larger tables. You're looking at table scans to resolve either.

Is that behaviour really necessary? Can you instead have one stored proc for 'fetch all' and one for 'fetch filtered by status'? If you can, that's the better approach.

You wouldn't (I hope) write methods in C# that can do radically different things depending on the parameters so don't do it with SQL stored procedures.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


ryan.mcatee
ryan.mcatee
SSC-Enthusiastic
SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)

Group: General Forum Members
Points: 175 Visits: 134
So... you always want null results, but if @StatusId is null, return all rows? If so, I wonder how performance here would compare:

DECLARE @maxStatus int, @minStatus int
SELECT @maxStatus=9999999, @minStatus=-9999999

IF @StatusId IS NOT NULL SELECT @maxStatus=@StatusId, @minStatus=@StatusId

SELECT * FROM Employees
WHERE StatusId BETWEEN @minStatus AND @maxStatus
UNION
SELECT * FROM Employees
WHERE StatusId IS NULL



Rolleyes
ryan.mcatee
ryan.mcatee
SSC-Enthusiastic
SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)SSC-Enthusiastic (175 reputation)

Group: General Forum Members
Points: 175 Visits: 134
No, you are not necessarily returning all instances where StatusId is null, just remove the last three lines:

...UNION
SELECT * FROM Employees
WHERE StatusId IS NULL
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search