Sql-Server

笛卡爾積 SQL / 具有多個值子句的插入

  • June 24, 2011

我想創建笛卡爾積 sql 查詢。但是,我得到了錯誤。我錯在哪裡?

alter table #Personel
(
Ad varchar(100)
)

alter table #Masa
(
MasaAdi varchar(100)
)

insert into #Personel values('deneme1'),('deneme2'),('deneme3')
insert into #Masa values('Masa1'),('masa2'),('masa3')
select Ad,MasaAdi from
(
  select ROW_NUMBER() over(partition by MasaAdi order by ID,Ad ) as Sira
     ,ID,Ad,MasaAdi
  from (
  select newid() as ID,Ad,MasaAdi from #Personel
  left outer join #Masa on 1=1
  ) t
) t2 where Sira = 1 order by Ad

在 SQL Server 上,您可以編寫

select Ad,MasaAdi from
(
select ROW_NUMBER() over(partition by MasaAdi order by ID,Ad ) as Sira,ID,Ad,MasaAdi
from (
select newid() as ID,Ad,MasaAdi 
from #Personel
cross join #Masa
)t
) t2 where Sira = 1 order by Ad

這是強調它是交叉連接的最佳語法。

但在 SQL Server 2008 上,您的查詢給出了相同的結果。

編輯:

您的問題不是選擇語句,而是插入。具有多個值子句的插入是 SQL Server 2008 的新功能。對於 2005,您必須編寫:

insert into #Personel values('deneme1')
insert into #Personel values('deneme2')
insert into #Personel values('deneme3')

insert into #Masa values('Masa1')
insert into #Masa values('masa2')
insert into #Masa values('masa3')

引用自:https://dba.stackexchange.com/questions/3459