Solved

Help with SQL Stored procedure - multiple search criteria

Posted on 2006-12-01
13
438 Views
Last Modified: 2010-05-18
I have a select statment:

Select shortssn, lastname, firstname, lastupdated, status
from people p
      join peopledepartment d on p.peopleid = d.people
where d.department IN (1,2,3) and p.status IN ('cleared','pending','new','do not use','Expired')
Order by lastname, firstname

I need to modify it as a stored procedure and pass a value for the list of departments and list of status.  So the amounts could be 0 or more departments and the status can be 0 or more status.

Can anybody help converting this to a stored procedure where I can dynamically change the search criteria.  

I must do it as a stored procedure unfortunately....
0
Comment
Question by:flg8tor96
  • 6
  • 5
  • 2
13 Comments
 
LVL 11

Expert Comment

by:rw3admin
ID: 18056058
you can create following proc and use something as simple as, but somehow I feel you want more out of it

Exec MyProc "1,2,3","'cleared','pending','new','do not use','Expired'"

Create Proc MyProc @DeptID Varchar(100), @Status Varchar(200)
as
Declare
@Query Varchar(1000)
Select @Query='
Select shortssn, lastname, firstname, lastupdated, status
from people p
     join peopledepartment d on p.peopleid = d.people
where d.department IN ('+@DeptID+') and p.status IN ('+@Status+')
Order by lastname, firstname'

Exec (@Query)
0
 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18056195
Or maybe something like:


SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[udfListToTable] (
      @ArrayList      varchar(8000),
      @Delimiter      varchar(20)
      )
RETURNS
      @ReturnTable table (
            value sql_variant
) AS


BEGIN
      SET @ArrayList = @ArrayList + @Delimiter
      
      DECLARE
            @iLastPosition int
      
      WHILE (PATINDEX ('%' + @Delimiter+ '%' , @ArrayList) <> 0)
      BEGIN
            SET @LastPosition =  PATINDEX ('%' + @Delimiter+ '%' , @ArrayList)
            
            INSERT @ReturnTable (
                  value
                  )
            VALUES (
                  LEFT (@ArrayList, @LastPosition - 1)
                  )
            
            SET @ArrayList = STUFF (@ArrayList, 1, @LastPosition, '')
      END

      RETURN
END
GO




EXEC MYPROC '1,2,3','cleared,pending,new,do not use,Expired'

CREATE PROC MYPROC @DeptID Varchar(100), @Status Varchar(200)
as


BEGIN


       SELECT shortssn, lastname, firstname, lastupdated, status
         FROM people p
         JOIN peopledepartment d
           ON   p.peopleid = d.people
    LEFT JOIN [dbo].[udfListToTable] (@DeptID, ',') depts
           ON d.department = depts.value
    LEFT JOIN [dbo].[udfListToTable] (@DeptID, ',') status
           ON p.status = status.value
    ORDER BY lastname, firstname

END
0
 

Author Comment

by:flg8tor96
ID: 18056249
Ok still having trouble - I created the procedure first

CREATE   procedure background.getstatusdg1
@departmentid varchar(100),
@status varchar(100)

as

Declare @SQL as Varchar(1000)

Select @SQL ='shortssn, lastname, firstname, lastupdated, status
from people p
      join peopledepartment d on p.peopleid = d.people
where d.department IN ('+@departmentid+') and p.status IN ('+@status+')
Order by lastname, firstname'

Exec(@SQL)

-------------------------------------------------------------------------------
Then I am trying to execute using

Declare @departmentID varchar(100), @status varchar(100)

set @DepartmentID = '1,2,3'
set @status = 'cleared,pending,new,do not use, expired'

Exec background.getstatusdg1 @departmentID, @status

and get - Line 1: Incorrect syntax near ','.

I've tried changing stuff around but may have something to do with how I am enclosing the @status variable since you had double quotes with single.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 13

Expert Comment

by:Atlanta_Mike
ID: 18056840

Your problem is most likely ...

'cleared,pending,new,do not use, expired'

