开发者

Default values in Insert Select in SQL

开发者 https://www.devze.com 2022-12-12 10:59 出处:网络
How to pass default values in the Insert Select construction in SQL? I have a table: create table1 (field1 int, field2 int, field3 int default 1337)

How to pass default values in the Insert Select construction in SQL?

I have a table:

    create table1 (field1 int, field2 int, field3 int default 1337)
    create table2 (field1 int, field2 int)

I want to insert table2 into table1 with a construction similar to this:

    insert into table1 select field1, field2, DEFAULT from table2

Is it possible to use something instead of D开发者_Python百科EFAULT in my example to get the task done? How are previously selected tables usually inserted with default values?


Try

INSERT INTO table1 (field1, field2)
SELECT field1, field2 FROM table2

I tested this using SQL Server 2005

DECLARE @Table1 TABLE(
        field1 INT,
        field2 INT,
        field3 INT DEFAULT 1337
)

INSERT INTO @Table1 (field1,field2,field3) SELECT 1, 2, 3

DECLARE @Table2 TABLE(
        field1 INT,
        field2 INT
)

INSERT INTO @Table2 (field1,field2) SELECT 15, 16

INSERT INTO @Table1 (field1,field2)
SELECT  field1,
        field2
FROM    @Table2

SELECT * FROM @Table1
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号