As per RyanDev's suggestion I'm posting this solution ...
BulletVictim[
^] said
Quote:
try two ' around the @fin_year
(''+@fin_year+''))
I responded
Quote:
Your error is in @sql. Can you post the output from the PRINT @sql command. BulletVictim is correct - you appear to missing single quotes around the string value.
OP has subsequently claimed that adding
group by
solved the problem
SELECT @fin_year = COALESCE (@fin_year + ',[' + fin_year + ']', '[' + fin_year + ']')
FROM after_audit_cc_trans group by fin_year
The
reason this works could be determined by examining the contents of
@fin_year
as this is being inserted into the
@sql
string for use in an
IN
clause.
in ('+@fin_year+'))
so the expected format is IN('value1','value2', 'value3') etc.
The "trick" here is (as the OP has done) to print the SQL that the
execute
command has been provided. It is usually far easier to see the problem when you can see the characters that the error reports ... "Incorrect syntax near '2010-20'" in this instance.