MS Access:如何将 NULL 插入 DateTime 字段

发布于 2024-09-15 04:02:59 字数 784 浏览 1 评论 0原文

我有一个 MS Access 数据库(令人难以忍受),并通过 PHP (ODBC) 与其通信。

我必须在 INSERT 语句中包含一个 DateTime 字段。该字段在 Access 中未定义为“必需”,这意味着它确实可以为 NULL,并且事实上 Access 数据库中的某些行已经为 NULL。

我遇到的问题很简单:如何通过 SQL 插入 NULL?我在网上找到的所有结果都通过 Visual Basic 或 C# 等解决了这个问题,而我在 PHP 中通过 ODBC 使用 SQL。

我已经尝试过以下操作:(

INSERT INTO table_name (datetime_field) VALUES (NULL)
INSERT INTO table_name (datetime_field) VALUES (#NULL#)
INSERT INTO table_name (datetime_field) VALUES ('NULL')
INSERT INTO table_name (datetime_field) VALUES ('#NULL#')
INSERT INTO table_name (datetime_field) VALUES ('')

我的查询中还有大约 30 个其他列。)

当我尝试“”或 NULL 时,我得到的确切错误是“条件表达式中的数据类型不匹配”。其他返回解析错误(可以理解)。

请注意,我必须在 INSERT 语句中包含该字段。该字段具有默认值,但在许多情况下,我正在传输的原始数据具有 NULL,该数据在目标数据库中也必须是 NULL。

提前致谢!

I have an MS Access database (intolerably enough), and communicating with it through PHP (ODBC).

There is a DateTime field that I have to include in my INSERT statement. This field is NOT defined as "Required" in Access, meaning that it is indeed NULL-able, and in fact some of the rows in the Access database are already NULL.

The problem I'm having is simple: How to insert NULL through SQL? All the results I've found online have addressed it from something like Visual Basic or C#, whereas I'm using SQL through ODBC in PHP.

I have already tried the following:

INSERT INTO table_name (datetime_field) VALUES (NULL)
INSERT INTO table_name (datetime_field) VALUES (#NULL#)
INSERT INTO table_name (datetime_field) VALUES ('NULL')
INSERT INTO table_name (datetime_field) VALUES ('#NULL#')
INSERT INTO table_name (datetime_field) VALUES ('')

(There's about 30 other columns in my query.)

The exact error I get is 'Data type mismatch in criteria expression' when I try '' or NULL. The others return a parse error (understandably).

Please note that I have to include the field in the INSERT statement. The field has a default value, but in many cases the original data that I'm transporting has a NULL that must also be a NULL in the target database.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

最舍不得你 2024-09-22 04:03:00

尝试将其留空

(values fld1,,fld2)

Try just leaving it blank

(values fld1,,fld2)
鸵鸟症 2024-09-22 04:03:00

您使用哪些库来与 ODBC 通信?
这可能是空值语法(库解释它的方式)的问题吗?

看看页面是否有帮助。
注意:我没有使用过 PHP。

What are the libraries, you are using in order to talk to ODBC?
Could it be a problem with the syntax for null values, the way library interprets it?

See, if this page helps.
Note: I have not worked with PHP.

梦开始←不甜 2024-09-22 04:03:00

我有这种类型的错误
试试这个。

INSERT INTO table_name (datetime_field) VALUES (DBNull.value)

它对我来说效果很好。

I have this type of error and
Try this.

INSERT INTO table_name (datetime_field) VALUES (DBNull.value)

It works fine for me.

败给现实 2024-09-22 04:03:00

我认为它区分大小写。我使用 NULL 并失败了。然后我用了Null。一切都好

I think it's case sensitive. I was using NULL and failing. Then I used Null. all good

把梦留给海 2024-09-22 04:02:59

尝试以下操作。它对我有用:

INSERT INTO sometable ( somedate, somethingelse )
SELECT Null AS Expr1, "foo" AS Expr2;

基本上,您将 null 包装在选择查询中,并让 SQL 弄清楚如何将其表示为插入。


-- 编辑 --

这也应该有效:

INSERT INTO sometable ( somedate, somethingelse )
values (Null , "foo");

但由于某种原因,它不适用于我的默认安装。

根据我的预感,我将数据库从 ANSI-89 切换到 ANSI-92,VALUES 方法开始工作。我把它改回 ANSI-89,它仍然有效。不仅如此,在我创建的任何新数据库上,它现在也可以工作。奇怪...安装中的某些东西必须通过来回切换而改变(并坚持),而不仅仅是 ANSI-89/92。这似乎就是我们得到不同结果的原因。

您可以通过转到 Office 徽标 -> 访问选项 -> 对象设计器 -> 查询设计来切换数据库 ocwe。更改 SQL Server 兼容语法 (ANSI 92) - 并检查“此数据库”。

好吧,很奇怪。

Try the following. It works for me:

INSERT INTO sometable ( somedate, somethingelse )
SELECT Null AS Expr1, "foo" AS Expr2;

Basically, you are wrapping the null in the select query and letting SQL figure out how to represent it to the insert.


-- EDIT --

This SHOULD also work:

INSERT INTO sometable ( somedate, somethingelse )
values (Null , "foo");

But for some reason it doesn't with my default install.

On I hunch, I switched my DB from ANSI-89 to ANSI-92, and the VALUES method started working. I switched it back to ANSI-89, and it still works. Not only that, on ANY new database I create, it now also works. Weird... something in the installation must be getting changed, (and sticking) by the switching back and forth that's not just ANSI-89/92. This seems to be why we were getting different results.

You can switch the database ocwe by going to Office Logo->Access Options->OBJECT DESIGNERS->QUERY DESIGN. Change SQL Server Compatible Syntax (ANSI 92) - and checking "This database".

Ok, very odd.

不可一世的女人 2024-09-22 04:02:59

我知道您已经弄清楚了这一点,但还有 dbNull

I know you've already figured this out but there is also dbNull

一向肩并 2024-09-22 04:02:59

INSERT INTO table_name (datetime_field) VALUES (DbNull.Value)

INSERT INTO table_name (datetime_field) VALUES (DbNull.Value)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文