当 SQL 中的值为空时,如何跳过 INSERT 上的列条目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7691184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:22:52  来源:igfitidea点击:

How do I skip a column entry on INSERT when a value is blank in SQL?

sqlsql-server

提问by Barrett Kuethen

I'm inserting lots of rows into a database and some of the columns are blank for some of the rows.

我在数据库中插入了很多行,并且某些行的某些列是空白

How can I insert without assigning a dummy value to these blank fields?

如何在不为这些空白字段分配虚拟值的情况下插入?

       1 INSERT Leads VALUES('name', 'cityName', 5, 'anotherValue')
       2 INSERT Leads VALUES('name', 'cityName',  , 'anotherValue')
       3 INSERT Leads VALUES('name', 'cityName', 2, 'anotherValue')
       4 INSERT Leads VALUES('name', 'cityName', 9, 'anotherValue')

My problem lies in row 2 where there is a blank value in between city name and another value. Ideally, I'd like the value to remain null.

我的问题在于第 2 行,其中城市名称和另一个值之间有一个空白值。理想情况下,我希望该值保持为空。

Any help is appreciated.

任何帮助表示赞赏。

Thanks!

谢谢!

回答by marc_s

You should always explicitly specify which columns you're inserting to - then you can leave out those you don't want:

您应该始终明确指定要插入的列 - 然后您可以省略那些您不想要的列:

INSERT INTO dbo.Leads(Col1, Col2, Col4) VALUES('name', 'cityName', 'anotherValue')

(leaving out Col3here in this example)

Col3在这个例子中离开这里)

回答by Abe Miessler

Just tell it to insert a null:

只需告诉它插入一个空值:

    INSERT Leads VALUES('name', 'cityName', 5, 'anotherValue')
    INSERT Leads VALUES('name', 'cityName', null , 'anotherValue')
    INSERT Leads VALUES('name', 'cityName', 2, 'anotherValue')
    INSERT Leads VALUES('name', 'cityName', 9, 'anotherValue')

回答by rodneyrehm

You will have to find these fields and insert a NULL. The regular expression /,\s*,/ should identify the gaps. If you're running a linux/mac try:

您必须找到这些字段并插入 NULL。正则表达式 /,\s*,/ 应识别间隙。如果您运行的是 linux/mac,请尝试:

perl -pi -e 's/,\s*,/, NULL,/g' path/to/file.sql

回答by Mor Shemesh

Another method you can use:

您可以使用的另一种方法:

INSERT Leads VALUES('name', 'cityName', 5, 'anotherValue')
INSERT Leads VALUES('name', 'cityName', DEFAULT, 'anotherValue')
INSERT Leads VALUES('name', 'cityName', 2, 'anotherValue')
INSERT Leads VALUES('name', 'cityName', 9, 'anotherValue')

This would also work for columns that have default values and\or columns that you can't insert a value to (like TIMESTAMP)

这也适用于具有默认值的列和/或无法插入值的列(如 TIMESTAMP)

回答by Watermark Studios

You need to use nullif there is no value.

null如果没有价值,则需要使用。

回答by Andrew

For Insert statements Use Values(Null,...) if field accepts Null

对于 Insert 语句,如果字段接受 Null,请使用 Values(Null,...)

Use Values('',...) if field has (,not null) requirement,and for fields where you do not want Null to display e.g. a Note field.

如果字段有 (,not null) 要求,以及您不希望 Null 显示的字段,例如注释字段,请使用 Values('',...)。

回答by jpprade

I had the problem with an existing application that worked in prod but not on a fresh local install.

我的现有应用程序在 prod 中工作但在全新的本地安装中没有问题。

Mysql had to be configured like this :

Mysql 必须这样配置:

sql-mode=MYSQL40

and then it will accept row like :

然后它将接受如下行:

 INSERT Leads VALUES('name', 'cityName',  , 'anotherValue')