I am executing the following sql. I get a syntax error which is (Incorrect syntax near '=') The query executes fine and gives proper results when executed normally. couldn't understand. plz take a look.
DECLARE @pvchMachineId VARCHAR(100) = ''
DECLARE @pvchMake VARCHAR(100) = ''
DECLARE @sql NVARCHAR(1000)
SELECT @sql = ' SELECT TOP 20 x.intId, x.vchMachineId, x.AUDenom, x.intGroupId,
x.vchMake, x.vchModel, x.mCurrency
from dbo.Machine x
inner join
(select max(m1.AUDenom) as audenom, m1.vchMachineId
from dbo.Machine m1
left JOIN dbo.ImportedFile ife on m1.intImportedFileId = ife.intId
WHERE ife.dtFileDate >= ''1-1-2013'' AND ife.dtFileDate <= ''1-29-2014'' AND
--following two lines cause the error
(' + @pvchMake + '= ''0'' OR m1.vchMake = @pvchMake) AND
(' + @pvchMachineId +'= ''0'' OR m1.vchMachineId = @pvchMachineId)
group by vchMachineId) y
on x.AUDenom = y.audenom and x.vchMachineId = y.vchMachineId
ORDER BY x.AUDenom DESC'
--in sql ends when the line ends. Same in dynamic sql.