Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Comments] Cheatsheet - SQL RAISERROR #6

Open
chadbaldwin opened this issue Jan 17, 2021 · 5 comments
Open

[Comments] Cheatsheet - SQL RAISERROR #6

chadbaldwin opened this issue Jan 17, 2021 · 5 comments
Labels

Comments

@chadbaldwin
Copy link
Owner

https://chadbaldwin.net/2021/01/15/raiserror-cheatsheet.html

@dantheother
Copy link

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
Copy link
Owner Author

chadbaldwin commented Jan 18, 2021

@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
Copy link
Owner Author

@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
Copy link

samot1 commented Jan 19, 2021

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
Copy link
Owner Author

@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
Labels
Projects
None yet
Development

No branches or pull requests

3 participants