?
Solved

Selecting one or all records in a stored procedure

Posted on 2013-01-08
6
Medium Priority
?
219 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

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 article I will describe the Copy Database Wizard 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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

777 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