Solved

Adding filter data in a stored procedure

Posted on 2008-10-24
11
438 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

785 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