3
Answers

Dynamic SQL Query

Riddhi Valecha

Riddhi Valecha

11y
1.1k
1
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
Answers (3)