简体   繁体   English

SQL Server Nullable Column

[英]SQL Server Nullable Column

I have a SQL Server table with a few columns of type varchar which are nullable. 我有一个SQL Server表,其中包含一些可以为null的varchar类型的列。 When an aspx page posts data if the textbox is empty, the database table column is updated to an empty string. 如果文本框为空,则aspx页面发布数据时,数据库表列将更新为空字符串。

To maintain the null value rather han have it replaced by an empty string, I can either have the logic to change the empty string to System.DBnull in the middle tier c# code or do so in the stored procedure. 为了保持空值而不是将它替换为空字符串,我可以使用逻辑将空字符串更改为中间层c#代码中的System.DBnull,或者在存储过程中执行此操作。

Are there other better ways to handle this situation? 还有其他更好的方法来处理这种情况吗?

you can use a trigger or do it in the proc, you can use the NULLIF function 你可以使用触发器或在proc中执行它,你可以使用NULLIF函数

Example

DECLARE @d VARCHAR(20)
SELECT @d = ''
SELECT NULLIF(@d,'')

I would probably put it near the code that makes a decision based on the distinction between an empty string and a null string. 我可能会把它放在根据空字符串和空字符串之间的区别做出决定的代码附近。 If there is no difference between their meaning or the resulting behavior of the application, I would suggest making the field non-nullable and sticking with an empty string. 如果它们的含义或应用程序的结果行为之间没有区别,我建议将该字段设置为不可为空并且使用空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM