将空字符串插入 SQL Server 的 INT 列 [英] Insert empty string into INT column for SQL Server
问题描述
一个SAMPLE
表只有一列ID
,类型为int
,默认为null.
A SAMPLE
table has only one column ID
of type int
, default null.
当我这样做时在 Oracle 中:
In Oracle when I do:
insert into SAMPLE (ID) values ('');
新记录添加了空白值.但在 SQL Server 2008 中,当我运行相同的 insert
语句时,新记录的值为 0.
the new record is added with blank value. But in SQL Server 2008, when I run the same insert
statement, the new record has the value of 0.
有没有办法强制 SQL Server 2008 默认空白字符串为 NULL 而不是 0(对于数字类型的列)?
Is there a way to force SQL Server 2008 to default blank string to NULL instead of 0 (for numerical type of columns)?
推荐答案
假设您的 INSERT 语句是在您的应用程序的许多地方重用的存储过程的一部分(或者,也许,批处理总是由相同的客户端代码的一部分)并且插入的值是作为字符串参数传递的数字,您可以像这样修改 INSERT:
Assuming that your INSERT statement is part of a stored procedure re-used in many places of your application (or, perhaps, is a batch always constructed by the same part of the client code) and that the inserted value is a number passed as a string argument, you could modify the INSERT like this:
INSERT INTO SAMPLE (ID) VALUES (NULLIF(@argument, ''));
这篇关于将空字符串插入 SQL Server 的 INT 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!