473,398 Members | 2,188 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,398 software developers and data experts.

How to convert this Oracle SQL(CONNECT BY) to DB2 SQL

Hi,

I am migrating an application from Oracle to DB2. There is an Oracle SQL like:

SELECT c1, c2
FROM table
CONNECT BY c2 = PRIOR c1
START WITH c2 IN ('1','2')

My question is how to convert above SQL to DB2 SQL.

Thanks in advance,
Michael
Nov 12 '05 #1
5 17460
Here is the SQL Standard version (also supported by SQL Server 2005)
which works in DB2 for LUW
WITH rec(c1, c2)
AS (SELECT c1, c2 FROM table WHERE c2 IN ('1','2')
UNION ALL
SELECT table.c1, table.c2 FROM table, rec WHERE table.c2 = rec.c2)
SELECT c1, c2 FROM rec

Cheers
Serge
Nov 12 '05 #2

"Michael" <mi**********@rogers.com> wrote in message
news:8f**************************@posting.google.c om...
Hi,

I am migrating an application from Oracle to DB2. There is an Oracle SQL like:
SELECT c1, c2
FROM table
CONNECT BY c2 = PRIOR c1
START WITH c2 IN ('1','2')

My question is how to convert above SQL to DB2 SQL.

Can you point us to an SQL Reference manual for Oracle? I've never seen a
statement like this in DB2 but if I can see an Oracle SQL Reference I might
be able to figure out a DB2 SQL equivalent, if there is one.

Rhino
Nov 12 '05 #3
The sql you provided works. But when I put it into the following sql
as a subquery, i alwasy got error message:

SQL0104N An unexpected token "AS" was found

Is there limitation for DB2 WITH...AS... clause?

SELECT
T.a,
T.b,
(WITH rec(c1, c2)
AS (SELECT c1, c2 FROM table WHERE c2 = T.a
UNION ALL
SELECT table.c1, table.c2 FROM table, rec WHERE table.c2 =
rec.c2)
SELECT c1, c2 FROM rec FETCH FIRST 1 ROWS ONLY
) as c
FROM T
thanks,
Michael
Serge Rielau <sr*****@ca.ibm.com> wrote in message news:<2t*************@uni-berlin.de>...
Here is the SQL Standard version (also supported by SQL Server 2005)
which works in DB2 for LUW
WITH rec(c1, c2)
AS (SELECT c1, c2 FROM table WHERE c2 IN ('1','2')
UNION ALL
SELECT table.c1, table.c2 FROM table, rec WHERE table.c2 = rec.c2)
SELECT c1, c2 FROM rec

Cheers
Serge

Nov 12 '05 #4
WITH in DB2 is only support at the top level of SELECT,
In the SELECT feeding an INSERT and in the RETURN statement of SQL
functions.

Often you can pull the WITH up to top level.
In your case we need a little trick though due to the correlation:

CREATE FUNCTION rec(a INT)
RETURNS TABLE(c1 INT, c2 INT)
RETURN
SELECT WITH rec(c1, c2)
AS (SELECT c1, c2 FROM table WHERE c2 = T.a
UNION ALL
SELECT table.c1, table.c2 FROM table, rec
WHERE table.c2 = rec.c2)
SELECT c1, c2 FROM rec;

T.a,
T.b,
(SELECT c1, c2 FROM TABLE(rec(T.a)) AS c FETCH FIRST ROW ONLY
) as c
FROM T;

Am I corrcet to assuem that this is a simplified example?
The FETCH FIRST seems rather bizarre semantically unless you presume
some sort of order (which DB2 does not!)

Cheers
Serge
Nov 12 '05 #5
Serge Rielau <sr*****@ca.ibm.com> wrote in message news:<2t*************@uni-berlin.de>...
WITH in DB2 is only support at the top level of SELECT,
In the SELECT feeding an INSERT and in the RETURN statement of SQL
functions.

Often you can pull the WITH up to top level.
In your case we need a little trick though due to the correlation:

CREATE FUNCTION rec(a INT)
RETURNS TABLE(c1 INT, c2 INT)
RETURN
SELECT WITH rec(c1, c2)
AS (SELECT c1, c2 FROM table WHERE c2 = T.a
UNION ALL
SELECT table.c1, table.c2 FROM table, rec
WHERE table.c2 = rec.c2)
SELECT c1, c2 FROM rec;

T.a,
T.b,
(SELECT c1, c2 FROM TABLE(rec(T.a)) AS c FETCH FIRST ROW ONLY
) as c
FROM T;

Am I corrcet to assuem that this is a simplified example?
The FETCH FIRST seems rather bizarre semantically unless you presume
some sort of order (which DB2 does not!)

Cheers
Serge


It's simplified example. You answer makes a lot of sense!
Thank you,
Michael
Nov 12 '05 #6

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

Similar topics

3
by: Thierry B. | last post by:
Hi, from SQL*Plus, i use: DESCRIBE MyTable and I get this result (example): Nom NULL ? Type ----------------------------------------- --------...
0
by: JONL | last post by:
Have been connecting to oracle using the following with success my $dbHandle = DBI->connect($sConnectString, $sUsername, $sPassword ); adding ,\%attr after password permits me to set the db handle...
4
by: susmita_ganguly | last post by:
Hi I am trying to upgrade from oracle 8i to oracle 9i on the same server ..I don't know much abt migration . Can anyone help me out. Thanks. Susmita
4
by: Joseph Gulla | last post by:
Hi all I'm trying to install the OEM for 9i. The install goes smoothly. I can sql connect to db with repository when I use Oracle enterprise manger consol login I can connect standalone. The...
4
by: Amardeep Verma | last post by:
Hi, I have a quick question. Which role/privileges are required before a user can give the statement "GRANT ALL PRIVILEGES"? Thanking you in Advance Have a nice day
4
by: Tig | last post by:
Hi all. I have a need to connect to an Oracle 7.3.3.5 database. I have a user who successfully connects to it with her Oracle 7.3 client. I have an Oracle 9.2 client installed on my machine....
4
by: thiamwah | last post by:
Hi, I have a SQL statement that access to one database (DB2) i.e. SELECT DB1.TABLE1.TABLE1_ID, DB1.TABLE1.FIELD_A, DB1.TABLE2.FIELD_1, DB1.TABLE2.FIELD_2,
5
by: Steven C | last post by:
Hello! I am trying to connect to a SQL Server (MSDE) database in mixed mode authentication, via C#, but when I use the MSDE instance name, I keep getting an "Unrecognized Escape Sequence"...
4
by: musai | last post by:
I have created vb oracle application I installed oracle client in three machine. All machine was reconfigured after oracle installed I could see table and record set through sql plus sheet in...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.