8

假设,我有 2 个表 t1 和 t2。

create table t1 
{
id int not null,
col1 int null,
col2 int null,
col3 int null
}

create table t2
{
id uniqueidentifier not null,
col1 int null,
col2 int null,
col3 int null
}

我想将下面的结果集插入到表 t2 中。

select distinct col1, col2, col3 from t1

如何使用查询来实现这一点?我尝试了以下语句,但我知道它在语法上是错误的。

insert into t2
select newid(), distinct col1, col2, col3 from t1
4

3 回答 3

21
insert into t2
select newid(),a.*
from
(Select distinct col1, col2, col3 from t1) a
于 2013-01-09T13:20:36.463 回答
2

如果它是自动生成的,您可以省略uniqueidentifier字段。

INSERT INTO t2 (col1, col2, col3)
SELECT DISTINCT col1, col2, col3 FROM t1

更多关于使用 uniqueidentifier 数据

于 2013-01-09T13:21:21.780 回答
2

这应该有效。

INSERT INTO t2
SELECT NEWID(), col1, col2, col3 
FROM
(
    SELECT DISTINCT col1, col2, col3 
    FROM t1
)DT
于 2013-01-09T13:23:35.143 回答