{"id":65,"date":"2010-02-11T23:41:51","date_gmt":"2010-02-11T23:41:51","guid":{"rendered":"http:\/\/jcrawfor74.wordpress.com\/?p=65"},"modified":"2024-06-08T18:12:19","modified_gmt":"2024-06-08T08:12:19","slug":"sql-server-print-logging-in-tsql-stored-procedures","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2010\/02\/11\/sql-server-print-logging-in-tsql-stored-procedures\/","title":{"rendered":"SQL Server Print Logging in TSQL \/ Stored Procedures"},"content":{"rendered":"<div id=\"ntsbl-3029201446\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div>\n<p>In tsql when running stored procedures you may wish to output debug statements.<\/p>\n\n\n\n<p>The problem is that in a long running tsql the &#8220;Print&#8221; statements will not output until the entire proc has completed.<\/p>\n\n\n\n<p>The solution is to use the RAISERROR statement. This will force the output of the message immediately.<\/p>\n\n\n\n<p>You don&#8217;t want to actually raise an error so call it with a 0 error code, but it has the ability to specify &#8220;WITH NOWAIT&#8221; which outputs the message straight away.<\/p>\n\n\n\n<p>Combine this with my previous post and you can do this;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql line-numbers\">set @msg = dbo.udfGetDurationMsg('Task 1', @jobStart, getDate())\nRAISERROR(@msg, 0, 0) WITH NOWAIT<\/code><\/pre>\n\n\n\n<p>which would produce output like:<\/p>\n\n\n\n<p>Task 1 &#8211; 0mins 43sec<\/p>\n\n\n\n<p>Hope this helps<br>Cheers<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In tsql when running stored procedures you may wish to output debug statements. The problem is that in a long running tsql the &#8220;Print&#8221; statements [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-65","post","type-post","status-publish","format-standard","hentry","category-sql-server"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/65","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}