动态声明游标

发布于 2024-12-25 04:04:13 字数 363 浏览 0 评论 0原文

我需要根据我所处的情况在存储过程中动态创建游标,理想情况如下:

IF (varOrgGroupCode = '') THEN
BEGIN
DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS`;
END;
ELSE
BEGIN
DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS` WHERE `groupcode` = varOrgGroupCode;  
END;
END IF;

上面的代码将导致语法错误。我用谷歌搜索过&有人通过使用临时表来做到这一点。谁能告诉我怎么办?

I need to create Cursors dynamically in stored procedure based on what situation i'm in, the ideal is below:

IF (varOrgGroupCode = '') THEN
BEGIN
DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS`;
END;
ELSE
BEGIN
DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS` WHERE `groupcode` = varOrgGroupCode;  
END;
END IF;

The code above will cause an syntax error. I've googled & someone did it by using temp table. Can anyone tell me how please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瞎闹 2025-01-01 04:04:13

您在 Google 上找到的内容可能是创建一个临时表,根据您的谓词填充该临时表,然后在游标声明中引用该临时表。

像这样的事情:

DELIMITER $

CREATE PROCEDURE `some_procedure`(IN varOrgGroupCode varchar(100))
BEGIN

DECLARE bNoMoreRows BOOLEAN DEFAULT FALSE;
DECLARE vGroupCode varchar(100);

DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS_TEMP`;

declare continue handler for not found set bNoMoreRows := true; 

BEGIN

  drop table if exists LICENSEKEYS_TEMP;

  create temporary table `LICENSEKEYS_TEMP` (groupCode varchar(100));

  IF (varOrgGroupCode = '') THEN
    insert into `LICENSEKEYS_TEMP` (groupCode) SELECT `groupcode` FROM `LICENSEKEYS`;
  ELSE
    insert into `LICENSEKEYS_TEMP` (groupCode) SELECT `groupcode` FROM `LICENSEKEYS` WHERE `groupcode` = varOrgGroupCode;  
  END IF;

  open groupCode;

  GROUPCODE_LOOP: loop

   fetch groupCode into vGroupCode;

   -- Do some stuff

   if bNoMoreRows then
      close groupCode;
      leave GROUPCODE_LOOP;
   end if;

  END LOOP GROUPCODE_LOOP;

 END;

END$

DELIMITER ;

Probably the thing you found on Google was to create a temp table, populate that temp table based on your predicate and then reference the temp table in your cursor declaration.

Something like this:

DELIMITER $

CREATE PROCEDURE `some_procedure`(IN varOrgGroupCode varchar(100))
BEGIN

DECLARE bNoMoreRows BOOLEAN DEFAULT FALSE;
DECLARE vGroupCode varchar(100);

DECLARE groupCode CURSOR FOR
SELECT `groupcode` FROM `LICENSEKEYS_TEMP`;

declare continue handler for not found set bNoMoreRows := true; 

BEGIN

  drop table if exists LICENSEKEYS_TEMP;

  create temporary table `LICENSEKEYS_TEMP` (groupCode varchar(100));

  IF (varOrgGroupCode = '') THEN
    insert into `LICENSEKEYS_TEMP` (groupCode) SELECT `groupcode` FROM `LICENSEKEYS`;
  ELSE
    insert into `LICENSEKEYS_TEMP` (groupCode) SELECT `groupcode` FROM `LICENSEKEYS` WHERE `groupcode` = varOrgGroupCode;  
  END IF;

  open groupCode;

  GROUPCODE_LOOP: loop

   fetch groupCode into vGroupCode;

   -- Do some stuff

   if bNoMoreRows then
      close groupCode;
      leave GROUPCODE_LOOP;
   end if;

  END LOOP GROUPCODE_LOOP;

 END;

END$

DELIMITER ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文