Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hello friends,
I'm trying to create Dynamic SQL statement in Stored Procedure, here is my following code

ALTER PROC usp_GetJobCheckListDetails_sel
(
	@strSEARCH VARCHAR(500)
)
AS
BEGIN
	--DECLARE @SQL AS VARCHAR(800)
	--SELECT @SQL = 'SELECT J.CheckListId, JobNo, DepartmentId, CheckListName, ControlValue, jobtype FROM tbl_JobCheckList J
	--INNER JOIN tbl_DepartmentCheckList D ON D.CheckListId=J.CheckListId
	--WHERE ' + @strSEARCH + ''
	--EXEC @SQL
	EXEC('SELECT J.CheckListId, JobNo, DepartmentId, CheckListName, ControlValue, jobtype FROM tbl_JobCheckList J
	INNER JOIN tbl_DepartmentCheckList D ON D.CheckListId=J.CheckListId
	WHERE ' + @strSEARCH + '')
END


EXEC usp_GetJobCheckListDetails_sel  'DepartmentId IN (2,4,5) AND jobtype = 5'


My quires are:
1. When i used the comment part as part of SP, then i get error saying not valid identifier. So i commented that part.
2. When i use later part, it does not give any result.

Any idea, where i going wrong?

Thanks in advance
Posted

Pass two parameters(DepartmentId, jobtype). Then modify the procedure based on that.
ALTER PROC usp_GetJobCheckListDetails_sel
(
	@deptId VARCHAR(500),
        @jobType VARCHAR(50)
)
AS
BEGIN
	
	SELECT J.CheckListId, JobNo, DepartmentId, CheckListName, ControlValue, jobtype FROM tbl_JobCheckList J
	INNER JOIN tbl_DepartmentCheckList D ON D.CheckListId=J.CheckListId
	WHERE DepartmentId IN (@deptId) AND jobtype = @jobType  
	
END

Then call the Sp like below.
EXEC usp_GetJobCheckListDetails_sel  ''2','4','5'','5'

This is the write way.
 
Share this answer
 
v3
Comments
dhage.prashant01 18-Apr-11 4:26am    
Nice idea, but any way to do by dynamic query?
Manfred Rudolf Bihy 18-Apr-11 4:59am    
Dynamic queries are subject to SQL injection and should be avoided. It really is better to do it in this way!
dhage.prashant01 18-Apr-11 4:55am    
tell me, My DepartmentId is integer how can we type cast @deptId to integer??
Toniyo Jackson 18-Apr-11 5:01am    
Keep it in VARCHAR only. Because you need to pass multiple department id. So pass the id like this.
EXEC usp_GetJobCheckListDetails_sel ''2','4','5'','5'.
Use single quotes for each id.
Manfred Rudolf Bihy 18-Apr-11 5:00am    
Better approach than using dynamic SQL, my 5+!
Dynamic SQL is vulnerable to SQL injection and should be avoided. :thumbsup:
try that in the Following way

SQL
DECLARE @Query  VARCHAR(800)
DECLARE @strSEARCH Varchar(50)
set @strSEARCH = 'ProductId = 1'
    set @Query = 'Select * from Products
    WHERE ' + @strSEARCH + ''
    EXEC (@Query)
 
Share this answer
 
Comments
dhage.prashant01 18-Apr-11 4:26am    
I think, i have tried this

DECLARE @SQL AS VARCHAR(800)
SELECT @SQL = 'SELECT J.CheckListId, JobNo, DepartmentId, CheckListName, ControlValue, jobtype FROM tbl_JobCheckList J
INNER JOIN tbl_DepartmentCheckList D ON D.CheckListId=J.CheckListId
WHERE ' + @strSEARCH + ''
EXEC @SQL

it gives error to me
Manfred Rudolf Bihy 18-Apr-11 4:58am    
Look again:
mahen said Set @SQL = and you said SELECT @SQL =
there really is a difference!
Manfred Rudolf Bihy 18-Apr-11 4:58am    
My 5!
Mahendra.p25 18-Apr-11 5:07am    
Thanks
The problem is only that you are calling EXEC without round brackets.

Try this

SQL
DECLARE @SQL AS VARCHAR(800)
    SELECT @SQL = 'SELECT J.CheckListId, JobNo, DepartmentId, CheckListName, ControlValue, jobtype FROM tbl_JobCheckList J
    INNER JOIN tbl_DepartmentCheckList D ON D.CheckListId=J.CheckListId
    WHERE ' + @strSEARCH + ''
    EXEC(@SQL)


Its perfectly working fine for me...

Thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900