Solved

Help with SQL Stored procedure - multiple search criteria

Posted on 2006-12-01
13
429 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
 
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

932 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

14 Experts available now in Live!

Get 1:1 Help Now