473,395 Members | 1,516 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,395 software developers and data experts.

Strange behavior of FOR LOOP CURSOR

Hi

DB Details
==========

Expand|Select|Wrap|Line Numbers
  1.  
  2. C:\Program Files (x86)\IBM\SQLLIB\BIN>db2level
  3. DB21085I  Instance "DB2" uses "32" bits and DB2 code release "SQL09073" with
  4. level identifier "08040107".
  5. Informational tokens are "DB2 v9.7.301.326", "s101006", "IP23213", and Fix Pack
  6. "3a".
  7. Product is installed at "C:\PROGRA~2\IBM\SQLLIB" with DB2 Copy Name "DB2COPY1".
  8.  
  9.  
Brief description about the problem:
=================================
Within a cursor for loop, I'm trying to DETACH and ADD a partition for the same range. Both the operations (detach,add) are happening as desired. But the issue here is, my CURSOR SELECT has only 1 record and is expected to come out of the LOOP iterating once. Strangely, I see the cursor iterating more than once becoming an infinite loop !!!


DB2 Experts-
Please let me know the reason why CURSOR is iterating more than once when there is only one record in it.

Any help is greatly apprecitaed.


Code:
=====
Expand|Select|Wrap|Line Numbers
  1.  
  2. drop table r_dummy@
  3.  
  4. create table r_dummy(a int,b date)
  5. partition by range(b)
  6. (
  7. starting from ('07/18/2013') ending at('07/18/2013'),
  8. starting from ('07/19/2013') ending at('07/19/2013'),
  9. starting from ('07/20/2013') ending at('07/20/2013')
  10. )
  11. @
  12.  
  13. BEGIN
  14.     DECLARE l_detach_table_main ANCHOR DATA TYPE TO syscat.tables.tabname;
  15.     DECLARE l_count INT DEFAULT 0;
  16.     DECLARE l_cur_cout INT DEFAULT 0;
  17.     DECLARE l_detach_stmt VARCHAR(1000);
  18.     DECLARE l_fn_status INT;
  19.     DECLARE l_sleep_ts TIMESTAMP;
  20.  
  21.     SELECT COUNT(*)
  22.     INTO l_cur_cout
  23.     FROM SYSCAT.DATAPARTITIONS
  24.     WHERE TABNAME='R_DUMMY'
  25.     AND TABSCHEMA= 'I508282' 
  26.     AND status <> 'L'
  27.     AND TO_DATE(highvalue,'YYYY-MM-DD') = '07/18/2013'
  28.     AND TO_DATE(lowvalue,'YYYY-MM-DD') = '07/18/2013'
  29.     ;
  30.  
  31.     CALL DBMS_OUTPUT.PUT_LINE('Cursor count : '||CHAR(l_cur_cout));
  32.  
  33.     cur_loop:
  34.     BEGIN
  35.         FOR i AS c1 CURSOR WITH HOLD FOR 
  36.         SELECT datapartitionname dpname,lowvalue lv,highvalue hv
  37.         FROM SYSCAT.DATAPARTITIONS
  38.         WHERE TABNAME='R_DUMMY'
  39.         AND TABSCHEMA= 'I508282' 
  40.         AND status <> 'L'
  41.         AND TO_DATE(highvalue,'YYYY-MM-DD') = '07/18/2013'
  42.         AND TO_DATE(lowvalue,'YYYY-MM-DD') = '07/18/2013'
  43.         DO
  44.             SET l_count=l_count + 1 ;
  45.             CALL DBMS_OUTPUT.PUT_LINE('Iteration count : '||CHAR(l_count));
  46.  
  47.             IF l_count>l_cur_cout THEN
  48.                 CALL DBMS_OUTPUT.PUT_LINE('!!!! Still iterating when there are no records left !!!!');
  49.                 LEAVE cur_loop;
  50.             END IF;
  51.  
  52.             SET l_detach_table_main = 'DETACH_'||dpname||'_'||'R_DUMMY'||REPLACE(TO_CHAR(CURRENT_TIMESTAMP,'MM-DD-YY-HH24-MI-SS-AM-FF6'),'-','_');
  53.             SET l_detach_stmt='ALTER TABLE'||' '||'I508282'||'.'||'R_DUMMY'||' '||'DETACH PARTITION'||' '||dpname||' '||'INTO'||' '||'I508282'||'.'||l_detach_table_main;
  54.             EXECUTE IMMEDIATE l_detach_stmt;
  55.             COMMIT;
  56.             CALL DBMS_OUTPUT.PUT_LINE('Table  '||l_detach_table_main||' created here');
  57.  
  58.             BEGIN
  59.                 DECLARE l_add_partition_str VARCHAR(4000);
  60.                 DECLARE l_start_date DATE;
  61.                 DECLARE l_end_date DATE;
  62.  
  63.                 SET l_start_date=TO_DATE(lv,'YYYY-MM-DD');
  64.                 SET l_end_date=TO_DATE(hv,'YYYY-MM-DD');
  65.  
  66.                 SET l_add_partition_str='ALTER TABLE '||'I508282'
  67.                 ||'.'
  68.                 ||'R_DUMMY'
  69.                 ||' ADD PARTITION  STARTING FROM '
  70.                 ||''''
  71.                 ||VARCHAR_FORMAT(l_start_date,'MM/DD/YYYY')
  72.                 ||''''
  73.                 ||' INCLUSIVE ENDING AT '
  74.                 ||''''
  75.                 ||VARCHAR_FORMAT(l_end_date,'MM/DD/YYYY')
  76.                 ||''''
  77.                 ||' INCLUSIVE ';
  78.  
  79.                 CALL DBMS_OUTPUT.PUT_LINE('l_add_partition_str : '||l_add_partition_str);
  80.                 EXECUTE IMMEDIATE l_add_partition_str;
  81.                 COMMIT;
  82.             END;
  83.             CALL DBMS_OUTPUT.PUT_LINE('First iteration completed');
  84.         END FOR;
  85.     END;
  86.  
  87.     CALL DBMS_OUTPUT.PUT_LINE('Drop detached table : '||l_detach_table_main||' manually');
  88.     COMMIT;
  89. END @
  90.  
  91.  
