Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Help with SQL Stored procedure - multiple search criteria

Posted on 2006-12-01
13
Medium Priority
?
457 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
[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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 2000 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

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!

Question has a verified solution.

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

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…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
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…

664 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