如何在SQL中的Insert Select构造中传递默认值?

我有一张桌子:

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

我想将 table2 插入 table1 ,其结构与此类似:

    insert into table1 select field1, field2, DEFAULT from table2

在我的示例中是否可以使用某些东西代替 DEFAULT 来完成任务?以前选择的表通常如何使用默认值插入?

有帮助吗?

解决方案

尝试

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

我使用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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top