home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / database / informix / 2714 < prev    next >
Encoding:
Text File  |  1992-12-15  |  4.5 KB  |  139 lines

  1. Newsgroups: comp.databases.informix
  2. Path: sparky!uunet!cs.utexas.edu!asuvax!ennews!anasaz!briand
  3. From: briand@anasazi.com (Brian Douglass)
  4. Subject: Re: ESQL C problem with sqlerrd[2]
  5. Organization: Anasazi Inc Phx Az USA
  6. Date: Tue, 15 Dec 1992 18:26:13 GMT
  7. Message-ID: <1992Dec15.182613.23081@anasazi.com>
  8. References: <1giuitINNcgq@emory.mathcs.emory.edu> <1992Dec14.230122.22841@unlv.edu>
  9. Sender: usenet@anasazi.com (Usenet News)
  10. Lines: 127
  11.  
  12. In article <1992Dec14.230122.22841@unlv.edu> ray@unlv.edu (Ray Tripamer) writes:
  13. >In article <1giuitINNcgq@emory.mathcs.emory.edu> kristen.m.altman@ccd.harris.com (Kristen Altman) writes:
  14. >>In my application, I am using ESQL C 4.1 and formatting dynamic SQL stmts.
  15. >>
  16. >>After the first fetch the number of rows returned in sqlerrd[2]
  17. >>is 0, but the sqlca.sqlcode = 0 also.  If I understand this correctly,
  18. >>the sqlerrd[2] says there are no rows, but the sqlcode says there are rows.  
  19. >>-- 
  20. >>Kristen Altman
  21. >>kma@ccd.harris.com
  22. >
  23. >The sqlca.sqlerrd[2] value DOES NOT hold the number of rows found by opening
  24. >a cursor for a select statement (dynamic or otherwise).  The sqlerrd[2] value
  25. >is filled in only by the update and delete statements (maybe insert too), but
  26. >not by a select.
  27. >
  28. >The only way to get the number of rows from a select statment is to
  29. >execute some type of "select count(*) into $cnt ..." statement with the same
  30. >from and where clause as the original select statement.
  31.  
  32. Hello Ray, long time no hear.
  33.  
  34. Sorry to disagree with you, but Kristen is sort of right.  By definition,
  35. sqlerrd[2] is set with the number of rows processed AFTER an SQL statement
  36. has executed.  This includes the UPDATE, DELETE, INSERT, and SELECT
  37. statements.  The difference with SELECT lies in the use of a cursor.  The
  38. SELECT is not complete until the last row is processed and you get sqlcode=100
  39. (I'm talking strictly successful operations).  Therefore, you should only
  40. expect sqlerrd[2] to contain a value other than 0 after the last FETCH.
  41.  
  42. This was strictly true in versions 1.X and 2.X.  However, in ONLINE, things 
  43. changed.  If the number of rows in the table is small, as well as the table 
  44. size, ONLINE may fill sqlerrd[2] after the first FETCH.  This is because 
  45. ONLINE has completed the SELECT, and the results are just waiting in a buffer 
  46. to be FETCHed.  The following is a test program to demonstrate this:
  47.  
  48. create table x1 (
  49.     x0    char(20),
  50.     x2    integer
  51. );
  52.  
  53. And the program:
  54.  
  55. main()
  56. {
  57. $ char x1[21];
  58. $ long x2;
  59.  
  60. $    database briand1;
  61.  
  62. $    declare c1 cursor for select * from x1;
  63.     printf("2 sqlca.sqlcode=%ld, 2=%ld\n", sqlca.sqlcode, sqlca.sqlerrd[2]);
  64.  
  65. $    open c1;
  66.     printf("3 sqlca.sqlcode=%ld, 2=%ld\n", sqlca.sqlcode, sqlca.sqlerrd[2]);
  67.  
  68.     while (1)
  69.     {
  70. $        fetch c1 into $x1, $x2;
  71.         if(sqlca.sqlcode !=0)
  72.             break;
  73.         printf("4 sqlca.sqlcode=%ld, 2=%ld\n", sqlca.sqlcode, sqlca.sqlerrd[2]);
  74.         printf("x1=%s, x2=%ld\n", x1, x2);
  75.     }
  76.     printf("5 sqlca.sqlcode=%ld, 2=%ld\n", sqlca.sqlcode, sqlca.sqlerrd[2]);
  77. }
  78.  
  79. With just 3 records in the table, I get the following output:
  80. 1 sqlca.sqlcode=0, 2=0
  81. 2 sqlca.sqlcode=0, 2=0
  82. 3 sqlca.sqlcode=0, 2=0
  83. 4 sqlca.sqlcode=0, 2=3
  84. x1=test 1              , x2=1
  85. 4 sqlca.sqlcode=0, 2=3
  86. x1=test 2              , x2=2
  87. 4 sqlca.sqlcode=0, 2=3
  88. x1=test 3              , x2=3
  89. 5 sqlca.sqlcode=100, 2=3
  90.  
  91. Notice that 4 is the first print after the FETCH, and sqlerrd[2] is already
  92. set to 3.  Though the cursor is still open, the ONLINE engine has completed
  93. its work and left the records in the buffer.
  94.  
  95. Now, with 500 records in the table, running the same program I get this:
  96.  
  97. 1 sqlca.sqlcode=0, 2=0
  98. 2 sqlca.sqlcode=0, 2=0
  99. 3 sqlca.sqlcode=0, 2=0
  100. 4 sqlca.sqlcode=0, 2=0
  101. x1=test 70             , x2=70
  102. 4 sqlca.sqlcode=0, 2=0
  103. ** Notice that sqlerrd[2] is 0 on the first FETCH
  104. x1=test 71             , x2=71
  105. 4 sqlca.sqlcode=0, 2=0
  106. x1=test 72             , x2=72
  107. .
  108. .
  109. .
  110. x1=test 424            , x2=424
  111. 4 sqlca.sqlcode=0, 2=0
  112. x1=test 425            , x2=425
  113. *** With approximately 74 rows to go, the engine completes, and sqlerrd[2] is 
  114. *** set with the final value
  115. 4 sqlca.sqlcode=0, 2=500
  116. x1=test 426            , x2=426
  117. 4 sqlca.sqlcode=0, 2=500
  118. x1=test 427            , x2=427
  119. .
  120. .
  121. .
  122. 4 sqlca.sqlcode=0, 2=500
  123. x1=test 499            , x2=499
  124. 4 sqlca.sqlcode=0, 2=500
  125. x1=test 500            , x2=500
  126. 5 sqlca.sqlcode=100, 2=500
  127.  
  128. So, sqlerrd[2] IS set by a SELECT statement, but not until the engine
  129. completes its work.  In V1.X and V2.X this is on the last row, in ONLINE it
  130. will happen prior to the last row, but there is no way to estimate exactly
  131. when this will be.  The only gauranteed time it will be set is upon
  132. completion.
  133.  
  134. >--
  135. >Ray Tripamer
  136. >ray@asci.com
  137.  
  138.  
  139.