• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Selecting one or all records in a stored procedure

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
VBBRett
Asked:
VBBRett
4 Solutions
 
Chris LuttrellSenior Database ArchitectCommented:
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
 
harshada_sonawaneCommented:
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
 
Chris LuttrellSenior Database ArchitectCommented:
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
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Alpesh PatelAssistant ConsultantCommented:
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
 
LIONKINGCommented:
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
 
Anthony PerkinsCommented:
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now