2

I want to create cartesian product sql query.But,I'm getting the error.Where I wrong?

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
4
  • I tried your code on SQL Server 2008 and got no error. Which DBMS are you using. Do you get an error or do you get the wrong result?
    – bernd_k
    Commented Jun 23, 2011 at 14:42
  • If your platform allows, you should make your Cartesian product intent explicit by specifying CROSS JOIN. Commented Jun 23, 2011 at 17:13
  • Technically a CROSS JOIN in SQL isn't the same as the Cartesian Product but CROSS JOIN may be what you want. A comma (,) is the alternative syntax that can be used instead of CROSS JOIN (not all SQL versions support CROSS JOIN but most do support the "comma join" syntax)
    – nvogel
    Commented Jun 23, 2011 at 17:26
  • @dportas Can you provide a source for Cartesian Product <> CROSS JOIN? Commented Jun 23, 2011 at 20:39

1 Answer 1

3

On SQL Server you can write

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

This is the best syntax to emphasize that it is a cross join.

But on SQL Server 2008 your query gives the same result .

EDIT:

Your problem is not the select statement, but the inserts. Inserts with multiple value clauses is a new feature of SQL Server 2008. For 2005 you have to write:

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')
4
  • @bernd_k-thanks for your answer.I'm using SQL Server 2005.Here is my error code. Msg 102, Level 15, State 1, Line 10 Incorrect syntax near ','. Msg 102, Level 15, State 1, Line 11 Incorrect syntax near ','.
    – Cell-o
    Commented Jun 24, 2011 at 5:48
  • Extended my answer
    – bernd_k
    Commented Jun 24, 2011 at 6:28
  • I have a question that chaught my mind.for example, They are sitting different tables two times some people from personel list. For instance, e.g 1-deneme1 -masa2 , 2-deneme2-masa3 ,3-deneme1-masa1 How to provide to sit down separate to each table of each person?
    – Cell-o
    Commented Jun 24, 2011 at 7:36
  • Any suggestions?
    – Cell-o
    Commented Jun 24, 2011 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.