SQL Server/T-SQL/Transact SQL/Error — различия между версиями

Материал из SQL эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 10:20, 26 мая 2010

A simple example of the @@ERROR variable

1> -- A simple example of the @@ERROR variable.
2>
3>
4> SELECT 5 / 0
5> SELECT @@ERROR
6>
7> GO
Msg 8134, Level 16, State 1, Server sqle\SQLEXPRESS, Line 4
Divide by zero error encountered.
-----------
       8134
(1 rows affected)
1>



Create a Stored Procedure that raises an error

3> CREATE PROCEDURE #usp_TempProc
4> AS
5> SELECT 1/0
6> RETURN @@ERROR
7> GO
1>
2> DECLARE @ErrorCode int
3> EXEC @ErrorCode = #usp_TempProc
4> PRINT @ErrorCode
5> GO
Msg 8134, Level 16, State 1, Server BCE67B1242DE45A\SQLEXPRESS, Procedure #usp_TempProc_________________________________
__________________________________________________________________________00000077, Line 11
Divide by zero error encountered.
(0 rows affected)
8134
1>
2> drop procedure #usp_TempProc
3> GO



Rollback transaction on error

23>
24> CREATE TABLE MySavings(AccountNum Int NOT NULL,
25>                        Amount Money NOT NULL)
26>
27> CREATE TABLE MyChecking(AccountNum Int NOT NULL,
28>                         Amount Money NOT NULL)
29>
30> ALTER TABLE MyChecking ADD CONSTRAINT ckMinBalance
31> CHECK (Amount > $100.00)
32>
33> INSERT MySavings VALUES (12345, $1000.00)
34>
35> INSERT MyChecking VALUES (12345, $1000.00)
36> GO
(1 rows affected)
(1 rows affected)
1>
2> /*SQL SERVER 2000 Error Handling*/
3> BEGIN TRANSACTION
4>   UPDATE MyChecking SET Amount = Amount - $990.00
5>   WHERE AccountNum = 12345
6>     IF @@ERROR != 0
7>       BEGIN
8>         ROLLBACK TRANSACTION
9>         RETURN
10>       END
11>     ELSE
12>   UPDATE MySavings SET Amount = Amount + $990.00
13>   WHERE AccountNum = 12345
14>     IF @@ERROR != 0
15>       BEGIN
16>         ROLLBACK TRANSACTION
17>         RETURN
18>       END
19>     ELSE
20> COMMIT TRANSACTION
21> GO
Msg 547, Level 16, State 1, Server sqle\SQLEXPRESS, Line 4
The UPDATE statement conflicted with the CHECK constraint "ckMinBalance". The conflict occurred in database "master", table "dbo.MyChecking"
The statement has been terminated.
1>
2> drop table MySavings;
3> drop table MyChecking;
4> GO
1>
2>



Use @Error

1> create table employee(
2>     ID          int,
3>     name        nvarchar (10),
4>     salary      int,
5>     start_date  datetime,
6>     city        nvarchar (10),
7>     region      char (1))
8> GO
1>
2> insert into employee (ID, name,    salary, start_date, city,       region)
3>               values (1,  "Jason", 40420,  "02/01/94", "New York", "W")
4> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (2,  "Robert",14420,  "01/02/95", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (3,  "Celia", 24020,  "12/03/96", "Toronto",  "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (4,  "Linda", 40620,  "11/04/97", "New York", "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (5,  "David", 80026,  "10/05/98", "Vancouver","W")
3> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (6,  "James", 70060,  "09/06/99", "Toronto",  "N")
3> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (7,  "Alison",90620,  "08/07/00", "New York", "W")
3> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (8,  "Chris", 26020,  "07/08/01", "Vancouver","N")
3> GO
(1 rows affected)
1> insert into employee (ID, name,    salary, start_date, city,       region)
2>               values (9,  "Mary",  60020,  "06/09/02", "Toronto",  "W")
3> GO
(1 rows affected)
1>
2> select * from employee
3> GO
ID          name       salary      start_date              city       region
----------- ---------- ----------- ----------------------- ---------- ------
          1 Jason            40420 1994-02-01 00:00:00.000 New York   W
          2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
          3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
          4 Linda            40620 1997-11-04 00:00:00.000 New York   N
          5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
          6 James            70060 1999-09-06 00:00:00.000 Toronto    N
          7 Alison           90620 2000-08-07 00:00:00.000 New York   W
          8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
          9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W
(9 rows affected)
1>
2> --Dealing with Errors
3>
4> -- Making Use of @@ERROR
5>
6> DECLARE @Error int
7> INSERT INTO Employee  (ID) VALUES (999999)
8> SELECT @Error = @@ERROR
9> PRINT ""
10> PRINT "The Value of @Error is " + CONVERT(varchar, @Error)
11> PRINT "The Value of @@ERROR is " + CONVERT(varchar, @@ERROR)
12> GO
(1 rows affected)
(1 rows affected)

(1 rows affected)
The Value of @Error is 0
(1 rows affected)
The Value of @@ERROR is 0
1>
2> select * from employee
3> GO
ID          name       salary      start_date              city       region
----------- ---------- ----------- ----------------------- ---------- ------
          1 Jason            40420 1994-02-01 00:00:00.000 New York   W
          2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
          3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
          4 Linda            40620 1997-11-04 00:00:00.000 New York   N
          5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
          6 James            70060 1999-09-06 00:00:00.000 Toronto    N
          7 Alison           90620 2000-08-07 00:00:00.000 New York   W
          8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
          9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W
     999999 NULL              NULL                    NULL NULL       NULL
(10 rows affected)
1>
2>
3> drop table employee
4> GO
1>