SQL Server Nullable Column
I have a SQL Server table with a few columns of type varchar which are nullable. When an aspx page posts data if the textbox is empty, the database table column is updated to an empty string.
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.
Are there other better ways 开发者_StackOverflow中文版to handle this situation?
you can use a trigger or do it in the proc, you can use the NULLIF
function
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.
精彩评论