473,657 Members | 2,507 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Probably dumb question

I have a (somewhat complicated) query that returns the rows from a table
and its self join in the correct order. I want to call a procedure on
each of the returned rows, in the order returned. Is there any to do
this in straight sql or must I write an sql procedure with a cursor on
the select and issue the call while accessing rows through the cursor?

What I'm trying to do (as those of you that follow comp.databases. theory
will remember) is to create (and maintain) a sorted tree from an
unsorted one.

What I'd like (I'm making this syntax up from wishful thinking) is
something like:

for each row in (select ...) call add_location2(s ubx.entity_id,
subx.loc_id, subx.namex, supx.loc_id)

query follows (after Joe Celko in _Trees and Hierarchies in SQL for
Smarties_, p51):

select subx.entity_id, subx.loc_id, subx.namex, supx.loc_id
from is3.locations as supx,
is3.locations as subx
where subx.leftx between supx.leftx and supx.rightx
and supx.loc_id<>su bx.loc_id
and not exists
(select *
from is3.locations as midx
where midx.leftx between supx.leftx and supx.rightx
and subx.leftx between midx.leftx and midx.rightx
and midx.loc_id not in (supx.loc_id, subx.loc_id))
order by subx.leftx

The procedure (ibid, pp 78-79) modified to create a sorted (by namex) tree:

create procedure add_location2(i n ent integer,
in new_id integer,
in new_name varchar(50),
in under integer)
language sql
modifies sql data

begin
declare under_lft integer;
declare under_rgt integer;
declare parent_level smallint;
declare sib_lft integer;
declare sib_rgt integer;
declare my_lft integer;
declare my_rgt integer;

set schema nullid;

select leftx, rightx, levelx into under_lft, under_rgt,
parent_level
from locations2
where loc_id = under;
and leftx between under_lft and under_rgt
and namex<new_name
order by namex desc
fetch first 1 row only;

if(sib_rgt is null) then begin
set my_lft = under_lft+1;
set my_rgt = under_lft+2;
end;
else begin
set my_lft = sib_rgt+1;
set my_rgt = sib_rgt+2;
end;
end if;

update locations2
set leftx = case
when leftx >= my_rgt-1 then leftx+2
else leftx
end,
rightx = case
when rightx >= my_rgt-1 then rightx+2
else rightx
end;

insert into locations2 values(new_id,n ew_name,my_lft, my_rgt,ent,
null, parent_level+1) ;

end
Nov 12 '05 #1
9 1345
Bob Stearns wrote:
I have a (somewhat complicated) query that returns the rows from a table
and its self join in the correct order. I want to call a procedure on
each of the returned rows, in the order returned. Is there any to do
this in straight sql or must I write an sql procedure with a cursor on
the select and issue the call while accessing rows through the cursor?

What I'm trying to do (as those of you that follow comp.databases. theory
will remember) is to create (and maintain) a sorted tree from an
unsorted one.

What I'd like (I'm making this syntax up from wishful thinking) is
something like:

for each row in (select ...) call add_location2(s ubx.entity_id,
subx.loc_id, subx.namex, supx.loc_id)

query follows (after Joe Celko in _Trees and Hierarchies in SQL for
Smarties_, p51):

select subx.entity_id, subx.loc_id, subx.namex, supx.loc_id
from is3.locations as supx,
is3.locations as subx
where subx.leftx between supx.leftx and supx.rightx
and supx.loc_id<>su bx.loc_id
and not exists
(select *
from is3.locations as midx
where midx.leftx between supx.leftx and supx.rightx
and subx.leftx between midx.leftx and midx.rightx
and midx.loc_id not in (supx.loc_id, subx.loc_id))
order by subx.leftx

The procedure (ibid, pp 78-79) modified to create a sorted (by namex) tree:

create procedure add_location2(i n ent integer,
in new_id integer,
in new_name varchar(50),
in under integer)
language sql
modifies sql data

begin
declare under_lft integer;
declare under_rgt integer;
declare parent_level smallint;
declare sib_lft integer;
declare sib_rgt integer;
declare my_lft integer;
declare my_rgt integer;