Output : Below output witness wierd behavior of CURSOR
======

Expand|Select|Wrap|Line Numbers
  1.  
  2. C:\Program Files (x86)\IBM\SQLLIB\BIN>db2 -td@ -sf "C:\Users\I508282\Desktop\post_a_qn.sql"
  3. DB20000I  The SQL command completed successfully.
  4.  
  5. DB20000I  The SQL command completed successfully.
  6.  
  7. DB20000I  The SQL command completed successfully.
  8.  
  9. Cursor count : 1
  10. Iteration count : 1
  11. Table  DETACH_PART0_R_DUMMY07_18_13_13_25_21_PM_821912 created here
  12. l_add_partition_str : ALTER TABLE I508282.R_DUMMY ADD PARTITION  STARTING FROM '07/18/2013' INCLUSIVE ENDING AT '07/18/2013' INCLUSIVE
  13. First iteration completed
  14. Iteration count : 2
  15. !!!! Still iterating when there are no records left !!!!
  16. Drop detached table : DETACH_PART0_R_DUMMY07_18_13_13_25_21_PM_821912 manually
  17.  
  18.  

Thanks
Raghu
Jul 18 '13 #1
0 1355

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Marcus | last post by:
Hello, I recently converted all my existing MyISAM tables to InnoDB tables through phpmyadmin. I noticed some strange behavior whenever I would refresh the screen, as phpmyadmin would report...
1
by: Dan Stromberg | last post by:
The below small program is giving strange behavior. At the bottom of the code, please find "this works" and "this doesn't work" comments. Why does one work and the other not? TIA. ...
1
by: Mei | last post by:
Hi, I'm running ASP under IIS 6 with Tomcat. During the process, Tomcat will forward to an ASP page with some data and those data will be written to MS SQL and display some information in that...
11
by: Marlene Stebbins | last post by:
Something very strange is going on here. I don't know if it's a C problem or an implementation problem. The program reads data from a file and loads it into two arrays. When xy, x, y, *xlist and...
18
by: genc ymeri | last post by:
Hi, I just noticed that while trying to run this code : MessageBox.Show("Hello"); the message box pops up but shows no string/message in it. (expecting the "Hello" string). Even the "OK"...
2
by: Maileen | last post by:
Hi, I have some strange behavior in my application. Let's say I have 2 buttons and 1 Textbox. 1. when i click on button1, it writes "hello world" in my textbox. i see it. 2. after that, i...
1
by: Alexander Inochkin | last post by:
Hi! I found same strange behavior of ASP.NET. It is possible this is the bug. Follow the steps:
0
by: ivb | last post by:
Hi all, I am using DB2 8.1.11.1 on NT with ASP.NET 1.1 When application make connection to database (via ADO.NET), it set "Connection timeout" parameter to 30 seconds. After, when my webpage...
10
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
0
by: Antonio Paglia | last post by:
Hi guys, I saw an strange behavior with some printers of series HP Laserjet 1000. I have installed several printers on my PC, some of them are : HP LaserJet 1020 e 1010. I'm using...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.