When I exec this
SET @Qry =
'SELECT [LMITNO] INTO ##TABLETEMP' + LTRIM(RTRIM(STR(@@SPID))) + '
FROM OPENQUERY(MAA,''SELECT DISTINCT [LMITNO] FROM MAA.ESMEL3.MVXDDTPRD.MILOMA
WHERE [LMITNO] LIKE ' + '''' + @ChemicalPreFix + '''' + ''')'
I get this.
SELECT [LMITNO] INTO ##TABLETEMP91 FROM
OPENQUERY(MAA,'SELECT DISTINCT [LMITNO]
FROM MAA.ESMEL3.MVXDDTPRD.MILOMA WHERE [LMITNO] LIKE ''CHE%''')
But the error I get is that there is incorrect syntax near CHE%. However the syntax looks okay to me. What's wrong?