The trouble is not enough single quotes.
You have:
'select * FROM dagent WHERE ROW_DATE = ''+@startDate+'' '
^^ ^^
In each case where you have two adjacent single quotes, you need a third too. The two single quotes map to one single quote, so the quoted string contains +@startDate+
, not the concatenation of your variable.
You need:
'select * FROM dagent WHERE ROW_DATE = '''+@startDate+''' '
Now the first two single quotes in the triplet map to a single quote; the third terminates the string, the +@startDate+
becomes string concatenation, and then the next single quote starts a new string, the two single quotes map to one quote, and the space and single quote finish the string.
How to debug?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…