473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

SQL Cross join

I have a SQL Server code which goes like this:

UPDATE ms
SET c1 = ms.c1 + ur.c1
FROM test ms
CROSS JOIN
test ur
WHERE ms.c2 = 'a'
AND ur.c2 = 'b'

Can someone please help me with the equivalent db2 code??

I am facing problems because of this.

Apr 13 '06 #1
11 8182
chettiar wrote:
I have a SQL Server code which goes like this:

UPDATE ms
SET c1 = ms.c1 + ur.c1
FROM test ms
CROSS JOIN
test ur
WHERE ms.c2 = 'a'
AND ur.c2 = 'b'

Can someone please help me with the equivalent db2 code??

I am facing problems because of this.

, <comma> :-)

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 13 '06 #2
chettiar wrote:
I have a SQL Server code which goes like this:

UPDATE ms
SET c1 = ms.c1 + ur.c1
FROM test ms
CROSS JOIN
test ur
WHERE ms.c2 = 'a'
AND ur.c2 = 'b'

Can someone please help me with the equivalent db2 code??

I am facing problems because of this.

MERGE INTO test ms USING test ur
ON ms.c2 = 'a' and ur.c2 = 'b'
WHEN MATCHED THEN UPDATE SET c1 = ms.c1 + ur.c1

This is a strange example btw...

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 13 '06 #3
SQL0788N The same row of target table "ADWINST1.TEST" was identified
more than once for an update, delete or insert operation of the MERGE
statement. SQLSTATE=21506

Thats the error I get when I am executing this query of yours.

Apr 13 '06 #4
chettiar wrote:
SQL0788N The same row of target table "ADWINST1.TEST" was identified
more than once for an update, delete or insert operation of the MERGE
statement. SQLSTATE=21506

Thats the error I get when I am executing this query of yours.

That's why I called the example weird....

Please provide some sample data along with the result you're getting
on SQL Server.
I wonder whether SQL server is doing what you expect it to do.
SQL Server does not protect you from updating the same row twice
resulting in non deterministic results.

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 13 '06 #5
the data is as follows

1 a
1 b
1 a
1 c
2 a
2 b

The sql server updates it properly. All I need is a way I can do it in
DB2.

Apr 13 '06 #6
chettiar wrote:
the data is as follows

1 a
1 b
1 a
1 c
2 a
2 b

The sql server updates it properly. All I need is a way I can do it in
DB2.

Define properly! Give us the result.

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 13 '06 #7
Cross joins create a Cartesian product, so what do you expect?

update ms
set c1 =
(
select sum(ms.c1+ur.c1) from ms,ur
where ms.c2 ='a' and ur.c2 = 'b'
)
where c2 = 'a'
;

-- Artur Wronski

Apr 14 '06 #8
chettiar wrote:
the data is as follows

1 a
1 b
1 a
1 c
2 a
2 b

The sql server updates it properly. All I need is a way I can do it in
DB2.


SQL Server may have done this properly in your view but it certainly
isn't guaranteed to do so reliably. More than one value is returned
from the join on the right hand side of the = assignment. The correct
result from the UPDATE is therefore officially undefined in SQL Server.
So if your code works for you today it is probably more by luck than by
design.

This is sloppy and dangerous code. Since it doesn't give any
well-defined result you'd better respecify exactly what result you
expect if you need help to port it.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Apr 16 '06 #9
Actually my problem is I am migrating the SQL Server Code into DB2. So
I am not pretty sure how I can go about doing the cartesian product
update.

Thats the reason I need help.

The data in SQL Server is like i already gave.

c1 c2
--- -----
1 a
1 b
1 a
1 c
2 a
2 b


RE****************************@acm.org wrote:
chettiar wrote:
the data is as follows

1 a
1 b
1 a
1 c
2 a
2 b

The sql server updates it properly. All I need is a way I can do it in
DB2.


SQL Server may have done this properly in your view but it certainly
isn't guaranteed to do so reliably. More than one value is returned
from the join on the right hand side of the = assignment. The correct
result from the UPDATE is therefore officially undefined in SQL Server.
So if your code works for you today it is probably more by luck than by
design.

This is sloppy and dangerous code. Since it doesn't give any
well-defined result you'd better respecify exactly what result you
expect if you need help to port it.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--


Apr 20 '06 #10
chettiar wrote:
Actually my problem is I am migrating the SQL Server Code into DB2. So
I am not pretty sure how I can go about doing the cartesian product
update.

Thats the reason I need help.

The data in SQL Server is like i already gave.

c1 c2
--- -----
1 a
1 b
1 a
1 c
2 a
2 b


What I said before. No-one can give you the equivalent DB2 code because
your code is broken. Even in SQL Server the result is undefined
(although you may be lucky and get some result that you like). So you
need to give a better specification.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Apr 20 '06 #11
chettiar wrote:
Actually my problem is I am migrating the SQL Server Code into DB2. So
I am not pretty sure how I can go about doing the cartesian product
update.

Thats the reason I need help.

The data in SQL Server is like i already gave.

c1 c2
--- -----
1 a
1 b
1 a
1 c
2 a
2 b

We need the expected OUTPUT.
What do you want the end result to be?

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 20 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ike | last post by:
Oh I have a nasty query which runs incredibly slowly. I am running MySQL 4.0.20-standard. Thus, in trying to expedite the query, I am trying to set indexes in my tables. My query requires four...
4
by: thilbert | last post by:
All, I have a perplexing problem that I hope someone can help me with. I have the following table struct: Permission ----------------- PermissionId Permission
3
by: Dam | last post by:
Using SqlServer : Query 1 : SELECT def.lID as IdDefinition, TDC_AUneValeur.VALEURDERETOUR as ValeurDeRetour FROM serveur.Data_tblDEFINITIONTABLEDECODES def,...
3
by: Klint Gore | last post by:
I've hit a situation where I'm getting an unexpected set of results from a cross join that I've narrowed down to a table alias. If I do (a has 4 rows, b has 1 row) select a.field1, b.* from a...
3
by: Martin | last post by:
Hello everybody, I have the following question. As a join clause on Oracle we use " table1.field1 = table2.field1 (+) " On SQL Server we use " table1.field1 *= table2.field1 " Does DB2...
3
by: Henrik Juul | last post by:
I have the following 2 tables: (BATCHES) BatchID KEY ID OrderID Action1DateTime Action2DateTime Action3DateTime Action4DateTime
0
by: metaperl | last post by:
How can I cross join all rows of table A with 1000 rows (first 1000, any 1000) of table B?
3
atksamy
by: atksamy | last post by:
I have 2 tables like which i would like to query to form a new table. table 1 number type serial index 1000001 613 3 1 1000001 613 3 ...
2
by: binui | last post by:
I have a table with fields facility,shift,line,date,No; of Items of size <7mm. I've to write a query to display the total no:of items having size <7mm in each line of a particular shift, of a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.