Please help me to find the mistakes. I think it is due to apostrophe but I dont know how to correct.
My code returned error :
Msg 156, Level 15, State 1, Line 3 Incorrect syntax near the keyword 'or'.
Use YEAR_TO_DATE;
Declare @policy NVARCHAR(MAX);
Declare @account_list NVARCHAR(MAX);
Declare @sql NVARCHAR(MAX);
Set @policy ='TTY%';
Set @account_list='18000,19000';
Set @sql =
'Select
case
when [Debit_Account] in ('+@account_list+') and ([policy1] like '+@policy+' or [policy2] like '+@policy+' or [policy3] like '+@policy+') then -[Amount]
when [Credit_Account] in ('+@account_list+') and ([policy1] like '+@policy+' or [policy2] like '+@policy+' or [policy2] like '+@policy+') then [Amount]
else 0
end as [Amount]
from CTGS
where [Debit_Account] in ('+@account_list+') or [Credit_Account] in ('+@account_list+')';
EXEC sp_executesql @sql;