2013-12-13 57 views
-2
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 
+0

沒有機會..沒有Mising) – Nisar

回答

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

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

每個錯誤消息示出了可能的錯誤是。
在你的情況下,它說,... 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); 
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//