Solved

Sql stored procedure help

Posted on 2008-06-26
3
252 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
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 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 142

Expert Comment

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

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

778 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