1

谁能帮我解释为什么我不能在存储过程上创建临时表。

这是我的存储过程脚本

DELIMITER $$

DROP PROCEDURE IF EXISTS getProductionItem$$
CREATE DEFINER=pvtuser@`%` PROCEDURE getProductionItem(projectID BIGINT(20))


BEGIN

  CREATE TEMPORARY TABLE tempProdItem(
    prodType SET('JOB','BATCH','DOCGROUP'),
    taskQueueId BIGINT(20),
    jobBatchDocId BIGINT(20),
    jobBatchDocName VARCHAR(255),
    lockStatus SET('LOCKED','OPEN')
  );


  DECLARE done INT DEFAULT 0;


  DECLARE prodType VARCHAR(10);
  DECLARE taskQueueId BIGINT(20);
  DECLARE jobBatchDocId BIGINT(20);
  DECLARE jobBatchDocName VARCHAR(255);
  DECLARE lockStatus VARCHAR(10);


  DECLARE docCursor CURSOR
      FOR SELECT 'DOCGROUP',
                 jq.taskqueueid,
                 jq.documentgroupid,
                 dgd.documentGroupName,
                 COALESCE(jql.lockStatus,'OPEN')
            FROM jobdetails jd
           INNER JOIN jobqueue jq
              ON jd.jobid=jq.jobid
           INNER JOIN documentgroupdetails dgd
              ON jq.documentgroupid=dgd.documentgroupid
            LEFT JOIN jobqueuelocked jql
              ON jq.taskqueueid=jql.taskqueueid
           GROUP BY jq.taskqueueid;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

  OPEN docCursor;

  Loop1: LOOP
    FETCH docCursor INTO prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus;
    IF done=1 THEN
      LEAVE Loop1;
    ELSE
       /* TODO INSERT HERE IN TEMP TABLE */
    END IF;

  END LOOP;

  CLOSE docCursor;

  SELECT * FROM tempProdItem;

END

$$
DELIMITER;
4

1 回答 1

1
DELIMITER $$

DROP PROCEDURE IF EXISTS getProductionItem$$
CREATE DEFINER=pvtuser@`%` PROCEDURE getProductionItem(projectID BIGINT(20))

BEGIN

  DECLARE done INT DEFAULT 0;
  DECLARE prodType VARCHAR(10);
  DECLARE taskQueueId BIGINT(20);
  DECLARE jobBatchDocId BIGINT(20);
  DECLARE jobBatchDocName VARCHAR(255);
  DECLARE lockStatus VARCHAR(10);

  DECLARE docCursor CURSOR
      FOR SELECT 'DOCGROUP',
                 jq.taskqueueid,
                 jq.documentgroupid,
                 dgd.documentGroupName,
                 COALESCE(jql.lockStatus,'OPEN')
            FROM jobdetails jd
           INNER JOIN jobqueue jq
              ON jd.jobid=jq.jobid
           INNER JOIN documentgroupdetails dgd
              ON jq.documentgroupid=dgd.documentgroupid
            LEFT JOIN jobqueuelocked jql
              ON jq.taskqueueid=jql.taskqueueid
           GROUP BY jq.taskqueueid;

  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;

  CREATE TEMPORARY TABLE tempProdItem(
    prodType SET('JOB','BATCH','DOCGROUP'),
    taskQueueId BIGINT(20),
    jobBatchDocId BIGINT(20),
    jobBatchDocName VARCHAR(255),
    lockStatus SET('LOCKED','OPEN')
  );


  OPEN docCursor;

  Loop1: LOOP
    FETCH docCursor INTO prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus;
    IF done=1 THEN
      LEAVE Loop1;
    ELSE
      INSERT INTO tempProdItem
             (prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus)
      VALUES (prodType,taskQueueId,jobBatchDocId,jobBatchDocName,lockStatus);
    END IF;

  END LOOP;

  CLOSE docCursor;

  SELECT * FROM tempProdItem;

END

$$
DELIMITER;
于 2012-05-22T13:00:24.530 回答