Friday, March 23, 2012

I want to set this error into a variable

hi all
Now I have an error operation, then SQL Server give error message as
follows:
Violation of UNIQUE KEY constraint 'gwbh'. Cannot insert duplicate key in
object 't_xt_gwsj'. "
I want to set this error into a variable, e.g. @.errstr
how to do?
thanks!
?Hi
http://www.sommarskog.se/error-handling-I.html
http://www.sommarskog.se/error-handling-II.html
"?" <zhangchao19@.hotmail.com> wrote in message
news:%237btgD$HGHA.3752@.TK2MSFTNGP11.phx.gbl...
> hi all
> Now I have an error operation, then SQL Server give error message as
> follows:
> Violation of UNIQUE KEY constraint 'gwbh'. Cannot insert duplicate key in
> object 't_xt_gwsj'. "
> I want to set this error into a variable, e.g. @.errstr
> how to do?
> thanks!
> --
> ?
>|||thank you very much, these articles is very helpful.
I also have a question, for example:
when @.error=111 , the corresponding string in table sysmessages is
" '%ls' must be the first statement in a query batch. " (mark as A)
and in query analyzer we may see :
" create procedure must be the first statement in a query batch. " (mark as
B)
In this case, "create procedure" is the run-time value of %ls.
Now, I want to know, Is it possible to get this run-time value diretly? As
example above,
Is it possible to get the value "create procedure" without comparing and
parsing
the origin message A and run-time string B.
thx again.
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:u21TIK$HGHA.3984@.TK2MSFTNGP14.phx.gbl...
> Hi
> http://www.sommarskog.se/error-handling-I.html
> http://www.sommarskog.se/error-handling-II.html
>
> "?" <zhangchao19@.hotmail.com> wrote in message
> news:%237btgD$HGHA.3752@.TK2MSFTNGP11.phx.gbl...
>|||There are some kind of errors that you will not be able to capture. Upgrade
to SQL Server 2005 and you will benefit from BEGIN TRY ..CATCH error handle.
BEGIN TRANSACTION
BEGIN TRY
INSERT Title VALUES (@.Title_ID, Title_Name, ' ', ' ', ' ', ' ', 1112, 0)
WAITFOR DELAY '00:00:05'
SELECT COUNT (*) FROM Authors
COMMIT
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ER_Num
ROLLBACK
END CATCH;
SELECT @.@.TRANCOUNT AS '@.@.TCount
"tamsun" <tamsun@.gmail.com> wrote in message
news:uOqivOAIGHA.3944@.tk2msftngp13.phx.gbl...
> thank you very much, these articles is very helpful.
> I also have a question, for example:
> when @.error=111 , the corresponding string in table sysmessages is
> " '%ls' must be the first statement in a query batch. " (mark as A)
> and in query analyzer we may see :
> " create procedure must be the first statement in a query batch. " (mark
> as B)
> In this case, "create procedure" is the run-time value of %ls.
> Now, I want to know, Is it possible to get this run-time value diretly? As
> example above,
> Is it possible to get the value "create procedure" without comparing and
> parsing
> the origin message A and run-time string B.
> thx again.
>
> "Uri Dimant" <urid@.iscar.co.il> wrote in message
> news:u21TIK$HGHA.3984@.TK2MSFTNGP14.phx.gbl...
>|||but only SQL Server 2000
?
"Uri Dimant" <urid@.iscar.co.il> д?
news:eakPiuAIGHA.3064@.TK2MSFTNGP10.phx.gbl...
> There are some kind of errors that you will not be able to capture.
Upgrade
> to SQL Server 2005 and you will benefit from BEGIN TRY ..CATCH error
handle.
> BEGIN TRANSACTION
> BEGIN TRY
> INSERT Title VALUES (@.Title_ID, Title_Name, ' ', ' ', ' ', ' ', 1112, 0)
> WAITFOR DELAY '00:00:05'
> SELECT COUNT (*) FROM Authors
> COMMIT
> END TRY
> BEGIN CATCH
> SELECT ERROR_NUMBER() AS ER_Num
> ROLLBACK
> END CATCH;
> SELECT @.@.TRANCOUNT AS '@.@.TCount
>
>
> "tamsun" <tamsun@.gmail.com> wrote in message
> news:uOqivOAIGHA.3944@.tk2msftngp13.phx.gbl...
(mark
As
>|||only SQL Server 2000
?
"Uri Dimant" <urid@.iscar.co.il> д?
news:eakPiuAIGHA.3064@.TK2MSFTNGP10.phx.gbl...
> There are some kind of errors that you will not be able to capture.
Upgrade
> to SQL Server 2005 and you will benefit from BEGIN TRY ..CATCH error
handle.
> BEGIN TRANSACTION
> BEGIN TRY
> INSERT Title VALUES (@.Title_ID, Title_Name, ' ', ' ', ' ', ' ', 1112, 0)
> WAITFOR DELAY '00:00:05'
> SELECT COUNT (*) FROM Authors
> COMMIT
> END TRY
> BEGIN CATCH
> SELECT ERROR_NUMBER() AS ER_Num
> ROLLBACK
> END CATCH;
> SELECT @.@.TRANCOUNT AS '@.@.TCount
>
>
> "tamsun" <tamsun@.gmail.com> wrote in message
> news:uOqivOAIGHA.3944@.tk2msftngp13.phx.gbl...
(mark
As
>

No comments:

Post a Comment