如何将默认为日期时间的小日期时间列更改为日期时间?
我的数据库中有许多列最初创建为 smalldatetime
,但实际上需要是 datetime
。其中许多列是使用 DDL 创建的:
[columnname] smalldatetime not null default getdate()
...这意味着为了更改列的类型,我首先必须删除默认值,然后在更改后重新创建默认值。
当然,我没有在 DDL 中指定默认值的名称,因此所有列都有默认值,其名称类似于 DF__CaseLock__CaseCo__182C9B23
。当我的应用程序创建其数据库时,它是通过执行脚本来完成的,因此我的客户数据库中的默认名称(我猜测;我尚未验证这一点)与我的数据库中的名称不同。
即使我知道默认约束的名称,我也无法弄清楚在删除它后将其添加回来的语法。事实上,我不清楚是否可以向现有列添加默认约束。
看来我必须做的是这样的:
declare @t table (id int not null primary key, date smalldatetime null)
insert into @t (id, date)
select id, date_column from my_table
drop constraint constraint_name
alter table my_table drop column date_column;
alter table my_table add date_column datetime default getdate()
update my_table set date_column = t.date FROM my_table m JOIN @t t ON m.id = t.ID
...只有当我知道 constraint_name
是什么时,我才能编写该脚本,因为我无法删除约束中引用的列。
这真的有那么难吗?
I have a number of columns in my database that were originally created as smalldatetime
and that really need to be datetime
. Many of these columns were created with the DDL:
[columnname] smalldatetime not null default getdate()
...which means that in order to alter the column's type, I first have to drop the default, and then re-create the default after altering it.
Of course, I didn't specify a name for the default in the DDL, so the columns all have defaults with names like DF__CaseLock__CaseCo__182C9B23
. And when my application creates its database, it does so by executing a script, so the names of the defaults in my customers' databases are (I'm guessing; I haven't verified this) different from their names in mine.
And even if I know the name of the default constraint, I can't figure out the syntax for adding it back in after I drop it. In fact, it's not clear to me that it's possible to add a default constraint to an existing column.
It appears that what I have to do is something like this:
declare @t table (id int not null primary key, date smalldatetime null)
insert into @t (id, date)
select id, date_column from my_table
drop constraint constraint_name
alter table my_table drop column date_column;
alter table my_table add date_column datetime default getdate()
update my_table set date_column = t.date FROM my_table m JOIN @t t ON m.id = t.ID
...only I can only write that script if I know what constraint_name
is, because I can't drop a column that's referenced in a constraint.
Is this really that hard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以像这样添加默认约束:
您可以询问 sys 表以查找现有约束。当您重新添加它们时,您应该为它们提供一个明确的名称。 这是一篇博客文章,其中有一个数字用于处理删除/添加未命名约束的脚本。希望有帮助!
You can add a default constraint like this:
You can interrogate the sys tables to find the existing constraints. When you add them back in, you should provide an explicit name for them. Here's a blog post that has a number of scripts for dealing with dropping/adding unnamed constraints. Hope that helps!
恐怕你必须求助于动态 SQL。
但请记住,之前由旧默认设置设置的任何数据都将保留。
编辑: my_table 中存在多个默认列:
I'm afraid you have to resort to dynamic SQL.
Bear in mind though that any data that was previously set by the old default will remain.
EDIT: Where multiple default columns exist in my_table: