1

我在 SQL Server 2008R2 (10.5) 中有一个链接服务器设置,它使用 Microsoft OLE DB Provider for ODBC Drivers 连接到 Informix (Atomix) 数据库(它实际上指向使用 ODBC 驱动程序的 DSN)。通过这个,只要我插入的记录不尝试插入日期值,我就可以插入记录。我在日期值周围使用的分隔符和我尝试的 SQL 语法都没有关系——请参见示例:

INSERT INTO  [linkedinformix]...[tablename](daterequested) VALUES (2013-06-27) 

SELECT * FROM OPENQUERY(linkedinformix,'INSERT INTO tablename (daterequested) 
VALUES (2013-06-21))

以上将给出语法错误或类型冲突错误(或者在其他情况下,如果我不在进程外运行提供程序,则会导致 SQL Server 崩溃)。我尝试在我传递的日期值周围使用 {}、#、| 和其他分隔符,还尝试了不同的日期格式(2013 年 6 月 27 日等)。

如果我将 Microsoft Access 指向同一个 DSN 以创建链接表,我可以手动将日期写入表,因此我知道 ODBC 驱动程序可以处理它。

必须有一个简单的答案...

4

2 回答 2

1

Informix and DATE types — a fun (complicated) topic. Actually, it is fairly simple from within the Informix world; it is when other systems get involved with different views on how things should be done that it gets tricky.

If things are set up correctly (e.g. as in my environment), you could write in the Informix world:

INSERT INTO tablename(daterequested) VALUES('2013-07-03');

And you could substitute double quotes for single quotes because Informix is lax about the difference unless you hold its hand to the fire and say "I want to be shouted at when I use double quotes".

More elaborately, you could also write:

INSERT INTO tablename(daterequested) VALUES(DATETIME(2013-07-03) YEAR TO DAY);

This will work because (a) the formats for DATETIME are fixed in ISO 8601 (Date/Time Formats) or ISO 9075 (SQL) format and (b) Informix will convert from DATETIME YEAR TO DAY to DATE without any qualms. This is reliable, and doesn't rely on any environment variable settings or other complications, unlike the first version.

You could also reliably write:

INSERT INTO tablename(daterequested) VALUES(MDY(7, 3, 2013));

This uses a function MDY to convert the three integers to a DATE; the order of the arguments is (mnemonically) month, day, year. This is reliable because it doesn't depend on environment variables.

The first notation (using string '2013-07-03') relies on environment variables. The classic variable is $DBDATE; I run with DBDATE=y4md- set in the environment, so strings like '2013-07-03' are interpreted as in ISO 9075. However, the default value for DBDATE is effectively DBDATE=mdy4/ for US-style dates. However again, there are other variables, such as CLIENT_LOCALE, DB_LOCALE, and GL_DATE that all want to get in on the game. I use DBDATE because it gets top priority (and has done since the beginning of time), but the newer (other) variables have their merits. You can also experiment with:

INSERT INTO tablename(daterequested) VALUES(DATE('07/03/2013'))

Note the quotes and the parentheses. The string is interpreted according to the environment variables. Don't try DATE(2013-07-03) because that is equivalent to DATE(2003) (2013 minus 7 is 2006; 2006 - 3 is 2003), and because day 1 was 1900-01-01, day 2003 was 1905-06-16, a Monday.

The SQL standard provides for DATE '2013-03-07' but Informix does not support that parentheses-less notation.

You'll need to stick the SQL Server notations back into the SQL syntax, but the MDY and DATETIME methods will work, and you can finagle the DATE methods into working if you're willing to work with the environment variables or modify the format of the date string to match the expected behaviour.

于 2013-07-03T18:46:07.997 回答
0

根据此答案尝试格式 Mmm-DD-YY:

MS SQL Server 中的链接 Informix 表忽略条件

请注意,它区分大小写。

于 2013-06-27T22:03:18.523 回答