Solved

Adding filter data in a stored procedure

Posted on 2008-10-24
11
437 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
 

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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…

746 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

12 Experts available now in Live!

Get 1:1 Help Now