2013-01-09 12 views
5

Załóżmy, że mam 2 tabele t1 i t2.wstaw używając wybierz różne

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 
} 

Chcę wstawić poniższy zestaw wyników do tabeli t2.

select distinct col1, col2, col3 from t1 

Jak mogę to osiągnąć za pomocą zapytania? Próbowałem poniżej instrukcji, ale wiem, że jest to błędne składniowo.

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

Odpowiedz

13
insert into t2 
select newid(),a.* 
from 
(Select distinct col1, col2, col3 from t1) a 
2

Można pominąć uniqueidentifier pole czy to automatycznie generowane.

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

Więcej na ten Using uniqueidentifier Data

+0

że jest tylko wtedy, gdy jest on generowany automatycznie, jeśli jest wyraźnie int lub null, ofc nie –

+0

To prawda, dzięki wykonana korekta – peterm

2

To powinno działać.

INSERT INTO t2 
SELECT NEWID(), col1, col2, col3 
FROM 
(
    SELECT DISTINCT col1, col2, col3 
    FROM t1 
)DT 
Powiązane problemy