error

2019-08-27  本文已影响0人  鲸鱼酱375

1. Error Handling

1.1 What is Error Handling?

1.2 Methods of Error Handling

1.2.1 RaisError

It is s system function for providing info about the error. RAISERROR takes min of 3 parameter and it can be more based on error message requirements
Param1: msg_id from sys.messages or user defined message string or a variable which has error message or error id
Param2: Severity or Level. User can define his own severity level or if -1 is used severity is same as from sys.messages
Param3: State values range from 0-255
Other Params are based in error message requirements

like raise a flag

1.2.2 @@Error

1.2.3Try….Catch

如果catch错了,再创建一个catch block

1.2.4 If…Else

Mainly for logical use, one can anticipate possible errors and make proper conditions

1.2.5 throw

THROW: It is a new error function provided from SS 2012. It takes 3 parameters.
Param1: Message ID that user wants to display for the message
Param2: Message text
Param3: State
Severity is always 16 unless THROW is displaying original system message and severity.
THORW will give original exception in CATCH block.
THROW needs previous statement to be terminated with a semi colon.

  
CREATE TABLE dbo.TestRethrow  
(    ID INT PRIMARY KEY  
);  
BEGIN TRY  
    INSERT dbo.TestRethrow(ID) VALUES(1);  
--  Force error 2627, Violation of PRIMARY KEY constraint to be raised.  
    INSERT dbo.TestRethrow(ID) VALUES(1);  
END TRY  
BEGIN CATCH  
  
    PRINT 'In catch block.';  
    THROW;  
END CATCH;  

differencr between raiseerror and throw

14.3 视频代码

---syntax is correct
begin try
    select BusinessEntityID+JobTitle
    from AdventureWorks2017.HumanResources.Employee
end try

begin catch
    Print   'please corrext your syntax'
end catch



go
begin try
    select BusinessEntityID+JobTitle
    from AdventureWorks2017.HumanResources.Employee
end try

begin catch
    raiserror('there has been an error',5,2)
end catch
------------------create critical level
go
begin try
    select BusinessEntityID+JobTitle
    from AdventureWorks2017.HumanResources.Employee
end try

begin catch
    raiserror('there has been an error',13,2)  ---crtical level:13
end catch
-----------log: get the log file of error
go
begin try
    select BusinessEntityID+JobTitle
    from AdventureWorks2017.HumanResources.Employee
end try

begin catch
    raiserror('there has been an error',22,2)  ---crtical level
    with log
end catch


select BusinessEntityID+JobTitle
    from AdventureWorks2017.HumanResources.Employee
select @@error

select *
from sys.messages
where message_id =@@error

14.4 note

DROP TABLE IF EXISTS Person
GO
CREATE TABLE Person
(PerID INT IDENTITY PRIMARY KEY,
PerName VARCHAR(100) NOT NULL
)
GO

IF OBJECT_ID('PersonDep') IS NOT NULL
DROP TABLE PersonDep
GO

CREATE TABLE PersonDep
(
PerDepID INT IDENTITY (101,1) PRIMARY KEY,
DepName VARCHAR(100) NOT NULL,
DOB DATE CHECK (DATEDIFF(YY, DOB, GETDATE()) < 18),
PerID INT FOREIGN KEY REFERENCES Person(PerID) 
)
GO

CREATE OR ALTER PROC spPerDetails 
(@PerName VARCHAR(100), @DepName VARCHAR(100), @DepDOB DATE)
AS
    DECLARE @PerID INT = 0

    BEGIN TRY
        INSERT INTO Person VALUES
        (@PerName)
        --PRINT 'After Parent Insert'
        SET @PerID = (SELECT MAX(PerID)
                      FROM Person 
                      WHERE PerName = @PerName) 
        --PRINT 'After SET'
        INSERT INTO PersonDep VALUES
        (@DepName, @DepDOB, @PerID)
    END TRY

    BEGIN CATCH
        PRINT 'Invalid DATE is provided for DOB of Dependent'
        DELETE FROM Person
        WHERE PerID = @PerID
        EXEC spPerDetails @PerName, @DepName, NULL

    END CATCH
GO

EXEC spPerDetails 'Jason', 'Eva', '12/15/2016'

SELECT * FROM Person
SELECT * FROM PersonDep

EXEC spPerDetails 'Kaminsky', 'Viva', '12/15/2000'


上一篇 下一篇

猜你喜欢

热点阅读