Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
CREATE PROCEDURE [dbo].[sp_empInfo]
@empID int =null, 
@TesterName varchar(250)=null
as 
declare @strSql nvarchar(4000)

			SET	@strSql='Declare  users_cursor CURSOR FOR SELECT ID,TesterName FROM emp Where Status=1 '
				IF @empID IS NOT NULL AND LEN(@empID) > 0
					SET	@strSql = @strSql +' AND ID='+CAST(@empID AS NVARCHAR(10))
				IF @TesterName IS NOT NULL AND LEN(@TesterName) > 0
					SET	@strSql = @strSql +' AND TesterName='+@TesterName
		exec sp_executesql @strSql
   		open   users_cursor
				fetch next from users_cursor into @empID, @TesterName
						while @@FETCH_STATUS = 0 BEGIN
						---PROCESS
			    fetch next from users_cursor into @empID, @TesterName
	 	END
	 	close users_cursor
		deallocate users_cursor  


when i pass TesterName as Nil I'm getting Error 
Invalid column name 'Nil'


What I have tried:

Tried Appending N i.e. SET @strSql=N'Declare
but didn't work
Posted
Updated 21-Jul-16 21:21pm
v2
Comments
[no name] 22-Jul-16 3:07am    
Is there maybe simply a space missing in ' AND ID=' instead of ' AND ID= ' and also here ' AND TesterName='?
kedar001 22-Jul-16 3:18am    
NOPE Tried with adding Space,but didn't work
SET @strSql = @strSql +' AND TesterName= '''+@TesterName+''''
this is Working but is this right way ?
[no name] 22-Jul-16 3:22am    
I see. Yes this is the right way, having the value of @TesterName in single quotes.
pt1401 22-Jul-16 3:41am    
It isn't the right way, because it's horribly inefficient.
Try:-

IF @empID IS NOT NULL AND LEN(@empID) > 0
SELECT ID, TesterName FROM emp Where Status=1 AND ID=@empID
ELSE IF @TesterName IS NOT NULL AND LEN(@TesterName) > 0
SELECT ID, TesterName FROM emp Where Status=1 AND TesterName=@TesterName


Ah ok, that doesn't work when you supply both @empID & @TesterName
[no name] 22-Jul-16 3:44am    
And why you don't write this in your answer?

1 solution

SET @strSql = @strSql +' AND TesterName='+@TesterName

should be

SET @strSql = @strSql +' AND TesterName='''+@TesterName + ''''
 
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