SQL事务/ ErrorHandling / TryandCatch

时间:2017-01-13 06:06:18

标签: sql-server-2012

采用SQL编程(2012版)课程并完全陷入困境,无论我尝试多少次,程序都无法工作。到目前为止的要求(问题)以及我的内容如下。虚线下面是我为错误处理编写的另一个proc。请帮我完成这个......拜托!

/ *创建一个接受StockName,NewOpenPrice,NewClosePrice的存储过程。

一个。如果股票名称不存在,则应将新记录添加到dbo.Stocks表

湾如果股票名称确实存在,则OpenPrice和ClosePrice将使用新插入的价格进行更新。

℃。应使用事务(可重复读取隔离级别)

构建Insert和Update语句

d。 Update Catch语句应该用于Update和Insert语句。如果存在错误,则应调用dbo.error_handler存储过程。 * /

CREATE PROCEDURE spc_Stocks

@Name  varchar(25), @NewOpenPrice money, @NewClosePrice money

as
BEGIN   
        CREATE TABLE dbo.Stocks (
        StockID int IDENTITY(1,1), 
        StockName varchar(50), 
        OpenPrice money, 
        ClosePrice money    )

        INSERT INTO dbo.Stocks
        SELECT 'Walmart',21.58,22.98 UNION
        SELECT 'Target',17.32,15.23 UNION
        SELECT 'Taco Bell',4.58,12.98 UNION
        SELECT 'Microsoft',7.15,8.15 UNION
        SELECT 'Apple',10.79,9.89

        Select StockName from stocks 
        where StockName = @Name 
------Name does NOT exist 
    if (@Name = NULL)
Begin
        Insert into dbo.Stocks (StockName)
        Values (@Name)
END
----If name DOES exist 
ELSE
    BEGIN
        Begin TRY
            Begin SET TRANSACTION ISOLATION LEVEL REPEATABLE READ 
            UPDATE dbo.Stocks SET OpenPrice =@NewOpenPrice, ClosePrice= @NewClosePrice where StockName = @Name
    Commit transaction
    END TRY
    BEGIN CATCH

?!?!?!?!?!?

*这是我的ErrorHandler存储过程查询

ALTER PROCEDURE dbo.error_handler

as

BEGIN

DECLARE @errnum INT,
        @severity INT,
        @errstate INT,
        @proc NVARCHAR(126),
        @line INT,
        @message NVARCHAR(4000)
-- capture the error information that caused the CATCH block to be invoked
SELECT @errnum = ERROR_NUMBER(),
       @severity = ERROR_SEVERITY(),
       @errstate = ERROR_STATE(),
       @proc = ERROR_PROCEDURE(),
       @line = ERROR_LINE(),
       @message = ERROR_MESSAGE()
    end

1 个答案:

答案 0 :(得分:0)

Create PROCEDURE [dbo].[USP_Stocks]
      (@Name varchar(25), @OpenPrice MONEY, @ClosePrice MONEY)
AS
BEGIN

    -----Name does NOT exist
    IF NOT EXISTS (SELECT StockName FROM [dbo].[Stocks]
                    WHERE StockName = @Name) 

    BEGIN

        BEGIN TRY
            SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
            BEGIN TRAN
                INSERT INTO dbo.Stocks (StockName, OpenPrice, ClosePrice)
                VALUES (@Name ,@OpenPrice, @ClosePrice)
            COMMIT TRANSACTION
        END TRY

        BEGIN CATCH 
            EXEC dbo.error_handler
            ROLLBACK
        END CATCH

    END

    ----If name DOES exist 
    ELSE
    BEGIN
        BEGIN TRY
            SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
                BEGIN TRAN

                UPDATE dbo.Stocks SET OpenPrice =@OpenPrice, ClosePrice= @ClosePrice  
                where StockName = @Name
            Commit transaction
        END TRY
        BEGIN CATCH
            ROLLBACK TRUANSACTION     
            EXEC dbo.error_handler

        END CATCH
    END
END