flg8tor96
asked on
Help with SQL Stored procedure - multiple search criteria
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....
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'
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....
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,n ew,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
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,n
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
ASKER
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.
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.
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.
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 @DepartmentID = '1,2,3'
set @status = "'cleared','pending','new'
Exec background.getstatusdg1 @departmentID, @status
ASKER
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
Invalid column name ''cleared','pending','new'
it removed some of the quotes in the error msg. It is executed exactly as rw3admin
has above
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
Do this
SET QUOTED_IDENTIFIER OFF
Declare @departmentID varchar(100), @status varchar(100)
set @DepartmentID = '1,2,3'
set @status = "'cleared','pending','new'
Exec background.getstatusdg1 @departmentID, @status
SET QUOTED_IDENTIFIER ON
ASKER
now I get
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ','.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ','.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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....
SET QUOTED_IDENTIFIER OFF
Declare @departmentID varchar(100), @status varchar(100)
set @DepartmentID = '1,2,3'
set @status = "'cleared','pending','new'
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....
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*,*pendin g*,*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)
create following proc and then pass valuses as
Declare @departmentID varchar(100), @status varchar(100)
select @departmentID ='1,2,3',
@status='*cleared*,*pendin
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)
ASKER
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..
I think somehow that .net handles the quotes. My error before was I declared the Department ID as integer.
thanks..
cool... glad to help
rw3admin
rw3admin
Exec MyProc "1,2,3","'cleared','pendin
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)