1

我在使用 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 ; 
4

2 回答 2

2

DECLARE在Mysql (与 Oracle 不同)中,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 查询中完成。你真的需要一个存储过程吗?除非您有限制性拨款政策,否则这可能是多余的。

于 2013-02-24T16:40:31.163 回答
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 ; 

需要指出的一些错误

  • 它的 ELSEIF 而不是 ELSE IF 用于多个条件

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

于 2013-02-25T03:50:38.303 回答