Hi all...
I have a dynamic query as follow in stored procedure-
@EMPID int = NULL,
@ENAME varchar(50) = NULL
Declare @SQLQUERY as NVARCHAR(MAX)
set @SQLQUERY = 'Select * from EMP where 1=1'
if @EMPID IS NOT NULL
set @SQLQUERY = ' and EMP.ID ='+@EMPID
-----
Here I get an error-
Conversion failed when converting the nvarchar value 'select * From emp where EMP.ID=' to data type int.
Please help me out..
Thanks a lot.. in advance
Dynamic SQL Query
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.

Jignesh TrivediRank #16 · 50.3K reputationPosted Oct 9, 2013, 11:36 PM EDT
I think , problem is, you are going to concat int and varchar value
problem is in below line.... (i think)
set @SQLQUERY = ' and EMP.ID ='+@EMPID
try
set @SQLQUERY = ' and EMP.ID ='+ CAST(@EMPID AS VARCHAR(10))
hope this will help you...
Sunny SharmaRank #223 · 11.1K reputationPosted Oct 9, 2013, 10:56 PM EDT
Riddhi ValechaRank #794 · 2.9K reputationPosted Oct 10, 2013, 7:53 PM EDT
Thanks a lot ... to all...
@Jignesh Trivedi - Your solution worked out...Thanks A Ton !!