SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Quick Hints for using the RAISERROR Command


Quick Hints for using the RAISERROR Command

Author
Message
Richard Moldwin
Richard Moldwin
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 15

Noel,

Thanks. I have lots of practice!


BeerBeerBeer
BeerBeerBeer
Old Hand
Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)

Group: General Forum Members
Points: 383 Visits: 74
I like the concept but I generally take a slightly different approach.

Use of a logging table that is populated by a single stored procedure that takes in the descriptions and dates as supplied. You can use a control table to determine when to turn the debugging on or off and if you need to change the output message format, you can just change the one stored procedure. The stored procedure can also do the dirty work for you converting values. Write code once and your stored procs don't start to get too messy.

EXEC DebugSP @message, Paramlist



EXEC DebugSP @message, Paramlist


Regards,

Steve

Life without beer is no life at all

All beer is good, some beers are just better than others

BeerBeerBeer
BeerBeerBeer
Old Hand
Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)

Group: General Forum Members
Points: 383 Visits: 74
Oops, slight error in my example - lets try again


EXEC DebugSP @message, Paramlist

-- Statement block runs here

EXEC DebugSP @message, Paramlist


Regards,

Steve

Life without beer is no life at all

All beer is good, some beers are just better than others

Noel Kennedy
Noel Kennedy
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 57
Except of course this won't work if your code rolls back a transaction because your message records will be rolled back aswell
BeerBeerBeer
BeerBeerBeer
Old Hand
Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)

Group: General Forum Members
Points: 383 Visits: 74
Yes, good point.

Take the two pronged approach and use a RAISERROR inside the stored proc too. This way you get messages as they happen but also an easily searchable set if things don't go wrong.

And again, if you have the RAISERROR inside it's own stored proc, you only have to write it once.


Regards,

Steve

Life without beer is no life at all

All beer is good, some beers are just better than others

Noel Kennedy
Noel Kennedy
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 57
Have you got a script you could post for your 'DebugSP' proc? I am doing something now where this would be extremely useful? =)
BeerBeerBeer
BeerBeerBeer
Old Hand
Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)Old Hand (383 reputation)

Group: General Forum Members
Points: 383 Visits: 74
Here is a conceptual look at one

All you need to do is fill in the bits. I've borrowed some of the stuff Dave talked about in his article and just layed out the concept for you.

Apply your own standards and away you go.

CREATE PROCEDURE lsp_Debug
(
@dtStart DATETIME
, @dtEnd DATETIME
-- Variable list here
)
AS

DECLARE @ReturnCode INT

BEGIN
-- Set the default return value to success
SELECT @ReturnCode = 0

IF EXISTS ( SELECT
1
FROM
debug_control
WHERE
dbg_can_debug = 1
)

BEGIN

-- Calculate time elapsed if you have an end date as well as a start date

-- Send message to event log so even if a rollback occurs we can see how far we got
DECLARE @sStringVar VARCHAR(440)
, @lInteger INT
, @fFloat DECIMAL(6,2)
, @byUnsignedInt TINYINT

SELECT
@sStringVar = 'A string'
, @lInteger = 44
, @fFloat = 3.14
, @byUnsignedInt = 56

RAISERROR('The string variable contains %s, the int contains %d, the float contains %d, the uint contains %d'
, 10 -- Informational severity only.
, 1
, @sStringVar
, @lInteger
, @fFloat
, @byUnsignedInt
) WITH LOG
, NOWAIT

INSERT dbo.debug_log
(
-- Add apropriate columns here
)
SELECT
-- Select approapriate data here
FROM
-- Use from if you want to peek at data being processed

-- Check for any errors
SELECT @ReturnCode = @@ERROR

END
-- Return any errors found along the way
RETURN @ReturnCode

END
GO



Hope you find this useful, sorry I don't have more time to flesh it out for you.


Regards,

Steve

Life without beer is no life at all

All beer is good, some beers are just better than others

Noel Kennedy
Noel Kennedy
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 57
Thanks very much
mosaic-263591
mosaic-263591
SSC-Addicted
SSC-Addicted (440 reputation)SSC-Addicted (440 reputation)SSC-Addicted (440 reputation)SSC-Addicted (440 reputation)SSC-Addicted (440 reputation)SSC-Addicted (440 reputation)SSC-Addicted (440 reputation)SSC-Addicted (440 reputation)

Group: General Forum Members
Points: 440 Visits: 566

Thanks for a useful and clear article - much appreciated.

One comment: as an initial step the SQL profiler can also be useful - no coding required! Of course you cannot see how the parameters flow through the procedure, but the various statements it executes may give an indication of the program flow.


Patrick-304111
Patrick-304111
SSC Rookie
SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)SSC Rookie (28 reputation)

Group: General Forum Members
Points: 28 Visits: 81
excellect opps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search