如何选择创建为创建默认值的默认值,然后将其键入用户定义的数据类型



有一个表格,具有用户定义的数据类型的所有字段,而不是null。有一些默认值为CREATE DEFAULT,并伴随着那些用户定义的数据类型。给出了这种方式,我可能不会更改它。我需要从源有一些空的源中插入该表中的数据。如何在我的插入物中使用这些默认值?

我尝试使用sys.default_constraints的Defenition获得这些默认值,但是在sys.default_constraints中没有默认值为CREATE DEFAULT

'''sql

- 代码我可能不会更改

CREATE TYPE dt_myType FROM varchar(255) NOT NULL
GO
CREATE DEFAULT myDefault 
AS
''                     
GO
EXEC sp_bindefault 'myDefault', 'dt_myType'; 
GO
CREATE TABLE myTable (
    f1 dt_myType NOT NULL )
GO

- 我的代码

insert into myTable
 (f1)
select 
 case f
   when 1 then 'example'
   else --I need default here
 end
from ...
'''

如果您没有太多的列要测试,则可以做这样的事情:

insert into t (<other columns>, a, b)
    select <other columns>, x.a, x.b
    from x
    where x.a is not null and x.b is not null;
insert into t (<other columns>, a )
    select <other columns>, x.a
    from x
    where x.a is not null and x.b is null;
insert into t (<other columns>, b )
    select <other columns>, x.b
    from x
    where x.a is null and x.b is not null;
insert into t (<other columns>)
    select <other columns>
    from x
    where x.a is null and x.b is null;

这不能很好地概括到更多的列。

否则,我可以想到的一些选项:

  • 循环浏览每个记录(使用可怕的光标(,并根据NULL值的存在构造insert
  • 使用instead of触发器基本构造插入物,如上所述。
  • 插入数据so 所有值是默认值,然后用数据中的值更新它们。
  • 找出一种解析元数据中默认值的方法,将其表示为适当的类型,并将其包含在查询中。

最新更新