set schema nullid;

select leftx, rightx, levelx into under_lft,
under_rgt, parent_level
from locations2
where loc_id = under;
and leftx between under_lft and under_rgt
and namex<new_name
order by namex desc
fetch first 1 row only;

if(sib_rgt is null) then begin
set my_lft = under_lft+1;
set my_rgt = under_lft+2;
end;
else begin
set my_lft = sib_rgt+1;
set my_rgt = sib_rgt+2;
end;
end if;

update locations2
set leftx = case
when leftx >= my_rgt-1 then leftx+2
else leftx
end,
rightx = case
when rightx >= my_rgt-1 then rightx+2
else rightx
end;

insert into locations2 values(new_id,n ew_name,my_lft, my_rgt,ent,
null, parent_level+1) ;

end

You could use an dynmaoci compound statement (BEGIN ATOMIC ... END) with
a FOR and a CALL in DB2 for LUW V8.2.
In DB2 V8.1.4 and higher you could also use a correlated TABLE function
if you want inlining.
Heck you could even make that a correlated table function feeding the
FOR loop as the outer of the join. But that would be mean since you have
to rely on DB2's optimizer not kicking out the nested ORDER BY.

Cheres
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Serge Rielau wrote:
Bob Stearns wrote:
I have a (somewhat complicated) query that returns the rows from a
table and its self join in the correct order. I want to call a
procedure on each of the returned rows, in the order returned. Is
there any to do this in straight sql or must I write an sql procedure
with a cursor on the select and issue the call while accessing rows
through the cursor?

What I'm trying to do (as those of you that follow
comp.databases. theory will remember) is to create (and maintain) a
sorted tree from an unsorted one.

What I'd like (I'm making this syntax up from wishful thinking) is
something like:

for each row in (select ...) call add_location2(s ubx.entity_id,
subx.loc_id, subx.namex, supx.loc_id)

query follows (after Joe Celko in _Trees and Hierarchies in SQL for
Smarties_, p51):

select subx.entity_id, subx.loc_id, subx.namex, supx.loc_id
from is3.locations as supx,
is3.locations as subx
where subx.leftx between supx.leftx and supx.rightx
and supx.loc_id<>su bx.loc_id
and not exists
(select *
from is3.locations as midx
where midx.leftx between supx.leftx and supx.rightx
and subx.leftx between midx.leftx and midx.rightx
and midx.loc_id not in (supx.loc_id, subx.loc_id))
order by subx.leftx

The procedure (ibid, pp 78-79) modified to create a sorted (by namex)
tree:

create procedure add_location2(i n ent integer,
in new_id integer,
in new_name varchar(50),
in under integer)
language sql
modifies sql data

begin
declare under_lft integer;
declare under_rgt integer;
declare parent_level smallint;
declare sib_lft integer;
declare sib_rgt integer;
declare my_lft integer;
declare my_rgt integer;

set schema nullid;

select leftx, rightx, levelx into under_lft,
under_rgt, parent_level
from locations2
where loc_id = under;
and leftx between under_lft and under_rgt
and namex<new_name
order by namex desc
fetch first 1 row only;

if(sib_rgt is null) then begin
set my_lft = under_lft+1;
set my_rgt = under_lft+2;
end;
else begin
set my_lft = sib_rgt+1;
set my_rgt = sib_rgt+2;
end;
end if;

update locations2
set leftx = case
when leftx >= my_rgt-1 then leftx+2
else leftx
end,
rightx = case
when rightx >= my_rgt-1 then rightx+2
else rightx
end;

insert into locations2 values(new_id,n ew_name,my_lft, my_rgt,ent,
null, parent_level+1) ;

end


You could use an dynmaoci compound statement (BEGIN ATOMIC ... END) with
a FOR and a CALL in DB2 for LUW V8.2.
In DB2 V8.1.4 and higher you could also use a correlated TABLE function
if you want inlining.
Heck you could even make that a correlated table function feeding the
FOR loop as the outer of the join. But that would be mean since you have
to rely on DB2's optimizer not kicking out the nested ORDER BY.

