转换从MSSQL失败到MySQL

时间:2013-12-11 09:14:41

标签: mysql sql sql-server

我在尝试将MSSQL代码转换为MySQL时遇到了问题。

我的MSSQL代码是:

CREATE PROCEDURE ProductGroupGenerationSettingsCheck  
AS  
declare @id bit , @result varchar(50);  

SELECT  @id = automaticProductIdGeneration   
FROM         tbl_Settings--)  

if(@id =0)  
 begin  
 set @result='false'  
 end  

else if (@id =1)  
 begin  
 set @result='true'  
 end  


select @result  

我的MySQL代码是:

delimiter //
create procedure ProductGroupGenerationSettingsCheck(p_id tinyint(1),p_result varchar(50))
begin
    select p_id = automaticProductIdGeneration from tbl_Settings ;
    if(p_id = 0)
    begin 
    set p_result = 'false' ;
    end
    else if (p_id = 1)
    begin
        set p_result = 'true' ;
    end

    select p_result  as 'result' ;


end //
delimiter ;

我得到的错误是:

  

#1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在'begin set p_result ='false'附近使用正确的语法;如果(p_id = 1)在第5行开始设置p_re'

,则结束

我的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

delimiter //
create procedure ProductGroupGenerationSettingsCheck(IN p_id tinyint(1),IN p_result varchar(50)) /*specify what type of parameter it is, IN / OUT / INOUT*/
begin
    select p_id := automaticProductIdGeneration from tbl_Settings ; /*use assignment operator := instead of comparison =*/
    if(p_id = 0) then /*missing a then here*/
    begin 
    set p_result = 'false' ;
    /*don't end the if, when you still have an else if condition*/
    else if (p_id = 1) then /*missing a then again*/
    begin
        set p_result = 'true' ;
    end
    end if; /*missing an if here*/

    select p_result  as 'result' ; /*you could also use an OUT parameter for this...anyway...*/


end //
delimiter ;

答案 1 :(得分:1)

第四行的查询中有一个错误。哟必须使用下一个查询。

select automaticProductIdGeneration into p_id from tbl_Settings;

如果您发布整个错误消息,它可能会有用。它告诉你哪里是第一个问题。