为什么默认值不添加到列添加插入时间? [英] Why default value is not adding to the column add the time of insert ?

查看:70
本文介绍了为什么默认值不添加到列添加插入时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





ALTER TABLE MyTable

ALTER COLUMN OldCol1位NOT NULL



ALTER MyTable

添加约束OldCol1ShouldBeFalse默认0为OldCol1



如果没有为此列提供任何值,我将默认设置为0 ...

但现在当我尝试在此表中插入一行而不向此列提供任何值时..



我当时期望默认值应该自动设置为0 ..但是它会抛出一个异常,将unbale插入到此列中..为什么这样.. ??它应该插入我的默认值..

Hi,

ALTER TABLE MyTable
ALTER COLUMN OldCol1 bit NOT NULL

ALTER MyTable
ADD CONSTRAINT OldCol1ShouldBeFalse DEFAULT 0 FOR OldCol1

I set default to 0 if no value is supplied to this column ...
But now when i try to insert a row into this table without supplying any value to this column ..

I was expecting that default should set to 0 automatically .. but it throwing an exception that unbale to insert null to this column .. why so ..?? it should insert my default value ..

推荐答案

嗨torakami。



这是SQL语句的方式应该看起来像是在SQL Server中将列的默认值设置为零。



Hi torakami.

This is how the SQL statement should be looking like for setting the Default value of a column to Zero in SQL Server.

ALTER TABLE [dbo].[MyTable]
ADD  CONSTRAINT [OldCol1ShouldBeFalse]  DEFAULT ((0)) FOR [OldCol1]





试试这个,让我们知道结果。



谢谢,

RelicV



Try this one and let us know the result.

Thanks,
RelicV


ALTER TABLE TableName
ADD DEFAULT (DefaultValue) FOR ColumnName


这篇关于为什么默认值不添加到列添加插入时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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