[comp.databases] SQL Question record by record processing

bm@bike2work.Eng.Sun.COM (Bill Michel) (10/26/90)

I want to be able to process each record in one of my tables, one by one.
Something similar, say, in a shell script, to setting a variable to the
contents of a directory, and then using *shift* to process each file
one by one.

Thanks in advance for any pointers you can give.

Bill
--
Bill Michel			
bm@eng.sun.com		These views are my own, not Sun's.

broy@vaxa.weeg.uiowa.edu (Barbara Roy) (10/26/90)

In article <1731@exodus.Eng.Sun.COM>, bm@bike2work.Eng.Sun.COM (Bill Michel) writes...
>I want to be able to process each record in one of my tables, one by one.
>Something similar, say, in a shell script, to setting a variable to the
>contents of a directory, and then using *shift* to process each file
>one by one.
> 
>Thanks in advance for any pointers you can give.
> 
>Bill
>--
>Bill Michel			
>bm@eng.sun.com		These views are my own, not Sun's.

You neglected to state what SQL package you are using. If you were using ORACLE
V6, PL/SQL cursors would do the trick for you. PL/SQL allows cursors to fetch
one record at a time and process then with loop statements and conditional
branching.

Barbara Roy, Weeg Computing Center, University of Iowa, Iowa City, IA. 52242
Phone: 319-335-5506  
Internet: broy@vaxa.weeg.uiowa.edu     Bitnet: broyva@uiamvs
Disclaimer: My opinions are solely my own and may change daily.

eric@wdl47.wdl.fac.com (Eric Kuhnen) (10/26/90)

bm@bike2work.Eng.Sun.COM (Bill Michel) writes:

>I want to be able to process each record in one of my tables, one by one.
>Something similar, say, in a shell script, to setting a variable to the
>contents of a directory, and then using *shift* to process each file
>one by one.

Define a cursor that selects all rows of the table you wish to loop through.
Use a fetch loop to get each row from cursor, one row at a time.  If you
have included the SQL Communications Area in your program, you could do
something like the following:

declare cursor fetch_em for 
select * from <table-name>

open cursor fetch_em

fetch * into <variable-list>

while (sqlca.sqlcode == 0)
{
  [various and sundry processing statements]
  fetch * into <variable-list>
}

close cursor fetch_em

The sqlca structure is fairly common across vendor implementations of SQL.
When sqlca.sqlcode < 0, an error has occurred in the preceeding SQL statement.
When sqlca.sqlcode > 0, the preceeding SQL statement completed execution but
some special condition came up; i.e. 100 = "no rows returned," etc.  A 0 
means that the preceeding SQL statement went off without a hitch.

"Q"