Friday, February 24, 2012

Error Trapping

Hi all,
For the following script, I was wondering why it traps the
first error, but breaks on the second error without going
into the second error trapping.
Thanks in advance, Anna
----
use pubs
go
declare @.errcnt int
--insert datetime into varchar column
insert into authors (au_id)
values (getdate())
--first error trap
select @.errcnt = @.@.error
if @.errcnt <> 0
begin
print 'first error trap'
end
--insert varcahar into datetime
insert into sales
values (6380, 68709, 'wrong data for datetime', 5, 'Net
60', 'BU1032')
--second error trap
select @.errcnt = @.@.error
if @.errcnt <> 0
begin
print 'second error trap'
endAnna,
In the 2nd case you get syntax error. SQL Server checks whether it can cast
your expression to datetime - if it is not possible, thn you use wrong
syntax. Syntax checking occurs first, before compiling and executing the
expression. Because it was not executed, there was no error trapping.
--
Dejan Sarka, SQL Server MVP
Please reply only to the newsgroups.
"Anna Linne" <lliming12@.yahoo.com> wrote in message
news:0a2f01c38ef2$d9938760$a301280a@.phx.gbl...
> Hi all,
> For the following script, I was wondering why it traps the
> first error, but breaks on the second error without going
> into the second error trapping.
> Thanks in advance, Anna
> ----
> use pubs
> go
> declare @.errcnt int
> --insert datetime into varchar column
> insert into authors (au_id)
> values (getdate())
> --first error trap
> select @.errcnt = @.@.error
> if @.errcnt <> 0
> begin
> print 'first error trap'
> end
> --insert varcahar into datetime
> insert into sales
> values (6380, 68709, 'wrong data for datetime', 5, 'Net
> 60', 'BU1032')
> --second error trap
> select @.errcnt = @.@.error
> if @.errcnt <> 0
> begin
> print 'second error trap'
> end

No comments:

Post a Comment