Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
246 views
in Technique[技术] by (71.8m points)

sql - Unable to pass variable in Informix

I was able to figure out how to get connected to Avaya CMS through Informix using SQL. The below query works but when I try to replace the ''1/01/19'' with a variable, I get the following error: "EIX000: (-1205) Invalid month in date"

Code that works

select * from Openquery(CMS, 'select * FROM dagent  WHERE ROW_DATE = ''1/01/19'' ');

Code that does not work

DECLARE @startDate DATETIME
SET @startDate = '2021-01-21'
select * from Openquery(CMS, 'select * FROM dagent WHERE ROW_DATE = ''+@startDate+'' ');

Does anyone have an idea what the problem could be?

question from:https://stackoverflow.com/questions/65926568/unable-to-pass-variable-in-informix

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?

  • Assign the string you used to a variable and print it.

  • Assign the string I suggest to a variable and print it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...