2009-09-26 50 views
25
CREATE PROCEDURE [dbo].[PL_GEN_PROVN_NO1]   
@GAD_COMP_CODE VARCHAR(2) =NULL, 
@@voucher_no numeric =null output 
AS   
BEGIN 
    DECLARE @NUM NUMERIC 
    DECLARE @PNO NUMERIC        
    SET @PNO = 0 
    DECLARE @PNO1 NUMERIC 
    SET @PNO1=0 

-- begin transaction 

    IF NOT EXISTS (select GLDC_NEXT_PRV_NO 
       FROM GLAS_FINANCIAL_DOCUMENTS   
        WHERE GLDC_COMP_CODE = @GAD_COMP_CODE   
        AND GLDC_DOC_CODE = 'JV') 
    BEGIN 
       RAISERROR ('Error in generating provision number..',16,1) 
       -- ROLLBACK TRANSACTION 
    END 
ELSE 
SELECT @PNO=ISNULL(GLDC_NEXT_PRV_NO,0)+1 
FROM GLAS_FINANCIAL_DOCUMENTS   
WHERE GLDC_COMP_CODE = @GAD_COMP_CODE   
AND GLDC_DOC_CODE = 'JV' 

UPDATE GLAS_FINANCIAL_DOCUMENTS   
SET GLDC_NEXT_PRV_NO = @PNO   
WHERE GLDC_COMP_CODE = @GAD_COMP_CODE   
AND GLDC_DOC_CODE = 'JV' 

set @@[email protected]  
--commit transaction 
END 

在這個過程中如何處理try catch異常?如何添加一個Try/Catch到SQL存儲過程

+0

它的傷心,但我有這個在兩種工作 - 人堅持在帽編寫SQL! – 2009-09-26 13:22:07

回答

29

here

CREATE PROCEDURE [dbo].[PL_GEN_PROVN_NO1]   
     @GAD_COMP_CODE VARCHAR(2) =NULL, 
     @@voucher_no numeric =null output 
     AS   
    BEGIN 

    begin try 
     -- your proc code 
    end try 

    begin catch 
      -- what you want to do in catch 
    end catch  
    END -- proc end 
27

的Transact-SQL是一個比較棘手的是C#或C++ try/catch塊,由於交易的複雜性。 CATCH塊必須檢查xact_state()函數,並確定它是否可以提交或需要回滾。我已經涵蓋在我的博客的話題,我有一篇文章,介紹如何正確處理與try catch塊交易,包括可能的嵌套事務:Exception handling and nested transactions.

create procedure [usp_my_procedure_name] 
as 
begin 
    set nocount on; 
    declare @trancount int; 
    set @trancount = @@trancount; 
    begin try 
     if @trancount = 0 
      begin transaction 
     else 
      save transaction usp_my_procedure_name; 

     -- Do the actual work here 

lbexit: 
     if @trancount = 0 
      commit; 
    end try 
    begin catch 
     declare @error int, @message varchar(4000), @xstate int; 
     select @error = ERROR_NUMBER(), 
       @message = ERROR_MESSAGE(), @xstate = XACT_STATE(); 
     if @xstate = -1 
      rollback; 
     if @xstate = 1 and @trancount = 0 
      rollback 
     if @xstate = 1 and @trancount > 0 
      rollback transaction usp_my_procedure_name; 

     raiserror ('usp_my_procedure_name: %d: %s', 16, 1, @error, @message) ; 
     return; 
    end catch 
end 
2

是的 - 你甚至可以嵌套在try catch語句如:

BEGIN TRY 
SET @myFixDte = CONVERT(datetime, @myFixDteStr,101) 
END TRY 
BEGIN CATCH 
    BEGIN TRY 
     SET @myFixDte = CONVERT(datetime, @myFixDteStr,103) 
END TRY 
BEGIN CATCH 
    BEGIN TRY 
     SET @myFixDte = CONVERT(datetime, @myFixDteStr,104) 
    END TRY 
    BEGIN CATCH 
     SET @myFixDte = CONVERT(datetime, @myFixDteStr,105) 
    END CATCH 
END CATCH END CATCH 
0
Create Proc[usp_mquestions] 
( 
@title nvarchar(500), --0 
@tags nvarchar(max), --1 
@category nvarchar(200), --2 
@ispoll char(1), --3 
@descriptions nvarchar(max), --4 
)    
AS 
BEGIN TRY 




BEGIN 
DECLARE @message varchar(1000); 
DECLARE @tempid bigint; 

IF((SELECT count(id) from [xyz] WHERE [email protected])>0) 
BEGIN 
SELECT 'record already existed.'; 
END 
ELSE 
BEGIN    


if @id=0 
begin 
select @tempid =id from [xyz] where [email protected]; 

if @tempid is null 
BEGIN 
     INSERT INTO xyz 
     (entrydate,updatedate) 
     VALUES 
     (GETDATE(),GETDATE()) 

     SET @[email protected]@IDENTITY; 
END 
END 
ELSE 
BEGIN 
set @[email protected] 
END 
if @tempid>0 
BEGIN 

    -- Updation of table begin-- 


UPDATE tab_questions 
set [email protected], --0 
[email protected], --1 
[email protected], --2 
[email protected], --3 
[email protected], --4 
[email protected], --5 

WHERE [email protected] ; --9 ; 


IF @id=0 
BEGIN 
SET @message= 'success:Record added successfully:'+ convert(varchar(10), @tempid) 
END 
ELSE 
BEGIN 
SET @message= 'success:Record updated successfully.:'+ convert(varchar(10), @tempid) 

END 
END 
ELSE 
BEGIN 
SET @message= 'failed:invalid request:'+convert(varchar(10), @tempid) 
END 

END 
END 

END TRY 
BEGIN CATCH 
    SET @message='failed:'+ ERROR_MESSAGE(); 
END CATCH 
SELECT @message; 
+0

解釋會很好陪伴答案。 – 2016-08-05 06:32:06

相關問題