In the last hour a few people have referred to the site: http://www.sommarskog.se/
error-handling-II.html
I am trying to figure out the error checking. I used the document and the bo
l. For example, I cut this from the bol:
USE pubs
GO
UPDATE authors SET au_id = '172 32 1176'
WHERE au_id = '172-32-1176'
IF @.@.ERROR = 547
print 'A check constraint violation occurred'
Once I FIXED the code (the bol uses double quotes) I was able to see the pri
nt statement appear. That was an improvement.
Now in the sample code from http://www.sommarskog.se/error-handling-II.html
he returns a value. When I modify my code like this:
USE pubs
GO
UPDATE authors SET au_id = '172 32 1176'
WHERE au_id = '172-32-1176'
IF @.@.ERROR = 547
return 12
(12 is just a made up number to test with)
I get:
Server: Msg 178, Level 15, State 1, Line 7
A RETURN statement with a return value cannot be used in this context.
How CAN I return something?
Thanks.Roger,
Try putting your code inside a stored procedure.
create procedure dbo.p1
@.old_aid varchar(11),
@.new_aid varchar(11)
as
set nocount on
UPDATE dbo.authors
SET au_id = @.new_aid
WHERE au_id = @.old_auid
return @.@.error
go
declare @.rv int
declare @.error int
exec @.rv = dbo.p1 '172-32-1176', '172 32 1176'
set @.error = isnull(nullif(@.rv, 0), @.@.error)
print @.error
go
AMB
"Roger Twomey" wrote:
> In the last hour a few people have referred to the site: http://www.sommarskog.s
e/error-handling-II.html
> I am trying to figure out the error checking. I used the document and the
bol. For example, I cut this from the bol:
> USE pubs
> GO
> UPDATE authors SET au_id = '172 32 1176'
> WHERE au_id = '172-32-1176'
> IF @.@.ERROR = 547
> print 'A check constraint violation occurred'
>
> Once I FIXED the code (the bol uses double quotes) I was able to see the p
rint statement appear. That was an improvement.
> Now in the sample code from [url]http://www.sommarskog.se/error-handling-II.html[/url
] he returns a value. When I modify my code like this:
> USE pubs
> GO
> UPDATE authors SET au_id = '172 32 1176'
> WHERE au_id = '172-32-1176'
> IF @.@.ERROR = 547
> return 12
> (12 is just a made up number to test with)
> I get:
> Server: Msg 178, Level 15, State 1, Line 7
> A RETURN statement with a return value cannot be used in this context.
> How CAN I return something?
> Thanks|||>>
Server: Msg 178, Level 15, State 1, Line 7
A RETURN statement with a return value cannot be used in this context.
How CAN I return something?
By doing so from within a stored procedure. if you are not in a stored
procedure, where are you returning to? PRINT instead, or SELECT, or issue
your own RAISERROR().
A|||Here is my code:
CREATE PROCEDURE dbo.prTest
AS
UPDATE authors SET au_id = '172 32 1176'
WHERE au_id = '172-32-1176'
IF @.@.ERROR = 547
Begin
return 123
End
select 'fred'
go
When I run the proc I get this:
Server: Msg 547, Level 16, State 1, Procedure prTest, Line 4
UPDATE statement conflicted with COLUMN CHECK constraint 'CK__authors__au_id
__0CBAE877'. The conflict occurred in database 'Pubs', table 'authors', colu
mn 'au_id'.
The statement has been terminated.
This is exactly the same result I get if there is no @.@.Error code. I don't s
ee how this error checking code did anything?
Shouldn't I see something different?
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message news:u0ZJrGvYGH
A.4760@.TK2MSFTNGP03.phx.gbl...
> Server: Msg 178, Level 15, State 1, Line 7
> A RETURN statement with a return value cannot be used in this context.
>
> How CAN I return something?
>
> By doing so from within a stored procedure. if you are not in a stored
> procedure, where are you returning to? PRINT instead, or SELECT, or issue
> your own RAISERROR().
>
> A
>
>|||> Shouldn't I see something different?
Why do you say that? Think about it:
The update is executed. It encounters an error on a row. The error is return
ed to the client
application.
The IF statement is executed and the condition is true
The RETURN statement is executed, returning the value 123 to the calling pro
cedure/application.
You don't see the return value because no-one is catching and doing anything
with that value.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Roger Twomey" <rogerdev@.vnet.on.ca> wrote in message news:OctDzvvYGHA.4060@.
TK2MSFTNGP02.phx.gbl...
Here is my code:
CREATE PROCEDURE dbo.prTest
AS
UPDATE authors SET au_id = '172 32 1176'
WHERE au_id = '172-32-1176'
IF @.@.ERROR = 547
Begin
return 123
End
select 'fred'
go
When I run the proc I get this:
Server: Msg 547, Level 16, State 1, Procedure prTest, Line 4
UPDATE statement conflicted with COLUMN CHECK constraint 'CK__authors__au_id
__0CBAE877'. The
conflict occurred in database 'Pubs', table 'authors', column 'au_id'.
The statement has been terminated.
This is exactly the same result I get if there is no @.@.Error code. I don't s
ee how this error
checking code did anything?
Shouldn't I see something different?
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@.dnartreb.noraa> wrote in message
news:u0ZJrGvYGHA.4760@.TK2MSFTNGP03.phx.gbl...
> Server: Msg 178, Level 15, State 1, Line 7
> A RETURN statement with a return value cannot be used in this context.
> How CAN I return something?
> By doing so from within a stored procedure. if you are not in a stored
> procedure, where are you returning to? PRINT instead, or SELECT, or issue
> your own RAISERROR().
> A
>|||When executing the stored procedure you must declare (and use) a variable to
assign the return value from the procedure to:
declare @.r int
exec @.r = <procedure> <parameter list>
print @.r
What exactly are you trying to achieve?
ML
http://milambda.blogspot.com/|||Well... At this point I am trying to achieve the Zen state of knowlege. ;)
I am trying to figure out how this works so that I might be able to apply it
to something in the future.
Currently a huge amount of my .net code is error checking. Because I have
never been able to figure out the @.@.Error in SQL server I rely on my .Net
code to figure out what happened and fix it.
I think it would be ever so much easier if I understood @.@.Error use so that
I could take care of some of this in SQL itself. The sample I gave you is
just me trying to figure out what the heck is happening.
Okay. Executing the way you suggested worked. Now I just have to figure out
how I can use that knowlege.
For example, could my variable use to hold the error state be an output
param? I know how to get those from vb.net.
It's amazing the things I don't know! ;)
Thanks for your help.
Anyway, I will try
"ML" <ML@.discussions.microsoft.com> wrote in message
news:66BD512F-C1AE-4CD8-8C40-575153F30B9C@.microsoft.com...
> When executing the stored procedure you must declare (and use) a variable
> to
> assign the return value from the procedure to:
> declare @.r int
> exec @.r = <procedure> <parameter list>
> print @.r
> What exactly are you trying to achieve?
>
> ML
> --
> http://milambda.blogspot.com/|||As suggested before, read through Erland's articles. That will give you the
basics and a few good practices.
There are three ways a procedure can return data:
1) a result set (one or more) - the results of select statements (with the
exception of variable assigning selects);
2) output parameters - declared as output parameters when the procedure is
dreated and when the procedure is executed; and
3) the return value.
The latter is usually used for just the purpose of either returning 0 when
the procedure was executed without any errors, or returning a SQL Error
number (look at the sysmessages system table) or a user-defined numeric valu
e
that can be trapped in the calling process (e.g a user error could be the
fact that no rows were affected by an update statement).
But the essence of error-handling is IMHO:
1) to *prevent* possible errors (by validating inputs); and
2) to report fatal (unrecoverable) errors to the caller.
You mention the fact that you do most of your development work in .Net, so I
assume you are familiar with the TRY...CATCH method. If you need this
functionality, then you'd have to use SQL 2005, since it's not known in
previous versions.
What exactly is your goal here? We can help you find the best solution to
any problem, but we really need to know what it is that you need.
ML
p.s. the Zen model is the future. :)
http://milambda.blogspot.com/
No comments:
Post a Comment