If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Creating a cursor in a script

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-22-09, 12:29
petergriffin petergriffin is offline
Registered User
 
Join Date: Jul 2009
Posts: 5
Creating a cursor in a script

I have particular records in my db that I need to cross-reference and the only way I can think of doing this is by using a cursor executing dynamic SQL.

I want to do this in a single script I can run but the problem I have is the only examples I can find are for creating procedures but I don't want to do this because this is just something I need to run on the client's database from time to time to diagnose problems. Any idea how I can do this without creating a procedure?
Reply With Quote
  #2 (permalink)  
Old 07-22-09, 12:47
rdutton rdutton is offline
Registered User
 
Join Date: Dec 2008
Posts: 76
why not just create the procedure, use it, then drop it?
__________________
RD
Reply With Quote
  #3 (permalink)  
Old 07-22-09, 12:53
petergriffin petergriffin is offline
Registered User
 
Join Date: Jul 2009
Posts: 5
Because the user would have to give "create" access which they don't want to do (they're a bank)
Reply With Quote
  #4 (permalink)  
Old 07-22-09, 15:55
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Could you explain what exactly the cross-referencing shall do? Do you want to change (insert/update/delete) some data or do you just want to retrieve a result set returning some differences or erroneous records?

You typically don't need a cursor for that - just use some regular SQL statements that express your conditions and then work with the result set in whichever way you want.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #5 (permalink)  
Old 07-22-09, 16:13
petergriffin petergriffin is offline
Registered User
 
Join Date: Jul 2009
Posts: 5
Basically the way our app has been designed is that rather than allowing the db to decide the next primary key for each table these are stored in a seperate table containing the table name and key (I know...not my idea but something I'm stuck with!! ).

Sometimes these get out of sync so when the application tries to insert a new record it does so with an invalid primary key. What I need to do is loop through each of the records on the table and check the actual primary key value (using dynamic sql) to see where the 2 are out of sync. Unfortunately I don't have the option of changing how the application does this but what I just need to run this on an adhoc basis to see where there might be some problems (hope this makes sense)
Reply With Quote
  #6 (permalink)  
Old 07-22-09, 16:46
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by petergriffin

Sometimes these get out of sync so when the application tries to insert a new record it does so with an invalid primary key.
Invalid here means a duplicate value, because the next value in the key lookup table is less than the maximum PK value in the data table?

Code:
select * from datatable where pk >  
(select nextpk from keylookup where tablename = 'DATATABLE')
?

Last edited by n_i; 07-22-09 at 16:49.
Reply With Quote
  #7 (permalink)  
Old 07-22-09, 17:29
petergriffin petergriffin is offline
Registered User
 
Join Date: Jul 2009
Posts: 5
Quote:
Originally Posted by n_i
Invalid here means a duplicate value, because the next value in the key lookup table is less than the maximum PK value in the data table?

Code:
select * from datatable where pk >  
(select nextpk from keylookup where tablename = 'DATATABLE')
?
That's exactly the problem, unfortunately this is something that's in the application code which I don't have access to, what I just need to do is to occasionally run a script which will identify those keys which have gotten out of sync so they can be repaired (either manually or as part of the script)
Reply With Quote
  #8 (permalink)  
Old 07-22-09, 17:55
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
So why not just execute this query as a SQL script?
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #9 (permalink)  
Old 07-23-09, 13:58
petergriffin petergriffin is offline
Registered User
 
Join Date: Jul 2009
Posts: 5
That's exactly what I want to do but I need to loop through each record in the PKs table to check what the corresponding PK on the datatable is (was planning on doing this using dynamic SQL). The problem I'm having is in creating the cursor to loop through each of these records - just neater than having to manually create each select manually as it makes sure I don't miss any.
Reply With Quote
  #10 (permalink)  
Old 07-23-09, 14:46
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
How'bout this:
Code:
db2 -x "select 
'select', ''''||tablename||'''', ', pk from', tablename, 
'where pk > (select nextpk from keylookup where tablename = ''' || tablename || ''')' 
from keylookup" | db2
Obiously, all this should be on one line - I split it up for better readability.
Reply With Quote
  #11 (permalink)  
Old 07-24-09, 03:45
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by petergriffin
That's exactly what I want to do but I need to loop through each record in the PKs table to check what the corresponding PK on the datatable is (was planning on doing this using dynamic SQL). The problem I'm having is in creating the cursor to loop through each of these records - just neater than having to manually create each select manually as it makes sure I don't miss any.
Why do you think you need to loop? Simply join the 2 tables and be done with it - exactly as n_i posted.

p.s: SQL was not designed as a procedural language (originally). So don't try to cram it into this corset. If you do things the SQL-way (set oriented), you are ofter much better off.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On