MySQL存储过程存在多个语句问题

时间:2013-02-24 16:30:08

标签: mysql stored-procedures

我的MySQL存储过程非常糟糕,我无法在数据库中创建此过程(MySQL 5.5.27)

我想知道为什么它拒绝我的变量声明,多选和if else(希望是一些代码错误)我粘贴我的代码!

DELIMITER $$

DROP PROCEDURE IF EXISTS EditEnterprise

$$

CREATE PROCEDURE  EditEnterprise(    
    IN pEnterpriseId                VARCHAR(32),    
    IN pEnterpriseName              VARCHAR(64)    
) 

/* declare the new variables */    
DECLARE nEnterpriseId               VARCHAR(32);    
DECLARE nEnterpriseName             VARCHAR(64); 

/* declare the old variables */    
DECLARE oEnterpriseId               VARCHAR(32);    
DECLARE oEnterpriseName             VARCHAR(64);

BEGIN 

SET autocommit = 0;    

/* get the old variables */    
SELECT     
    Enterprise_ID = oEnterpriseId,    
    Enterprise_Name = oEnterpriseName    
FROM     
    enterprise    
WHERE    
    Enterprise_ID = pEnterpriseId;    

/* set the variables with the new inputs */  
IF pEnterpriseName = NULL    
    SET nEnterpriseName = oEnterpriseName    
ELSE IF pEnterpriseName = ''    
    SET oEnterpriseName = NULL    
ELSE
    SET nEnterpriseName = pEnterpriseName    


UPDATE enterprise    
SET     
    Enterprise_Name = nEnterpriseName    
WHERE Enterprise_ID = pEnterpriseId;

COMMIT;

END $$

DELIMITER ; 

2 个答案:

答案 0 :(得分:2)

在Mysql(与Oracle不同)中,DECLARE追踪BEGIN

您的代码应为:

DELIMITER $$

DROP PROCEDURE IF EXISTS EditEnterprise$$

CREATE PROCEDURE  EditEnterprise(
    IN pEnterpriseId                VARCHAR(32),
    IN pEnterpriseName              VARCHAR(64)
) 
BEGIN 
/* declare the new variables */
DECLARE nEnterpriseId               VARCHAR(32);
DECLARE nEnterpriseName             VARCHAR(64);

/* declare the old variables */
DECLARE oEnterpriseId               VARCHAR(32);
DECLARE oEnterpriseName             VARCHAR(64);

SET autocommit = 0;

/* get the old variables */
SELECT 
    Enterprise_ID := oEnterpriseId,
    Enterprise_Name := oEnterpriseName
FROM 
    enterprise
WHERE
    Enterprise_ID = pEnterpriseId;

/* set the variables with the new inputs */


IF pEnterpriseName IS NULL THEN
    SET nEnterpriseName = oEnterpriseName;
ELSEIF pEnterpriseName = '' THEN
    SET oEnterpriseName = NULL;
ELSE
    SET nEnterpriseName = pEnterpriseName;
END IF;

UPDATE enterprise SET 
    Enterprise_Name = nEnterpriseName
WHERE Enterprise_ID = pEnterpriseId;

COMMIT;

END$$

DELIMITER ; 

我还添加了丢失的;THENEND IF个关键字。 另外,请注意您可能在此行中输入错字:

SET oEnterpriseName = NULL;

你可能意味着:

SET nEnterpriseName = NULL;

现在我认为你应该重新考虑你的存储过程。实际上,所有这些都可以在单个SQL查询中完成。你真的需要一个存储过程吗?除非您有限制性拨款政策,否则可能只是多余的。

答案 1 :(得分:2)

SP中存在一些错误,应该是这样的

DELIMITER $$
DROP PROCEDURE IF EXISTS EditEnterprise
$$
CREATE PROCEDURE  EditEnterprise(
    IN pEnterpriseId                VARCHAR(32),
    IN pEnterpriseName              VARCHAR(64)
)

BEGIN

/* declare the new variables */
DECLARE nEnterpriseId               VARCHAR(32);
DECLARE nEnterpriseName             VARCHAR(64);

/* declare the old variables */
DECLARE oEnterpriseId               VARCHAR(32);
DECLARE oEnterpriseName             VARCHAR(64);

SET autocommit = 0;

/* get the old variables */
SELECT 
    @oEnterpriseID := Enterprise_ID,
    @oEnterpriseName := Enterprise_Name
FROM 
    enterprise
WHERE
    Enterprise_ID = pEnterpriseId;

/* set the variables with the new inputs */
IF pEnterpriseName = NULL THEN  
    SET @nEnterpriseName = @oEnterpriseName;
ELSEIF pEnterpriseName = '' THEN   
    SET @oEnterpriseName = NULL;
ELSE
    SET @nEnterpriseName = pEnterpriseName;
END IF;

UPDATE enterprise
SET 
    Enterprise_Name = nEnterpriseName,
    Enterprise_Established_Date = nEnterpriseCreateDate,
    Enterprise_Description = nEnterpriseDetails,
    Enterprise_Type = nEnterpriseType,
    Enterprise_Status = nEnterpriseStatus
WHERE Enterprise_ID = pEnterpriseId;
COMMIT;

END $$
DELIMITER ; 

指出一些错误

  • 如果ELSE IF适用于多种情况,则为ELSEIF

  • 声明变量“DECLARE myvar VARCHAR(32)”后,如果我们不使用SET命令,我们需要使用:=为其赋值。