2016-10-14 59 views
-2

我寫了一個存儲過程,我越來越近線64不正確的語法錯誤的語法提交在存儲過程中

CREATE PROCEDURE dbo.Transactions_Create 
(@AccountID INT, @EmplNo smallint,@Amount Money,@Description  VARCHAR(100),@EnteredBy VARCHAR(41),@Type char(1), 
    @TransId INT OUTPUT) 

AS 
    SET XACT_ABORT ON; 
    BEGIN Transaction 
     Declare @ActType dbo.AccountType 
     select @ActType =Type from dbo.Accounts 
     where [email protected]; 

Declare @Withdraw smallint 
select @Withdraw 
=WithdrawalCount from dbo.Accounts 
where [email protected] 

Declare @ServiceFee Money 
Declare @withdrawchange smallInt 
Declare @balancechange Money 

if @Type='D' 
    BEGIN 
     SET @ServiceFee=0.0; 
     SET @withdrawchange=0; 
     SET @[email protected]; 

    end; 
else 
    BEGIN  
     SET @withdrawchange=1; 
     if @ActType='cheaquing' 
      begin 
       SET @ServiceFee=0.50; 
      end 
      else 
       begin 
        if @Withdraw<2 
         begin 
          SET @ServiceFee=0.0; 
        end 
        else 
         begin 
          SET @ServiceFee=1.00; 
         end 
    SET @balancechange=(@[email protected])*-1; 
    end 
update dbo.Accounts 
SET Balance= [email protected], [email protected] 
where [email protected]; 

INSERT Into dbo.Transactions(AccountID, EmplNo ,Amount, Description ,EnteredBy ,Type) 

values (@AccountID, @EmplNo ,@Amount, @Description ,@EnteredBy ,@Type); 

SET @TransID = SCOPE_IDENTITY(); 

commit Transaction; 

我不知道什麼是錯我的語法。一切看起來都是正確的,從我所理解的一切都應該進入BEGIN TRANSACTION COMMIT TRANSACTION塊,所以我不明白爲什麼這是失敗的

+2

我猜你的'end's不與'begin's排隊。 –

+2

閱讀錯誤消息。考慮錯誤信息並將「問題」分解成更小的部分,直到確定實際問題區域。修復問題,並重新構建代碼/語法/查詢,確保不會再次破壞它。如果仍然存在問題(識別出一些相關的代碼後),然後詢問*幷包括相關的錯誤*。 – user2864740

回答

0

在AS語句之前聲明所有變量並開始事務。應該幫助這一點

CREATE PROCEDURE dbo.Transactions_Create 

@AccountID INT, 
@EmplNo smallint, 
@Amount Money, 
@Description VARCHAR(100), 
@EnteredBy VARCHAR(41), 
@Type char(1), 
@TransId INT OUTPUT, 
@Withdraw smallint, 
@ServiceFee Money, 
@withdrawchange smallInt, 
@balancechange Money, 
@ActType dbo.AccountType 

AS 
    SET XACT_ABORT ON; 
    BEGIN Transaction 
     select @ActType =Type from dbo.Accounts 
     where [email protected]; 


select @Withdraw = WithdrawalCount from dbo.Accounts 
where [email protected] 

if @Type='D' 
    BEGIN 
     SET @ServiceFee=0.0; 
     SET @withdrawchange=0; 
     SET @[email protected]; 

    end; 
else 
    BEGIN  
     SET @withdrawchange=1; 
     if @ActType='cheaquing' 
      begin 
       SET @ServiceFee=0.50; 
      end 
      else 
       begin 
        if @Withdraw<2 
         begin 
          SET @ServiceFee=0.0; 
        end 
        else 
         begin 
          SET @ServiceFee=1.00; 
         end 
    SET @balancechange=(@[email protected])*-1; 
    end 
update dbo.Accounts 
SET Balance= [email protected], [email protected] 
where [email protected]; 

INSERT Into dbo.Transactions(AccountID, EmplNo ,Amount, Description ,EnteredBy ,Type) 

values (@AccountID, @EmplNo ,@Amount, @Description ,@EnteredBy ,@Type); 

SET @TransID = SCOPE_IDENTITY(); 

commit Transaction; 
+0

試圖移動聲明,因爲您仍然給出相同的語法錯誤 – jsomers89

+0

嘗試用BEGIN TRY正確替換SET XACT_ABORT ON,然後放置結束TRY,BEGIN CATCH,ROLLBACK TRANSACTION; END CATCH;在COMMIT TRANSACTION之後。 –

+0

不應該是這一行:從dbo中選擇@Withdraw = WithdrawalCount。賬戶 其中AccountId = @ AccountId 有;最後呢? –