?
Solved

Sql stored procedure help

Posted on 2008-06-26
3
Medium Priority
?
270 Views
Last Modified: 2010-03-20
hello experts,
I have the following stored procedure;-

SELECT *
FROM Records
WHERE
groupid = (@groupid)
and functionid LIKE (@functionid)

The thing is that sometimes, the query may not require to filter by groupid and functionid and will just need to do a sraight search like so;-

SELECT *
FROM Records

What's the best way to implement this...(and why?)

Many thanks
0
Comment
Question by:claracruz
[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
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 21874864
for @functionid, passing '%' as value would do it.
for @groupid, the sql would need to be changed, for example:
SELECT *
FROM Records
WHERE ( groupid = @groupid or @groupid is null) 
and functionid LIKE @functionid 

and pass @functionid = '%' and @groupid = NULL to return all records.

Open in new window

0
 
LVL 4

Author Comment

by:claracruz
ID: 21875263
hi angellll,

Sorry, both are integer values so can't take null (well as far as I know anyways). what would be the replacement for this line;-
WHERE ( groupid = @groupid or @groupid is null)

where groupid is an integer
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21875416
integer variables and column CAN take nulls.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

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