Solved

Adding filter data in a stored procedure

Posted on 2008-10-24
11
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 …
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…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

763 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