Cheres
Serge

I need a little better pointer to "correlated TABLE function" (I'm
currently running 8.1.5). I couldn't find a useful reference at the
Information center using these keywords an I am sufficiently ignorant of
the subject that no further keywords occurred to me. Or a sample
skeleton example of the statement you are thing of would do. I can (I
think) fill in the blanks.
Nov 12 '05 #3
Bob Stearns wrote:
I need a little better pointer to "correlated TABLE function" (I'm
currently running 8.1.5). I couldn't find a useful reference at the
Information center using these keywords an I am sufficiently ignorant of
the subject that no further keywords occurred to me. Or a sample
skeleton example of the statement you are thing of would do. I can (I
think) fill in the blanks.

Here you go. Check e.g. listing 4 and 5:
http://www-128.ibm.com/developerwork...dm-0411rielau/
FP5 will do the job.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
Serge Rielau wrote:
Bob Stearns wrote:
I need a little better pointer to "correlated TABLE function" (I'm
currently running 8.1.5). I couldn't find a useful reference at the
Information center using these keywords an I am sufficiently ignorant
of the subject that no further keywords occurred to me. Or a sample
skeleton example of the statement you are thing of would do. I can (I
think) fill in the blanks.


Here you go. Check e.g. listing 4 and 5:
http://www-128.ibm.com/developerwork...dm-0411rielau/

FP5 will do the job.

Cheers
Serge

Thank you. I have my homework for the {day, week, month, year}. That is
some powerful sql code. Out of curiosity, how long did you work on these
codes to get the elegant results you did? In other words, is there any
hope for a beginner to learn to do this level of work in 5 years?
Nov 12 '05 #5
Bob Stearns wrote:
Serge Rielau wrote:
Bob Stearns wrote:
I need a little better pointer to "correlated TABLE function" (I'm
currently running 8.1.5). I couldn't find a useful reference at the
Information center using these keywords an I am sufficiently ignorant
of the subject that no further keywords occurred to me. Or a sample
skeleton example of the statement you are thing of would do. I can (I
think) fill in the blanks.

Here you go. Check e.g. listing 4 and 5:
http://www-128.ibm.com/developerwork...dm-0411rielau/

FP5 will do the job.

Cheers
Serge


Thank you. I have my homework for the {day, week, month, year}. That is
some powerful sql code. Out of curiosity, how long did you work on these
codes to get the elegant results you did? In other words, is there any
hope for a beginner to learn to do this level of work in 5 years?

Sure it can be doen in 5 years, but keep in mind that I did "nothing
but". The majority of my job has been to undertand SQL, implement it
into DB2 and devise new language. I'd guess it took 3-4 years to become
proficient. The rest is begging for resources. There are always more
ideas than there are developers to execute ;-)
By comparison I'm clueless about administrating DB2 or Java.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #6
Serge Rielau wrote:
Bob Stearns wrote:
I need a little better pointer to "correlated TABLE function" (I'm
currently running 8.1.5). I couldn't find a useful reference at the
Information center using these keywords an I am sufficiently ignorant
of the subject that no further keywords occurred to me. Or a sample
skeleton example of the statement you are thing of would do. I can (I
think) fill in the blanks.


Here you go. Check e.g. listing 4 and 5:
http://www-128.ibm.com/developerwork...dm-0411rielau/

FP5 will do the job.

Cheers
Serge

If I've understood the example (only possible, not probable, given that
I've only studied it and the relevant parts of the SQL Reference for 4
hours), I would have to change the function add_location2 to a table
function in order to use it in a construct similar to your listing 4. Is
that correct?

Another question: your sql statement apparently depends on the select
for itemlist being done "before" the new_ol_local function being called.
Is forced by the order of the phrases or is the optimizer smart enough
to see the dependency?
Nov 12 '05 #7
Bob Stearns wrote:
Another question: your sql statement apparently depends on the select
for itemlist being done "before" the new_ol_local function being called.
Is forced by the order of the phrases or is the optimizer smart enough
to see the dependency?

