简体   繁体   English

DB2(找到光标)无效关键字“找到”

[英]DB2 (Cursor IS Found) INVALID KEYWORD "FOUND"

I'm having a serious problem with DB2 Cursor.我遇到了 DB2 Cursor 的严重问题。 When I'm opening it I cant know is there any records in it or not?当我打开它时,我不知道里面是否有任何记录?

And also I'm writing my stored procedure in IBM® Data Studio Version 4.1.0.0我还在 IBM® Data Studio Version 4.1.0.0 中编写我的存储过程

You must run the FETCH statement to check, if there are records in the result set.您必须运行FETCH语句来检查结果集中是否有记录。 But you can't do it since you return the result to a caller.但是你不能这样做,因为你将结果返回给调用者。

So, the 1-st solution would be to use, let's say, (Declared | Created) Global Temporary Table to INSERT SELECT into it with checking, if any rows were inserted (using GET DIAGNOSTICS statement), and then open either cursor1 on this (C|D)GTT, or cursor2.因此,第一个解决方案是使用(声明 | 创建)全局临时表来INSERT SELECT并检查是否插入了任何行(使用GET DIAGNOSTICS语句),然后在此打开 cursor1 (C|D)GTT 或 cursor2。

The 2-nd solution would be to combine both select statements into a single one like below:第二种解决方案是将两个 select 语句组合成一个,如下所示:

SELECT T.*
FROM
(
  SELECT 
    RANK() OVER (ORDER BY NUMBER) RN
  , T.*
  FROM
  (
    SELECT 1 AS NUMBER, T1.*
    FROM SYSIBM.SYSDUMMY1 T1
    --WHERE 1=0
      UNION ALL
    SELECT 2 AS NUMBER, T2.*
    FROM SYSIBM.SYSDUMMY1 T2
  ) T
) T
WHERE RN=1;

The idea is to UNION both result sets, rank them by their constant column value ( NUMBER in this case), and return the result subset with the smallest rank.这个想法是UNION两个结果集,按它们的常量列值(在本例中为NUMBER对它们进行排名,并返回具有最小排名的结果子集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM