在mysql中创建存储过程时显示语法错误

时间:2013-12-13 10:02:38

标签: php mysql sql

    create procedure purchaseDetailsCheckOnDelete(p_purchaseDetailsId varchar(50))
    begin
        declare p_Exist tinyint(1);
        set p_Exist = False ;
        select p_Exist = (case  count(purchaseDetailsId) when '0' then False else True end  from tbl_PurchaseReturnDetails
        where (purchaseDetailsId = p_purchaseDetailsId) );
        select p_Exist ;

    end
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'from
tbl_PurchaseReturnDetails
                where (purchaseDetailsId = p_purchaseDetailsId)' at line 5

4 个答案:

答案 0 :(得分:1)

试试这个:

create procedure purchaseDetailsCheckOnDelete(p_purchaseDetailsId varchar(50))
begin
    declare p_Exist tinyint(1);
    set p_Exist = False ;
    select p_Exist = case  count(purchaseDetailsId) when '0' then False else True end  from tbl_PurchaseReturnDetails
    where purchaseDetailsId = p_purchaseDetailsId;
    select p_Exist ;

end

答案 1 :(得分:1)

每条错误消息都会显示可能的错误。
在你的情况下,它说... for the right syntax to use near 'from tbl_PurchaseReturnDetails ...

当它显示near时,表示错误是之前该特定声明。
所以from之前的陈述是错误的。你把闭合支架放在了错误的地方。

更改语句

select p_Exist =
  ( case count(purchaseDetailsId) when '0' then False else True end
from tbl_PurchaseReturnDetails
  where (purchaseDetailsId = p_purchaseDetailsId) );

select p_Exist =
  ( case count(purchaseDetailsId) when '0' then False else True end )
from tbl_PurchaseReturnDetails
  where (purchaseDetailsId = p_purchaseDetailsId);

答案 2 :(得分:1)

如果您决定使用括号,那么您错过了select,即

    select p_Exist = 
      (select case count(purchaseDetailsId) 

SqlFiddle here

或者,删除括号

    select p_Exist = case count(purchaseDetailsId) 
              when '0' then False else True end  
       from tbl_PurchaseReturnDetails
       where purchaseDetailsId = p_purchaseDetailsId;        

SqlFiddle here

修改

更短的时间:

    SELECT IFNULL((select 1 from tbl_PurchaseReturnDetails
       WHERE purchaseDetailsId = p_purchaseDetailsId), 0) AS p_Exist;

Fiddle

答案 3 :(得分:0)

使用此代码段

delimiter //
drop procedure if exists purchaseDetailsCheckOnDelete;
create procedure purchaseDetailsCheckOnDelete(p_purchaseDetailsId varchar(50))
   begin
       declare p_Exist tinyint(1);
       set p_Exist = False ;
       select p_Exist = (case  count(purchaseDetailsId) when '0' then False else True end) as 'P_EXIST'
       from tbl_PurchaseReturnDetails
       where (purchaseDetailsId = p_purchaseDetailsId);
       select p_Exist ;
   end//