在日期列中插入默认日期获取空值? [英] insert default date in date column getting null value?

查看:129
本文介绍了在日期列中插入默认日期获取空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果date_no列通过在C#程序中使用插入查询获取空值,请帮助我如何在date_no列中插入默认日期。

help me hows to insert default date in date_no column if date_no column getting null value through insert query using in C# program.??????

推荐答案

让我们看看例子:

Let see the example:
DECLARE @dates TABLE(aDate DATETIME NULL)

DECLARE @myDate DATETIME

SET @myDate='2013-01-01'
INSERT INTO @dates (aDate)
VALUES(@myDate)

SET @myDate=NULL

INSERT INTO @dates (aDate)
VALUES(@myDate)





在上面的例子中, @dates 表定义了 aDate 字段,可以存储 NULL 值。因此,当您尝试添加可为空的值时,表格接受它。



将表格的定义更改为:



In above example, @dates table has defined aDate field which can store NULL values. So, when you try to add nullable values, table "accept" it.

Change the definition of your table to:

DECLARE @dates TABLE(aDate DATETIME NOT NULL)



并尝试添加NULL值。

会发生什么?发生错误:


and try to add NULL value.
What would happen? Error occurs:

Msg 515, Level 16, State 2, Line 12<br />
Cannot insert the value NULL into column ''aDate'', table ''@dates''; column does not allow nulls. INSERT fails.





如果要在NULL的情况下添加默认日期,请创建如下存储过程:



If you want to add default date in case of NULL, create stored procedure like this:

CREATE PROCEDURE AddNewDefaultDate
    @myDate DATETIME NULL
BEGIN
    INSERT INTO YourTable (DateColumn, Column1, Column2, Column3,... ColumnN)
    VALUES(COALESCE(@myDate, '1900-01-01'), 'a', 1,... 3)
END


AS prasad sekhar表示将表中date_no列的DEFAULT约束设置为要保存的默认值。不要通过C#代码中的insert查询将值插入此列。每当在表上执行插入操作时,它总是会在此列中插入默认值。
AS prasad sekhar said set DEFAULT constraint of your date_no column in table to the default value you want to save. do not insert the value to this column through insert query from your C# code. it will always insert the default value t this column whenever an insert operation is done on table.


这篇关于在日期列中插入默认日期获取空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