使用原始默认属性值将一个表的一部分插入另一个表

时间:2017-12-14 21:33:35

标签: sql-server tsql

我有两个包含以下架构的表,

  • table1(a int,b int,c int,d int default 123)
  • table2(a int,b int,c int,e int)

我想将(a,b,c)的{​​{1}}列更新为table2,但保留默认值table1

例如,使用以下记录

d

------------------------
table1 | a | b | c | d |
------------------------
       | 1 | 2 | 3 | 4 |
------------------------

我想输出:

------------------------
table2 | a | b | c | e |
------------------------
       | 5 | 6 | 7 | 8 |
------------------------

以下是我的尝试:

--------------------------
table1 | a | b | c | d   |
--------------------------
       | 1 | 2 | 3 | 4   |
       | 5 | 6 | 7 | 123 |
--------------------------

此查询的语法无效,因为它返回子查询中的表并将其用作值。

我还试图避免使用下面的解决方案,因为它隐藏了使用默认值INSERT INTO table1(a, b, c, d) VALUES ((SELECT a, b, c FROM table2), DEFAULT) 的事实。我真的想明确地将其默认为明确。

d

有没有办法合并

INSERT INTO table1(a, b, c) 
    SELECT a, b, c  
    FROM table2

INSERT .. VALUES(..)

或者只是实现同一目标的另一种方式?

2 个答案:

答案 0 :(得分:1)

以下代码搜索特定表中特定列的默认值定义,并将其拼凑为整数。然后,该值可用于在其他语句中明确提供默认值,例如insert

-- if object_id( N'Samples', N'U' ) is not NULL
--   drop table Samples;

create table Samples ( Id Int Identity, Foo Int Default 42, Bar Int );

-- Get the default value from the metadata.
declare @FooDefaultValue as NVarChar(32); -- Note:   sys.default_constraints.definition   is declared as   NVarChar(max) .
select @FooDefaultValue = sdf.definition
  from sys.tables as st inner join
    sys.columns as sc on sc.object_id = st.object_id inner join
    sys.default_constraints as sdf on sdf.object_id = sc.default_object_id
    where st.name = 'Samples' and sc.name = 'Foo';
select @FooDefaultValue as 'Default Value NVarChar';

-- Cobble it from a string to an integer (since we know the correct data type).
declare @FooDefaultValueInteger as Int =
  Cast( Replace( Replace( @FooDefaultValue, '(', '' ), ')', '' ) as Int );
select @FooDefaultValueInteger as 'Default Value Integer';

-- Insert a few rows with data for both columns.
insert into Samples ( Foo, Bar )
  select Foo, Bar
    from ( values ( 1, 1 ), ( 2, 2 ), ( 3, 3 ) ) as Arthur( Foo, Bar );

-- Insert a few rows with an explicit default value for   Foo .
insert into Samples ( Foo, Bar )
  select @FooDefaultValueInteger, Bar
    from ( values ( 5 ), ( 6 ), ( 7 ) ) as Dent( Bar );

-- Display the results.
select * from Samples;

drop table Samples;

充其量只是笨拙。从字符串到整数的混乱使我感到脆弱。它可能应该有更多的模式名称验证,....当表是临时表时,它会变得更有趣。如果表是变量,则完全解开。通过分离从其使用中获取默认值,它打开了名称无法达成一致的可能性,即快速切割'n'paste而不编辑表或列名称可能导致坏事

所有人都说,我会避免这种情况,并在insert附近或其中添加评论,解释应该发生的事情。

答案 1 :(得分:0)

INSERT INTO table1 values (a, b, c) 
    SELECT a, b, c FROM table2