?
Solved

Sql stored procedure help

Posted on 2008-06-26
3
Medium Priority
?
276 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 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
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…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

839 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