These are character values. Each will have to be in a single quote to work in this case.

0
 
LVL 11

Expert Comment

by:rw3admin
ID: 18056971
Declare @departmentID varchar(100), @status varchar(100)

set @DepartmentID = '1,2,3'
set @status = "'cleared','pending','new','do not use', 'expired'" --- this whole thing is enclosed in double quotes

Exec background.getstatusdg1 @departmentID, @status


0
 

Author Comment

by:flg8tor96
ID: 18071096
I get -

Invalid column name ''cleared','pending','new','do not use', 'expired''.

it removed some of the quotes in the error msg.  It is executed exactly as rw3admin
has above
0
 
LVL 11

Expert Comment

by:rw3admin
ID: 18071152
its cause you have QUOTED_IDENTIFIER ON

Do this

SET QUOTED_IDENTIFIER OFF
Declare @departmentID varchar(100), @status varchar(100)

set @DepartmentID = '1,2,3'
set @status = "'cleared','pending','new','do not use', 'expired'" --- this whole thing is enclosed in double quotes

Exec background.getstatusdg1 @departmentID, @status
SET QUOTED_IDENTIFIER ON
0
 

Author Comment

by:flg8tor96
ID: 18076952
now I get

Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ','.
0
 
LVL 11

Accepted Solution

by:
rw3admin earned 500 total points
ID: 18077170
Oh... you were not using my code you were using cause when I run my example code it runs perfectly


CREATE   procedure background.getstatusdg1
.....

you see in that code you are missing Select

here is the correct syntax for your code

CREATE   procedure background.getstatusdg1
@departmentid varchar(100),
@status varchar(100)

as

Declare @SQL as Varchar(1000)

Select @SQL ='Select shortssn, lastname, firstname, lastupdated, status
from people p
     join peopledepartment d on p.peopleid = d.people
where d.department IN ('+@departmentid+') and p.status IN ('+@status+')
Order by lastname, firstname'

Exec(@SQL)
0
 

Author Comment

by:flg8tor96
ID: 18088573
Is it possible pass these variables to the stored procedure from a web server to return the dataset?  I tried but I think the error revolves around

SET QUOTED_IDENTIFIER OFF
Declare @departmentID varchar(100), @status varchar(100)

set @DepartmentID = '1,2,3'
set @status = "'cleared','pending','new','do not use', 'expired'" --- this whole thing is enclosed in double quotes

Exec background.getstatusdg1 @departmentID, @status
SET QUOTED_IDENTIFIER ON

Is there a way to pass these parameters to sql just passing the varchar variables?  Or set the SET QUOTED_IDENTIFIER OFF  from my webservice?

Or is that another post....
0
 
LVL 11

Expert Comment

by:rw3admin
ID: 18088822
ok forget about quoted identifier... lets do this
create following proc and then pass valuses as
Declare @departmentID varchar(100), @status varchar(100)
select @departmentID ='1,2,3',
         @status='*cleared*,*pending*,*new*,*do not use*, *expired*' -- see I am passing * instead of ' and using a replace function in proc to change these values

exec getstatusdg1 @departmentID, @status


CREATE   procedure background.getstatusdg1
@departmentid varchar(100),
@status varchar(100)
as
Declare @SQL as Varchar(1000)
select @status=replace(@status,'*','''')
Select @SQL ='Select shortssn, lastname, firstname, lastupdated, status
from people p
     join peopledepartment d on p.peopleid = d.people
where d.department IN ('+@departmentid+') and p.status IN ('+@status+')
Order by lastname, firstname'
Exec(@SQL)
0
 

Author Comment

by:flg8tor96
ID: 18088903
yes I like this one too, I did get the other to work, not sure how, but it works.  I think I will actually change to the above solution since it makes more sense to me.

I think somehow that .net handles the quotes.  My error before was I declared the Department ID as integer.

thanks..
0
 
LVL 11

Expert Comment

by:rw3admin
ID: 18088955
cool... glad to help

rw3admin
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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