Solved

Sql stored procedure help

Posted on 2008-06-26
3
230 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
Comment Utility
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
Comment Utility
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]
Comment Utility
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.

Join & Write a Comment

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

771 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

7 Experts available now in Live!

Get 1:1 Help Now