Solved

Selecting one or all records in a stored procedure

Posted on 2013-01-08
6
212 Views
Last Modified: 2013-01-23
I am trying to do a stored procedure where when a you have a value then it returns one record but if no value is inserted into the stored procedure, it returns all records.  So here is my stored procedure:

ALTER PROCEDURE
[dbo].[SP_SelectAllContinentsLoc]
@continentName      nvarchar(50)
AS
SELECT continentId, continentName
FROM
dbo.tblcontinents
WHERE
continentName = @continentName


So how would I set it that if @continentName is null, it returns all records?  Thanks!
0
Comment
Question by:VBBRett
6 Comments
 
LVL 26

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 125 total points
Comment Utility
ALTER PROCEDURE 
[dbo].[SP_SelectAllContinentsLoc]
@continentName      nvarchar(50) = NULL
AS
SELECT continentId, continentName
FROM
dbo.tblcontinents
WHERE
@continentName IS NULL
OR
continentName = @continentName

Open in new window

Now you can pass in NULL or not supply it at all and it should return all records from tblcontinents
0
 
LVL 2

Assisted Solution

by:harshada_sonawane
harshada_sonawane earned 125 total points
Comment Utility
u can use if else

ALTER PROCEDURE
[dbo].[SP_SelectAllContinentsLoc]
@continentName      nvarchar(50) = NULL
AS
if @continentName is null
    SELECT continentId, continentName
    FROM
    dbo.tblcontinents
else
     SELECT continentId, continentName
    FROM
    dbo.tblcontinents where continentName = @continentName
0
 
LVL 26

Expert Comment

by:Chris Luttrell
Comment Utility
I do not really recommend making a practice of using IF/ELSE with 2 selects in a stored procedure as it will cause performance issues with the optimizer and stored execution plans depending on which path is taken the first time the Procedure is called, but that is a much larger discussion.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 125 total points
Comment Utility
SELECT TOP
            (CASE WHEN (SELECT COUNT(1) FROM
            dbo.tblcontinents where continentName = @continentName)) > 0 THEN  (SELECT COUNT(1) FROM
            dbo.tblcontinents where continentName = @continentName) ELSE (SELECT COUNT(1) FROM
            dbo.tblcontinents) END )
      continentId, continentName
FROM
      dbo.tblcontinents
WHERE
      continentName = @continentName
0
 
LVL 13

Accepted Solution

by:
LIONKING earned 125 total points
Comment Utility
You can also try something like this:

ALTER PROCEDURE 
[dbo].[SP_SelectAllContinentsLoc]
@continentName      nvarchar(50)
AS
SELECT continentId, continentName
FROM
dbo.tblcontinents
WHERE
ISNULL(@continentName, continentName) = continentName

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I do not really recommend making a practice of using IF/ELSE with 2 selects
Actually I suspect it would actually perform better.  The real problem is that I suspect that this is not just a simple case of 2 SELECTs, but rather could morph into something much more complex.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now