Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Selecting one or all records in a stored procedure

Posted on 2013-01-08
6
Medium Priority
?
220 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 27

Assisted Solution

by:Chris Luttrell
Chris Luttrell earned 375 total points
ID: 38757845
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 375 total points
ID: 38757858
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 27

Expert Comment

by:Chris Luttrell
ID: 38757868
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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 375 total points
ID: 38758518
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 375 total points
ID: 38758740
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
ID: 38761519
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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, …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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