Solved

Sql stored procedure help

Posted on 2008-06-26
3
242 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

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. …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now