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

Bar*_*hen 4 sql sql-server

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

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

       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')
Run Code Online (Sandbox Code Playgroud)

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

任何帮助表示赞赏.

谢谢!

mar*_*c_s 12

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

INSERT INTO dbo.Leads(Col1, Col2, Col4) VALUES('name', 'cityName', 'anotherValue')
Run Code Online (Sandbox Code Playgroud)

(Col3在这个例子中省略了)


Abe*_*ler 8

只需告诉它插入一个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')
Run Code Online (Sandbox Code Playgroud)