Solved

Adding filter data in a stored procedure

Posted on 2008-10-24
11
439 Views
Last Modified: 2010-05-18
Hi

My SQL Server 2000 Stored Procedure below works fine.
But how do I add the @filterString into the SELECT statement?

I tried

if @directionData = 0
SELECT r.*, rs.*, c.* FROM requests AS r, requests_status AS rs, categories AS c WHERE r.status = rs.status_id AND r.category = c.category_id @filterString order by r.reuqest_id ASC

but that gave me a syntax error.

Thanks
AM
CREATE PROCEDURE usp_Request_GetRequests
(
	@directionData	int,
	@statusFilter int
) AS
 
declare @filterString varchar(500)
set @filterString = ''
if @statusFilter = 0
set @filterString = ' AND r.status = 0'
 
if @directionData = 0
SELECT r.*, rs.*, c.* FROM requests AS r, requests_status AS rs, categories AS c WHERE r.status = rs.status_id AND r.category = c.category_id order by r.reuqest_id ASC
if @directionData = 1
SELECT r.*, rs.*, c.* FROM requests AS r, requests_status AS rs, categories AS c WHERE r.status = rs.status_id AND r.category = c.category_id order by r.reuqest_id DESC

Open in new window

0
Comment
Question by:amoran
  • 4
  • 3
  • 2
11 Comments
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22797605
you can try it this way, i removed some if statments and use 1 query insted of 2 for order by with case
CREATE PROCEDURE usp_Request_GetRequests
(
        @directionData  int,
        @statusFilter int
) AS
 
 
SELECT 
	r.*, 
	rs.*, 
	c.* 
FROM requests AS r, requests_status AS rs, categories AS c 
WHERE 
	r.status = rs.status_id 
	AND r.category = c.category_id 
	AND (@statusFilter = 0 or r.status = 0 )
order by 
	case when @directionData = 0 then r.reuqest_id end, 
	case when @directionData = 1 then r.reuqest_id end DESC

Open in new window

0
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22797644
sorry line 16 should be like this

AND (@statusFilter <> 0 or r.status = 0 )
0
 
LVL 1

Expert Comment

by:GeniusSteals
ID: 22797663
Try

DECLARE @sql AS varchar(MAX)

SELECT @sql='
SELECT r.*, rs.*, c.* AS FilterString FROM requests AS r, requests_status AS rs, categories AS c WHERE ' + @FilterString + '
r.status = rs.status_id AND r.category = c.category_id order by r.reuqest_id ASC'

EXEC (@sql)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:amoran
ID: 22797723
Hi Cvijo
I think you might misunderstand what I'm getting at.
Ill have data like this ...

if @statusFilter = 0
set @filterString = ' AND r.status = 0'
if @statusFilter = 1
set @filterString = ' AND r.status = 4'
if @statusFilter = 2
set @filterString = ' AND r.status = 5'

but it's how to fit that into the select statement.
0
 
LVL 1

Expert Comment

by:GeniusSteals
ID: 22797838
My code would work as long as there is always an entry for @FilterString - I use dynamic queries like this a lot when the WHERE clause can change at run time
0
 
LVL 5

Accepted Solution

by:
Cvijo123 earned 20 total points
ID: 22797840
mybe something like this then:


CREATE PROCEDURE usp_Request_GetRequests
(
        @directionData  int,
        @statusFilter int
) AS
 
 
Declare @status int
 
if @statusFilter = -1 then 
	set @status = -1 -- we will ignore status filter is you send -1 as @statusFiter
 
if @statusFilter = 0 then 
	set @status =  0 
 
if @statusFilter = 1 then 
	set @status =  4 
 
if @statusFilter = 2 then 
	set @status =  5 
 
 
 
SELECT 
        r.*, 
        rs.*, 
        c.* 
FROM requests AS r, requests_status AS rs, categories AS c 
WHERE 
        r.status = rs.status_id 
        AND r.category = c.category_id 
        AND (@status = -1 or r.status = @status )
order by 
        case when @directionData = 0 then r.reuqest_id end, 
        case when @directionData = 1 then r.reuqest_id end DESC

Open in new window

0
 

Author Comment

by:amoran
ID: 22820275
Sorry for the delay guys ...


When I try

CREATE PROCEDURE usp_Request_GetRequests
(
      @directionData      int,
      @statusFilter int
) AS

DECLARE @sql AS varchar(MAX)

SELECT @sql='SELECT r.*, rs.*, c.* AS FilterString FROM requests AS r, requests_status AS rs, categories AS c WHERE ' + @statusFilter + '
r.status = rs.status_id AND r.category = c.category_id order by r.reuqest_id ASC'

EXEC (@sql)

I get an error

Must declare the vaiable @sql

Any ideas?
Thanks
0
 

Author Comment

by:amoran
ID: 22820348
And If I try

CREATE PROCEDURE usp_Request_GetRequests
(
      @directionData      int,
      @statusFilter int
) AS

DECLARE @sql AS varchar(500)
DECLARE @directionString AS varchar(500)
DECLARE @filterString As varchar(200)

--if @statusFilter = 1 set @filterString =  'r.status = 1 AND '


SELECT @sql='SELECT r.*, rs.*, c.* AS FilterString FROM requests AS r, requests_status AS rs, categories AS c WHERE ' + @statusFilter + 'r.status = rs.status_id AND r.category = c.category_id order by r.reuqest_id ASC'

EXEC (@sql)

GO


then I get

Syntax error converting the varchar value 'SELECT r.*, rs.*, c.* AS FilterString FROM requests AS r, requests_status AS rs, categories AS c WHERE ' to a column of data type int.
0
 
LVL 5

Expert Comment

by:Cvijo123
ID: 22820755
amoran what about my example ?
in my case you are not using dynamic SQL and everything you asked should work imo.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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…
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 setup several different housekeeping processes for a SQL Server.

808 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