A table function which MODFIES SQL DATA must be correlated to all the
join parters. This forces it to be the last (inner most) to start executing.
IFF there is a read/wrote conflict the optimizer will detect it and
ensure that the outer of the join have complete before the table
function is allowed to start.
To get good performance any sort of conflicts should be avoided.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #8
>> I want to call a procedure on each of the returned rows, in the order returned. <<

SQL is a set-oriented language!!! Let's get back to the basics of an
RDBMS. Rows are not records; fields are not columns; tables are not
files; there is no sequential access or ordering in an RDBMS, so
"first", "next" and "last" are totally meaningless.

We need more info ..

Nov 12 '05 #9
--CELKO-- wrote:
I want to call a procedure on each of the returned rows, in the order returned. <<

SQL is a set-oriented language!!! Let's get back to the basics of an
RDBMS. Rows are not records; fields are not columns; tables are not
files; there is no sequential access or ordering in an RDBMS, so
"first", "next" and "last" are totally meaningless.

We need more info ..

Look back at my message of 10/05/2005, 11:41PM, which started this
thread. It gives the complete sql, function, and reasoning of why I
think I have to process the given rows in a given order.
Nov 12 '05 #10

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

Similar topics

16
2505
by: squash | last post by:
a dumb question i had on my mind: Say you have a dynamically created web page . Isn't it more secure to write it in php since a visitor will not be able to tell it is dynamically created? But if you write it in perl, you have to put it in your cgi-bin which automatically means it's a dynamically generated page? security by obscurity . Is this true or hardly means anything? thx!
15
2671
by: Good Man | last post by:
Hey there I have a dumb question.... Let's say i have a database full of 4000 people.... I select everything from the database by: $result = mysql_query("SELECT * FROM People");
2
2269
by: InvisibleMan | last post by:
Hi, I feel a little dumb for asking this (considering im writing TSQL) but there doesn't seem to be any definitive answers on the search engines... Okay I understand that if you open the ADO connection that you close it with: adoCon.Close Set adoCon = Nothing
2
1297
by: TGF | last post by:
How do you copy a String type into a native char buffer? Dumb question, but not sure how to do it :( TGF
5
1545
by: Need Help | last post by:
This might be an extremely dumb question; I'm new to Visual C++, and am trying to use the simple Spooler API, OpenPrinter, but when I try to compile my source file, it says OpenPrinter is an undeclared identifier. Here's how I'm trying to use it HANDLE newPrinter = NULL BOOL op = OpenPrinter(ps09, &newPrinter, NULL) Now, as far as I'm concerned there should be nothing wrong with that code, but does anyone else know what might be the...
10
2291
by: Edward | last post by:
I've just taken over maintaining a system from a colleague who has left. I find the following line in her code: Dim params(2) As SqlClient.SqlParameter params(0) = New SqlClient.SqlParameter("@UserName", pvstrUsername) params(1) = New SqlClient.SqlParameter("@Password", pvstrPassword) params(2) = New SqlClient.SqlParameter("@MachineName", Environment.MachineName())
2
1108
by: Ed | last post by:
I'm rather new to ASP .Net, so I appologize in advance if I'm asking a dumb question here. I've been searching all over the place for an answer and haven't found a single one. I'm working on a form that gives the user a file control to upload a file from their machine to my server. With this file control are two buttons; one marked "Upload", the other marked "Cancel". The cancel button is to give them the option of just backing out...
2
2221
by: Bill Nguyen | last post by:
I would like to add a new VB.NET project using the same folder being used by another project so that I can share several forms already creaded by the other project. However, .NET created a new folder using whatever project name I supplied. Is there a work around for this? Thanks Bill
6
282
by: Robert Dufour | last post by:
What is the meaning of the word marshal and unmarshal in plain english as applied to an exe file? Does it mean the application has started and ended? Thanks for any help, Happy new year, Bob
0
8425
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8326
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8743
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8522
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7355
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4333
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2745
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1736
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.