Solved

Selecting one or all records in a stored procedure

Posted on 2013-01-08
6
218 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 125 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 125 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 21

Assisted Solution

by:Alpesh Patel
Alpesh Patel earned 125 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 125 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 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