Skip to content

[Comments] Cheatsheet - SQL RAISERROR #6

Open

Activity

dantheother

dantheother commented on Jan 18, 2021

@dantheother

Another cool feature of RAISERROR is you can capture the output from calling code if you're using an sqlConnection object. Here's an example in c# https://stackoverflow.com/a/23774727/11569, there's powershell examples out there too. Great way of getting messages to the console/ui (I guess console is a UI) for really long running stored procedures. Or for logging extra info about stored procedure executin.

chadbaldwin

chadbaldwin commented on Jan 18, 2021

@chadbaldwin
OwnerAuthor

@dantheother yup! It's awesome, that's probably my number 1 use for it. Every stored procedure I write is packed with them.

Messages get pushed to a buffer, but the buffer only occasionally gets flushed to output, and using NOWAIT will force a flush. I cover this at the top with a couple examples including substitution parameters to add a log date.

chadbaldwin

chadbaldwin commented on Jan 18, 2021

@chadbaldwin
OwnerAuthor

@dantheother I decided to add in a demo specifically for this, as it really is one of my favorite uses and should be included in this post. Thanks for the comment!

samot1

samot1 commented on Jan 19, 2021

@samot1

RAISERROR WITH NOWAIT runs into buffering problems too (but later than the usual PRINT).

See my question / demo at https://dba.stackexchange.com/questions/270729/ssms-sql-server-delays-console-output-raiseerror-with-nowait

Print starts to buffer after 40 lines, RAISERROR starts after 500 lines to buffer 50 lines.
So even RAISERROR is not the perfect solution for loops or much speaking stuff (imaging the use of Ola Hallengren's Maintenance procedures when you run eg. Index optimize over 100 databases with tons of tables each)

chadbaldwin

chadbaldwin commented on Jan 19, 2021

@chadbaldwin
OwnerAuthor

@samot1 Yup, I run into this limit all the time, but didn't feel the need to get into that level of detail as the 500 message cutoff, then switch to every 50 is sufficient for the majority of people. A few sections of this post probably could have been blog posts of their own, heh. The main goal is to encourage developers to improve the output on their code to aide readability and monitoring progress, there will always be outliers.

This is also the reason I recommended the interval option by only outputting messages in intervals rather than for every iteration of the loop.

That's a nice demo btw, I like that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      [Comments] Cheatsheet - SQL RAISERROR · Issue #6 · chadbaldwin/chadbaldwin.github.io