<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SQL Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/sql/</link>
	<description>Building real-world software with AI 🤖</description>
	<lastBuildDate>Tue, 17 May 2022 05:40:05 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.2</generator>

<image>
	<url>https://i0.wp.com/www.codejourney.net/wp-content/uploads/2018/10/cropped-512px-na-512px-JPEG-BEZ-NAPISU-1.jpg?fit=32%2C32&#038;ssl=1</url>
	<title>SQL Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/sql/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<item>
		<title>How To Fix SqlException Database Is Not Currently Available On Azure (0x80131904)</title>
		<link>https://www.codejourney.net/how-to-fix-sqlexception-database-is-not-currently-available-on-azure-0x80131904/</link>
					<comments>https://www.codejourney.net/how-to-fix-sqlexception-database-is-not-currently-available-on-azure-0x80131904/#respond</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Tue, 17 May 2022 05:32:15 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[azure]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[microsoft azure]]></category>
		<category><![CDATA[nhibernate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<guid isPermaLink="false">https://www.codejourney.net/?p=4036</guid>

					<description><![CDATA[<p>I recently deployed my .NET 6 application to Microsoft Azure. The app uses NHibernate for working with SQL Server database. After some time, I started getting an unhandled exception when opening the application: The database is not currently available was very interesting to me. I struggled a bit with solving that, so if you want&#8230;</p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-sqlexception-database-is-not-currently-available-on-azure-0x80131904/">How To Fix SqlException Database Is Not Currently Available On Azure (0x80131904)</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">I recently deployed my .NET 6 application to Microsoft Azure. The app uses <a href="https://nhibernate.info/">NHibernate</a> for working with SQL Server database. After some time, I started getting an unhandled exception when opening the application:</p>



<pre class="wp-block-code"><code>NHibernate.Exceptions.GenericADOException: could not load an entity: 
...
 ---&gt; Microsoft.Data.SqlClient.SqlException (0x80131904): <strong>Database 'myappdb' on server 'myapp.database.windows.net' is not currently available</strong>.  Please retry the connection later.  If the problem persists, contact customer support, and provide them the session tracing ID of '{EB501CF2-2F21-4E28-9042-2B83EEE57B91}'.</code></pre>



<p class="wp-block-paragraph">The <em>database is</em> <em>not currently available </em>was very interesting to me. I struggled a bit with solving that, so if you want to know how I did it &#8211; continue reading <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>



<span id="more-4036"></span>



<h2 class="wp-block-heading">The problem</h2>



<p class="wp-block-paragraph">My .NET 6 app is an <a href="https://azure.microsoft.com/en-us/services/app-service/">Azure App Service</a>, and it uses SQL Server database. Quite standard Azure stuff.</p>



<p class="wp-block-paragraph">The issue was that when opening the ASP.NET web app after some time of inactivity, there was an HTTP 500 error. When I checked the logs, the full error looked as follows:</p>



<pre class="wp-block-code"><code><strong>NHibernate.Exceptions.GenericADOException: could not load an entity</strong>: &#91;MyApp.Infrastructure.DomainObjects.ApplicationUser#37931c5a376748d7a49f215ca30283a2]&#91;SQL: SELECT applicatio0_.applicationuser_key as id1_3_0_, applicatio0_1_.UserName as username2_3_0_, applicatio0_1_.NormalizedUserName as normalizedusername3_3_0_, applicatio0_1_.Email as email4_3_0_, applicatio0_1_.NormalizedEmail as normalizedemail5_3_0_, applicatio0_1_.EmailConfirmed as emailconfirmed6_3_0_, applicatio0_1_.PhoneNumber as phonenumber7_3_0_, applicatio0_1_.PhoneNumberConfirmed as phonenumberconfirmed8_3_0_, applicatio0_1_.LockoutEnabled as lockoutenabled9_3_0_, applicatio0_1_.LockoutEnd as lockoutend10_3_0_, applicatio0_1_.AccessFailedCount as accessfailedcount11_3_0_, applicatio0_1_.ConcurrencyStamp as concurrencystamp12_3_0_, applicatio0_1_.PasswordHash as passwordhash13_3_0_, applicatio0_1_.TwoFactorEnabled as twofactorenabled14_3_0_, applicatio0_1_.SecurityStamp as securitystamp15_3_0_ FROM ApplicationUsers applicatio0_ inner join dbo.AspNetUsers applicatio0_1_ on applicatio0_.applicationuser_key=applicatio0_1_.Id WHERE applicatio0_.applicationuser_key=?]
 ---&gt; Microsoft.Data.SqlClient.SqlException (0x80131904): <strong>Database 'myappdb' on server 'myapp.database.windows.net' is not currently available</strong>.  Please retry the connection later.  If the problem persists, contact customer support, and provide them the session tracing ID of '{EB501CF2-2F21-4E28-2342-2B836E35E891}'.
   at Microsoft.Data.ProviderBase.DbConnectionPool.CheckPoolBlockingPeriod(Exception e)</code></pre>



<p class="wp-block-paragraph">There were two characteristics of this error:</p>



<ul class="wp-block-list"><li>it always appeared after some time of not using the web app</li><li>I never reproduced it locally</li></ul>



<p class="wp-block-paragraph">So it must have been something wrong with Azure.</p>



<h2 class="wp-block-heading">Transient issues in Azure databases</h2>



<p class="wp-block-paragraph">First thing I did, obviously, was checking Azure App Service logs. This is what I found:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" fetchpriority="high" decoding="async" width="1024" height="288" data-attachment-id="4053" data-permalink="https://www.codejourney.net/azure_database_transient_problems/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_database_transient_problems.png?fit=1482%2C417&amp;ssl=1" data-orig-size="1482,417" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Azure_database_transient_problems" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_database_transient_problems.png?fit=1024%2C288&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_database_transient_problems.png?resize=1024%2C288&#038;ssl=1" alt="Error log when database is not currently available error occured in Azure" class="wp-image-4053" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_database_transient_problems.png?resize=1024%2C288&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_database_transient_problems.png?resize=300%2C84&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_database_transient_problems.png?resize=768%2C216&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_database_transient_problems.png?w=1482&amp;ssl=1 1482w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="wp-block-paragraph">The first recommended step got me curious. <em>To reduce the impact of connection issues caused by future reconfiguration, <strong>please implement retry logic in your code</strong></em>.</p>



<p class="wp-block-paragraph">So I started reading. There&#8217;s even <a href="https://docs.microsoft.com/en-us/aspnet/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps-with-windows-azure/transient-fault-handling">official Microsoft documentation for that</a>. It basically says that <em>in the cloud environment you&#8217;ll find that failed and dropped database connections happen periodically.</em></p>



<p class="wp-block-paragraph">I must admit it seemed quite new to me. However, I started digging into that. I used <a href="https://github.com/App-vNext/Polly">Polly</a> to implement the retry logic in my app. I found few places where there was an NHibernate query for fetching users executed and wrapped it into the retry policy.</p>



<p class="wp-block-paragraph">Nothing worked. I was still getting the error. Then, after some talks with my coworkers, we found the real reason behind that&#8230;</p>



<h2 class="wp-block-heading">Solution: Azure database tier</h2>



<p class="wp-block-paragraph">When talking about the issue with my teammates, someone mentioned that the databases in Azure are <em>serverless</em> by default.</p>



<p class="wp-block-paragraph">I logged into the <a href="https://portal.azure.com/">Azure Portal</a> and checked the &#8220;Compute + storage&#8221; settings my SQL database my app used:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" width="1024" height="382" data-attachment-id="4055" data-permalink="https://www.codejourney.net/azure_tier_settings_serverless/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_serverless.png?fit=1193%2C445&amp;ssl=1" data-orig-size="1193,445" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Azure_tier_settings_serverless" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_serverless.png?fit=1024%2C382&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_serverless.png?resize=1024%2C382&#038;ssl=1" alt="Azure Portal and Service and compute tier settings of SQL Database set to General Purpose. Reason for database is not currently available error" class="wp-image-4055" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_serverless.png?resize=1024%2C382&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_serverless.png?resize=300%2C112&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_serverless.png?resize=768%2C286&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_serverless.png?w=1193&amp;ssl=1 1193w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="wp-block-paragraph">Indeed, the tier selected was &#8220;General purpose (Scalable compute and storage options)&#8221; with &#8220;Serverless&#8221; compute tier selected.</p>



<p class="wp-block-paragraph">I changed it to &#8220;Basic (For less demanding workloads)&#8221;:</p>



<div class="wp-block-image"><figure class="aligncenter size-large"><img data-recalc-dims="1" decoding="async" width="1024" height="507" data-attachment-id="4056" data-permalink="https://www.codejourney.net/how-to-fix-sqlexception-database-is-not-currently-available-on-azure-0x80131904/azure_tier_settings_basic/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_basic.png?fit=1188%2C588&amp;ssl=1" data-orig-size="1188,588" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="Azure_tier_settings_basic" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_basic.png?fit=1024%2C507&amp;ssl=1" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_basic.png?resize=1024%2C507&#038;ssl=1" alt="Azure Portal and tier of the SQL database changed to Basic. It made database is not currently available error disappear" class="wp-image-4056" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_basic.png?resize=1024%2C507&amp;ssl=1 1024w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_basic.png?resize=300%2C148&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_basic.png?resize=768%2C380&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2022/05/Azure_tier_settings_basic.png?w=1188&amp;ssl=1 1188w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure></div>



<p class="wp-block-paragraph">and&#8230; it worked <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f92f.png" alt="🤯" class="wp-smiley" style="height: 1em; max-height: 1em;" /> All errors saying that the database is not currently available disappeared. I also removed all Polly-related code for retry policies.</p>



<h2 class="wp-block-heading">A little explanation</h2>



<p class="wp-block-paragraph">Why do serverless databases in Azure behave like that?</p>



<p class="wp-block-paragraph">I finally found this explanation in the <a href="https://docs.microsoft.com/en-us/azure/azure-sql/database/serverless-tier-overview?view=azuresql">Microsoft documentation</a>:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>If a serverless database is paused, then <strong>the first login will resume the database and return an error stating that the database is unavailable with error code 40613</strong>. Once the database is resumed, the login must be retried to establish connectivity</p></blockquote>



<p class="wp-block-paragraph">That blew my mind! It means that the <em>database is not currently available</em> exception is expected. There&#8217;s also another part there:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Database clients with connection retry logic should not need to be modified. For connection retry logic options that are built-in to the SqlClient driver, see&nbsp;<a href="https://docs.microsoft.com/en-us/sql/connect/ado-net/configurable-retry-logic">configurable retry logic in SqlClient</a>.</p></blockquote>



<p class="wp-block-paragraph">Apparently, users of ORMs like Entity Framework <a href="https://docs.microsoft.com/en-us/ef/ef6/fundamentals/connection-resiliency/retry-logic">should not have to worry about those issues</a>. Unfortunately, NHibernate doesn&#8217;t have such built-in transient errors retry logic. That&#8217;s why I experienced this particular issue and couldn&#8217;t find any straightforward solution online. Simply not many people (if anyone at all? <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f601.png" alt="😁" class="wp-smiley" style="height: 1em; max-height: 1em;" />) experienced this before.</p>



<p class="wp-block-paragraph">If you want even more details and context, check out this article: <a href="https://kohera.be/azure-cloud/should-i-use-serverless-for-all-my-azure-sql-databases/">SHOULD I USE SERVERLESS FOR ALL MY AZURE SQL DATABASES?</a></p>



<h2 class="wp-block-heading">What if I want to use serverless Azure database?</h2>



<p class="wp-block-paragraph">Well&#8230; if you really want to use the serverless features of Azure, you might be struggling with NHibernate. As I mentioned, apparently there&#8217;s no built-in transient failures retry mechanism in the ORM. There&#8217;s one promising solution <a href="https://github.com/MRCollective/NHibernate.SqlAzure">here</a>. However, currently it&#8217;s build for NHibernate 3.3.1.4000, so it doesn&#8217;t work with the current version. There&#8217;s a <a href="https://github.com/MRCollective/NHibernate.SqlAzure/pull/37">Pull Request</a> and an <a href="https://github.com/MRCollective/NHibernate.SqlAzure/issues/32">issue</a> opened for upgrading it to the newest version and .NET 6/7. Let&#8217;s hope it is done soon.</p>



<p class="wp-block-paragraph">Otherwise, you can try using <a href="https://www.codejourney.net/2022/05/how-to-fix-sqlexception-database-is-not-currently-available-on-azure-0x80131904/">Polly</a> as I did initially. However, I didn&#8217;t manage to make fully working.</p>



<p class="wp-block-paragraph">If you have any other idea or solution &#8211; let us know in the comments below.</p>



<h2 class="wp-block-heading">Conclusion</h2>



<p class="wp-block-paragraph">I hope this article saves you some hours of debugging, so you can quickly solve this <em>database is not currently available</em> error in Azure. I wish I found this article on another website few weeks ago <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<p>The post <a href="https://www.codejourney.net/how-to-fix-sqlexception-database-is-not-currently-available-on-azure-0x80131904/">How To Fix SqlException Database Is Not Currently Available On Azure (0x80131904)</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/how-to-fix-sqlexception-database-is-not-currently-available-on-azure-0x80131904/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">4036</post-id>	</item>
		<item>
		<title>T-SQL/SSMS: transaction rollback in scripts with XACT_ABORT ON, GO statements and syntax errors</title>
		<link>https://www.codejourney.net/t-sqlssms-transaction-rollback-in-scripts-with-xact_abort-on-go-statements-and-syntax-errors/</link>
					<comments>https://www.codejourney.net/t-sqlssms-transaction-rollback-in-scripts-with-xact_abort-on-go-statements-and-syntax-errors/#comments</comments>
		
		<dc:creator><![CDATA[Dawid Sibiński]]></dc:creator>
		<pubDate>Thu, 16 Nov 2017 07:00:40 +0000</pubDate>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<guid isPermaLink="false">http://www.dsibinski.pl/?p=2425</guid>

					<description><![CDATA[<p>I&#8217;ve recently met a weird issue with T-SQL scripts at work and would like to share it with you today 🙂 T-SQL script with multiple objects created On daily basis I work a lot with MS SQL Server databases. We often create many T-SQL objects (tables, views, procedures, functions) and because of some reasons we&#8230;</p>
<p>The post <a href="https://www.codejourney.net/t-sqlssms-transaction-rollback-in-scripts-with-xact_abort-on-go-statements-and-syntax-errors/">T-SQL/SSMS: transaction rollback in scripts with XACT_ABORT ON, GO statements and syntax errors</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ve recently met a weird issue with T-SQL scripts at work and would like to share it with you today <img src="https://s.w.org/images/core/emoji/17.0.2/72x72/1f642.png" alt="🙂" class="wp-smiley" style="height: 1em; max-height: 1em;" /><br />
<span id="more-2425"></span></p>
<h1>T-SQL script with multiple objects created</h1>
<p>On daily basis I work a lot with MS SQL Server databases. We often create many T-SQL objects (tables, views, procedures, functions) and because of some reasons we cannot use Entity Framework or another from widely available ORMs. Nonetheless, all objects created in the database must be kept in the form of SQL scripts (files) containing set of <em>CREATE</em>, <em>ALTER</em>, <em>INSERT</em>, <em>DELETE</em> or whatever T-SQL statements.</p>
<p>What we often do is to create a single <em>.sql</em> file, which in fact often contains more than one, separate (independent) SQL statements (e.g. creates a table and a procedure). What we obviously want to ensure is that when executing the script either all statements are committed to the database or none of them. This means that if in a part responsible for creating a particular object any SQL error is raised, execution of the whole script should be interrupted and the whole transaction rolled back, so in effect none of the objects contained within this script are created (none of the statements batches is committed). Here the issue comes out.</p>
<h1><span title="Hint: double-click to select code">SQL script with XACT_ABORT</span><span title="Hint: double-click to select code"> </span><span title="Hint: double-click to select code">ON and GO statements</span></h1>
<p>In order to handle above-described requirements, the template for SQL script looks as follows:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist83104201" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-sql_template-sql" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-tsql  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="SQL_template.sql content, created by dsibinski on 08:32PM on November 15, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="SQL_template.sql">
<tr>
<td id="file-sql_template-sql-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-sql_template-sql-LC1" class="blob-code blob-code-inner js-file-line">  SET XACT_ABORT ON</td>
</tr>
<tr>
<td id="file-sql_template-sql-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-sql_template-sql-LC2" class="blob-code blob-code-inner js-file-line">  GO</td>
</tr>
<tr>
<td id="file-sql_template-sql-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-sql_template-sql-LC3" class="blob-code blob-code-inner js-file-line">  BEGIN TRANSACTION</td>
</tr>
<tr>
<td id="file-sql_template-sql-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-sql_template-sql-LC4" class="blob-code blob-code-inner js-file-line">  GO</td>
</tr>
<tr>
<td id="file-sql_template-sql-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-sql_template-sql-LC5" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-sql_template-sql-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-sql_template-sql-LC6" class="blob-code blob-code-inner js-file-line">  &#8212; SQL object 1 (CREATE/ALTER/INSERT etc&#8230;)</td>
</tr>
<tr>
<td id="file-sql_template-sql-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-sql_template-sql-LC7" class="blob-code blob-code-inner js-file-line">  GO</td>
</tr>
<tr>
<td id="file-sql_template-sql-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-sql_template-sql-LC8" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-sql_template-sql-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-sql_template-sql-LC9" class="blob-code blob-code-inner js-file-line">  &#8212; SQL object 2 (CREATE/ALTER/INSERT etc&#8230;)</td>
</tr>
<tr>
<td id="file-sql_template-sql-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-sql_template-sql-LC10" class="blob-code blob-code-inner js-file-line">  GO</td>
</tr>
<tr>
<td id="file-sql_template-sql-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-sql_template-sql-LC11" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-sql_template-sql-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-sql_template-sql-LC12" class="blob-code blob-code-inner js-file-line">  &#8212; next SQL batches if needed, each followed by GO</td>
</tr>
<tr>
<td id="file-sql_template-sql-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-sql_template-sql-LC13" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-sql_template-sql-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-sql_template-sql-LC14" class="blob-code blob-code-inner js-file-line">  COMMIT</td>
</tr>
<tr>
<td id="file-sql_template-sql-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-sql_template-sql-LC15" class="blob-code blob-code-inner js-file-line">  GO</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/39e47705e1d6d5f16c7a45badab543c8/raw/e6739f6192abc2e6b3b9226f01de3ee1e622f058/SQL_template.sql" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/39e47705e1d6d5f16c7a45badab543c8#file-sql_template-sql" class="Link--inTextBlock"><br />
          SQL_template.sql<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Firstly, we set <em>XACT_ABORT</em> to <em><span class="pl-k">ON. </span></em><span class="pl-k">This setting, <a href="https://docs.microsoft.com/en-us/sql/t-sql/statements/set-xact-abort-transact-sql" target="_blank" rel="noopener">according to Microsoft docs</a>, &#8220;specifies whether SQL Server automatically rolls back the current transaction when a Transact-SQL statement raises a run-time error&#8221;. That&#8217;s what we want &#8211; in case of any errors the whole opened transaction should be rolled back.</p>
<p></span></p>
<p>Then we open a transaction with <em>BEGIN TRANSACTION, </em>which is committed to the server at the end of the script with <em>COMMIT</em> statement.</p>
<p>In between, we put the actual SQL statements that are responsible for creating new SQL objects or sometimes executing some procedure, inserting some data etc.</p>
<p>As can be noticed, each of these SQL &#8220;chunks&#8221; is followed by a <em>GO</em> statement. <em>GO</em> is one of the SQL Server statements, that are not T-SQL statements, but are recognized by the <strong class="">sqlcmd</strong> utility and SQL Server Management Studio (SSMS). <em>GO</em> statement allows to separate a script into <em>batches</em>. When <em>GO</em> is used, it signals the end of a batch and sends it to the SQL Server. It is necessary in order to separate T-SQL statements (e.g. to separate a procedure&#8217;s creation part from another procedure&#8217;s <em>EXEC</em> statement coming just after). That is also what comes in default in scripts generated by SSMS.</p>
<p>Of course, everything is in one, big transaction, so everything will be ok, right? When there is any error in any of the batches all other statements will be rolled back? Not necessarily!</p>
<h1><span title="Hint: double-click to select code">XACT_ABORT</span><span title="Hint: double-click to select code"> </span><span title="Hint: double-click to select code">ON and syntax errors</span></h1>
<p>If we take a closer look at the above-cited description of <em>XACT_ABORT</em> from the Microsoft docs, it says that with this setting set to <em>ON</em>, transaction is rolled back &#8220;<span class="pl-k">when a Transact-SQL statement raises a <strong>run-time error</strong>&#8220;. <strong>Bold</strong> part is crucial here &#8211; it only works for <strong>run-time</strong> errors.</p>
<p></span></p>
<p>Let&#8217;s consider the following sample .sql script:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist83104663" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-peopletable_proc_exec-sql" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-tsql  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="PeopleTable_proc_exec.sql content, created by dsibinski on 08:48PM on November 15, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="PeopleTable_proc_exec.sql">
<tr>
<td id="file-peopletable_proc_exec-sql-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-peopletable_proc_exec-sql-LC1" class="blob-code blob-code-inner js-file-line">   SET xact_abort ON </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-peopletable_proc_exec-sql-LC2" class="blob-code blob-code-inner js-file-line">   GO </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-peopletable_proc_exec-sql-LC3" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-peopletable_proc_exec-sql-LC4" class="blob-code blob-code-inner js-file-line">   BEGIN TRANSACTION </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-peopletable_proc_exec-sql-LC5" class="blob-code blob-code-inner js-file-line">   GO </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-peopletable_proc_exec-sql-LC6" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-peopletable_proc_exec-sql-LC7" class="blob-code blob-code-inner js-file-line">   CREATE TABLE [dbo].[People] </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-peopletable_proc_exec-sql-LC8" class="blob-code blob-code-inner js-file-line">     ( </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-peopletable_proc_exec-sql-LC9" class="blob-code blob-code-inner js-file-line">        [id]        [INT] IDENTITY(1, 1) NOT NULL, </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-peopletable_proc_exec-sql-LC10" class="blob-code blob-code-inner js-file-line">        [name]      [NVARCHAR](max) NOT NULL, </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L11" class="blob-num js-line-number js-blob-rnum" data-line-number="11"></td>
<td id="file-peopletable_proc_exec-sql-LC11" class="blob-code blob-code-inner js-file-line">        [timestamp] [TIMESTAMP] NOT NULL, </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L12" class="blob-num js-line-number js-blob-rnum" data-line-number="12"></td>
<td id="file-peopletable_proc_exec-sql-LC12" class="blob-code blob-code-inner js-file-line">        CONSTRAINT [PK_People] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (pad_index = </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L13" class="blob-num js-line-number js-blob-rnum" data-line-number="13"></td>
<td id="file-peopletable_proc_exec-sql-LC13" class="blob-code blob-code-inner js-file-line">        OFF, statistics_norecompute = OFF, ignore_dup_key = OFF, allow_row_locks = </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L14" class="blob-num js-line-number js-blob-rnum" data-line-number="14"></td>
<td id="file-peopletable_proc_exec-sql-LC14" class="blob-code blob-code-inner js-file-line">        on, allow_page_locks = on) ON [PRIMARY] </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L15" class="blob-num js-line-number js-blob-rnum" data-line-number="15"></td>
<td id="file-peopletable_proc_exec-sql-LC15" class="blob-code blob-code-inner js-file-line">     ) </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L16" class="blob-num js-line-number js-blob-rnum" data-line-number="16"></td>
<td id="file-peopletable_proc_exec-sql-LC16" class="blob-code blob-code-inner js-file-line">   ON [PRIMARY] </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L17" class="blob-num js-line-number js-blob-rnum" data-line-number="17"></td>
<td id="file-peopletable_proc_exec-sql-LC17" class="blob-code blob-code-inner js-file-line">   textimage_on [PRIMARY] </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L18" class="blob-num js-line-number js-blob-rnum" data-line-number="18"></td>
<td id="file-peopletable_proc_exec-sql-LC18" class="blob-code blob-code-inner js-file-line">   GO </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L19" class="blob-num js-line-number js-blob-rnum" data-line-number="19"></td>
<td id="file-peopletable_proc_exec-sql-LC19" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L20" class="blob-num js-line-number js-blob-rnum" data-line-number="20"></td>
<td id="file-peopletable_proc_exec-sql-LC20" class="blob-code blob-code-inner js-file-line">   CREATE PROCEDURE [dbo].[InsertPerson] @name NVARCHAR(max) </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L21" class="blob-num js-line-number js-blob-rnum" data-line-number="21"></td>
<td id="file-peopletable_proc_exec-sql-LC21" class="blob-code blob-code-inner js-file-line">   AS </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L22" class="blob-num js-line-number js-blob-rnum" data-line-number="22"></td>
<td id="file-peopletable_proc_exec-sql-LC22" class="blob-code blob-code-inner js-file-line">     BEGIN </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L23" class="blob-num js-line-number js-blob-rnum" data-line-number="23"></td>
<td id="file-peopletable_proc_exec-sql-LC23" class="blob-code blob-code-inner js-file-line">         INSERT INTO dbo.People </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L24" class="blob-num js-line-number js-blob-rnum" data-line-number="24"></td>
<td id="file-peopletable_proc_exec-sql-LC24" class="blob-code blob-code-inner js-file-line">                     (NAME) </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L25" class="blob-num js-line-number js-blob-rnum" data-line-number="25"></td>
<td id="file-peopletable_proc_exec-sql-LC25" class="blob-code blob-code-inner js-file-line">         VALUES      (@name); </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L26" class="blob-num js-line-number js-blob-rnum" data-line-number="26"></td>
<td id="file-peopletable_proc_exec-sql-LC26" class="blob-code blob-code-inner js-file-line">     END </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L27" class="blob-num js-line-number js-blob-rnum" data-line-number="27"></td>
<td id="file-peopletable_proc_exec-sql-LC27" class="blob-code blob-code-inner js-file-line">   GO </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L28" class="blob-num js-line-number js-blob-rnum" data-line-number="28"></td>
<td id="file-peopletable_proc_exec-sql-LC28" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L29" class="blob-num js-line-number js-blob-rnum" data-line-number="29"></td>
<td id="file-peopletable_proc_exec-sql-LC29" class="blob-code blob-code-inner js-file-line">   INSERT INTO dbo.Persons &#8212; run-time error &#8211; dbo.Persons table does not exist</td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L30" class="blob-num js-line-number js-blob-rnum" data-line-number="30"></td>
<td id="file-peopletable_proc_exec-sql-LC30" class="blob-code blob-code-inner js-file-line">               (NAME) </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L31" class="blob-num js-line-number js-blob-rnum" data-line-number="31"></td>
<td id="file-peopletable_proc_exec-sql-LC31" class="blob-code blob-code-inner js-file-line">   VALUES      (&#39;Dawid&#39;) </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L32" class="blob-num js-line-number js-blob-rnum" data-line-number="32"></td>
<td id="file-peopletable_proc_exec-sql-LC32" class="blob-code blob-code-inner js-file-line">   GO </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L33" class="blob-num js-line-number js-blob-rnum" data-line-number="33"></td>
<td id="file-peopletable_proc_exec-sql-LC33" class="blob-code blob-code-inner js-file-line">
</td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L34" class="blob-num js-line-number js-blob-rnum" data-line-number="34"></td>
<td id="file-peopletable_proc_exec-sql-LC34" class="blob-code blob-code-inner js-file-line">   COMMIT </td>
</tr>
<tr>
<td id="file-peopletable_proc_exec-sql-L35" class="blob-num js-line-number js-blob-rnum" data-line-number="35"></td>
<td id="file-peopletable_proc_exec-sql-LC35" class="blob-code blob-code-inner js-file-line">   GO  </td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/b265d5a9494407df6049d5decb3c02b6/raw/61c87d8c591b9dbd3dd73daabbed70caae518f4e/PeopleTable_proc_exec.sql" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/b265d5a9494407df6049d5decb3c02b6#file-peopletable_proc_exec-sql" class="Link--inTextBlock"><br />
          PeopleTable_proc_exec.sql<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Here we have 3 T-SQL batches:</p>
<ul>
<li>Lines 7-17 contain a table&#8217;s creation statement</li>
<li>Lines 20-26 contain a procedure&#8217;s creation statement</li>
<li>Lines 29-31 contains an <em>INSERT</em> statement.
</li>
</ul>
<p>The last batch contains run-time error &#8211; there is no table <em>dbo.Persons </em>in the db. Execution of the above script in SSMS returns the following error:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist83107118" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-tsql_runtime_error-log" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-text  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="TSQL_runtime_error.log content, created by dsibinski on 10:06PM on November 15, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="TSQL_runtime_error.log">
<tr>
<td id="file-tsql_runtime_error-log-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-tsql_runtime_error-log-LC1" class="blob-code blob-code-inner js-file-line">  Msg 208, Level 16, State 1, Line 33</td>
</tr>
<tr>
<td id="file-tsql_runtime_error-log-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-tsql_runtime_error-log-LC2" class="blob-code blob-code-inner js-file-line">  Invalid object name &#39;dbo.Persons&#39;.</td>
</tr>
<tr>
<td id="file-tsql_runtime_error-log-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-tsql_runtime_error-log-LC3" class="blob-code blob-code-inner js-file-line">  Msg 3902, Level 16, State 1, Line 39</td>
</tr>
<tr>
<td id="file-tsql_runtime_error-log-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-tsql_runtime_error-log-LC4" class="blob-code blob-code-inner js-file-line">  The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/3f22670393358554f4847fc6523bf947/raw/5a80154af2055976e92ce58440256199845bddb7/TSQL_runtime_error.log" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/3f22670393358554f4847fc6523bf947#file-tsql_runtime_error-log" class="Link--inTextBlock"><br />
          TSQL_runtime_error.log<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>What we expect to happen is that because the last batch produced run-time error, the table and the procedure will not be created. <strong>And exactly this happens &#8211; neither the table nor the procedure is created in the database</strong>. <em>SET XACT_ABORT ON </em>did its job. Uff.</p>
<p>Now, let&#8217;s modify the last, <em>INSERT</em> statement by introducing a <strong>syntax error</strong> so it looks as follows:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist83105125" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-insert_with_syntax_error-sql" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-sql  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="Insert_with_syntax_error.sql content, created by dsibinski on 09:03PM on November 15, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="Insert_with_syntax_error.sql">
<tr>
<td id="file-insert_with_syntax_error-sql-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-insert_with_syntax_error-sql-LC1" class="blob-code blob-code-inner js-file-line">   INssSERT INTO dbo.Persons &#8212; syntax error &#8211; &#39;INssSERT&#39; instead of &#39;INSERT&#39;</td>
</tr>
<tr>
<td id="file-insert_with_syntax_error-sql-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-insert_with_syntax_error-sql-LC2" class="blob-code blob-code-inner js-file-line">               (NAME) </td>
</tr>
<tr>
<td id="file-insert_with_syntax_error-sql-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-insert_with_syntax_error-sql-LC3" class="blob-code blob-code-inner js-file-line">   VALUES      (&#39;Dawid&#39;) </td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/476478318a479a951f29b7f1f59af7da/raw/df392d86b509895d5a4cb16d83a1268b03edd2e7/Insert_with_syntax_error.sql" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/476478318a479a951f29b7f1f59af7da#file-insert_with_syntax_error-sql" class="Link--inTextBlock"><br />
          Insert_with_syntax_error.sql<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>Full .sql script with this change <a href="https://gist.github.com/dsibinski/aa95f17a96d704a499123816c1dc42a0" target="_blank" rel="noopener">is available here</a>.</p>
<p>Does it change a lot? We will still get an error during the script&#8217;s execution, right? Yes &#8211; we will get the following error:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist83107142" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-tsql_syntax_error-log" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-text  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="TSQL_syntax_error.log content, created by dsibinski on 10:06PM on November 15, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="TSQL_syntax_error.log">
<tr>
<td id="file-tsql_syntax_error-log-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-tsql_syntax_error-log-LC1" class="blob-code blob-code-inner js-file-line">   Msg 156, Level 15, State 1, Line 29</td>
</tr>
<tr>
<td id="file-tsql_syntax_error-log-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-tsql_syntax_error-log-LC2" class="blob-code blob-code-inner js-file-line">   Incorrect syntax near the keyword &#39;INTO&#39;.</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/42cc11d46141969b5530a03ab22a5c42/raw/cf9b38c9bb8f4b590ad7fc8be3eeaeef331baaf0/TSQL_syntax_error.log" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/42cc11d46141969b5530a03ab22a5c42#file-tsql_syntax_error-log" class="Link--inTextBlock"><br />
          TSQL_syntax_error.log<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>However, surprisingly, the table and the procedure <strong>are well-created in the database</strong>! The following screenshot presents:</p>
<p><figure id="attachment_2430" aria-describedby="caption-attachment-2430" style="width: 560px" class="wp-caption aligncenter"><a href="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?ssl=1" target="_blank" rel="noopener"><img data-recalc-dims="1" loading="lazy" decoding="async" data-attachment-id="2430" data-permalink="https://www.codejourney.net/t-sqlssms-transaction-rollback-in-scripts-with-xact_abort-on-go-statements-and-syntax-errors/ssms_objectscreated/" data-orig-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?fit=831%2C377&amp;ssl=1" data-orig-size="831,377" data-comments-opened="1" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" data-image-title="SSMS_ObjectsCreated" data-image-description="" data-image-caption="" data-large-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?fit=831%2C377&amp;ssl=1" class="wp-image-2430" src="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?resize=560%2C254&#038;ssl=1" alt="" width="560" height="254" srcset="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?w=831&amp;ssl=1 831w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?resize=300%2C136&amp;ssl=1 300w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?resize=768%2C348&amp;ssl=1 768w, https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?resize=720%2C327&amp;ssl=1 720w" sizes="auto, (max-width: 560px) 100vw, 560px" /></a><figcaption id="caption-attachment-2430" class="wp-caption-text">Objects created despite an error</figcaption></figure></p>
<p>Why did it happen? Why <em>XACT_ABORT</em> set to <em>ON</em> the whole transaction was <strong>not rolled back</strong>? That&#8217;s because, after re-reading again-mentioned <a href="https://docs.microsoft.com/en-us/sql/t-sql/statements/set-xact-abort-transact-sql" target="_blank" rel="noopener">Microsoft docs</a>, &#8220;compile errors, such as <strong>syntax errors, are not affected by SET XACT_ABORT</strong>&#8220;. Huh!</p>
<h1>Solution</h1>
<p>The only solution to this issue I found so far is to check, after each <em>GO</em> statement, if any errors occurred (using <span title="Hint: double-click to select code"><em>@@error</em>) and if yes, interrupt the whole script&#8217;s execution using <a href="https://docs.microsoft.com/en-us/sql/t-sql/language-elements/raiserror-transact-sql" target="_blank" rel="noopener"><em>raiserror</em></a>, for instance:</span></p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist83105562" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-go_raiserrror_usage-sql" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-tsql  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="GO_raiserrror_usage.sql content, created by dsibinski on 09:16PM on November 15, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="GO_raiserrror_usage.sql">
<tr>
<td id="file-go_raiserrror_usage-sql-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-go_raiserrror_usage-sql-LC1" class="blob-code blob-code-inner js-file-line">  INssSERT INTO dbo.Persons (NAME) VALUES (&#39;Dawid&#39;) </td>
</tr>
<tr>
<td id="file-go_raiserrror_usage-sql-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-go_raiserrror_usage-sql-LC2" class="blob-code blob-code-inner js-file-line">  GO</td>
</tr>
<tr>
<td id="file-go_raiserrror_usage-sql-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-go_raiserrror_usage-sql-LC3" class="blob-code blob-code-inner js-file-line">  if @@error != 0 raiserror(&#39;Error in script execution&#39;, 20, -1) with log</td>
</tr>
<tr>
<td id="file-go_raiserrror_usage-sql-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-go_raiserrror_usage-sql-LC4" class="blob-code blob-code-inner js-file-line">  GO</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/f8a9a5dea9606db4a953d7e3f04dba7c/raw/9d645072f524162468619ece5a32af53fd92dd00/GO_raiserrror_usage.sql" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/f8a9a5dea9606db4a953d7e3f04dba7c#file-go_raiserrror_usage-sql" class="Link--inTextBlock"><br />
          GO_raiserrror_usage.sql<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p><!-- Invalid Gist ID --></p>
<p>After introducing this solution to our example script, it looks <a href="https://gist.github.com/dsibinski/f08e5e685a620246b6d528fcba4ad3ff" target="_blank" rel="noopener">as in this gist</a>. After executing it in SSMS, the error is a bit different:</p>
<p><style>.gist table { margin-bottom: 0; }</style>
<div style="tab-size: 8" id="gist83107195" class="gist">
<div class="gist-file" translate="no" data-color-mode="light" data-light-theme="light">
<div class="gist-data">
<div class="js-gist-file-update-container js-task-list-container">
<div id="file-tsql_raiseerror-log" class="file my-2">
<div itemprop="text"
      class="Box-body p-0 blob-wrapper data type-text  "
      style="overflow: auto" tabindex="0" role="region"
      aria-label="TSQL_raiseerror.log content, created by dsibinski on 10:08PM on November 15, 2017."
    ></p>
<div class="js-check-hidden-unicode js-blob-code-container blob-code-content">
<p>  <template class="js-file-alert-template"></p>
<div data-view-component="true" class="flash flash-warn flash-full d-flex flex-items-center">
  <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
    <span><br />
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.<br />
      <a class="Link--inTextBlock" href="https://github.co/hiddenchars" target="_blank">Learn more about bidirectional Unicode characters</a><br />
    </span></p>
<div data-view-component="true" class="flash-action">        <a href="{{ revealButtonHref }}" data-view-component="true" class="btn-sm btn">    Show hidden characters<br />
</a>
</div>
</div>
<p></template><br />
<template class="js-line-alert-template"><br />
  <span aria-label="This line has hidden Unicode characters" data-view-component="true" class="line-alert tooltipped tooltipped-e"><br />
    <svg aria-hidden="true" data-component="Octicon" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-alert">
    <path d="M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z"></path>
</svg><br />
</span></template></p>
<table data-hpc class="highlight tab-size js-file-line-container" data-tab-size="4" data-paste-markdown-skip data-tagsearch-path="TSQL_raiseerror.log">
<tr>
<td id="file-tsql_raiseerror-log-L1" class="blob-num js-line-number js-blob-rnum" data-line-number="1"></td>
<td id="file-tsql_raiseerror-log-LC1" class="blob-code blob-code-inner js-file-line">  Msg 156, Level 15, State 1, Line 33</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L2" class="blob-num js-line-number js-blob-rnum" data-line-number="2"></td>
<td id="file-tsql_raiseerror-log-LC2" class="blob-code blob-code-inner js-file-line">  Incorrect syntax near the keyword &#39;INTO&#39;.</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L3" class="blob-num js-line-number js-blob-rnum" data-line-number="3"></td>
<td id="file-tsql_raiseerror-log-LC3" class="blob-code blob-code-inner js-file-line">  Msg 2745, Level 16, State 2, Line 37</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L4" class="blob-num js-line-number js-blob-rnum" data-line-number="4"></td>
<td id="file-tsql_raiseerror-log-LC4" class="blob-code blob-code-inner js-file-line">  Process ID 53 has raised user error 50000, severity 20. SQL Server is terminating this process.</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L5" class="blob-num js-line-number js-blob-rnum" data-line-number="5"></td>
<td id="file-tsql_raiseerror-log-LC5" class="blob-code blob-code-inner js-file-line">  Msg 50000, Level 20, State 1, Line 37</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L6" class="blob-num js-line-number js-blob-rnum" data-line-number="6"></td>
<td id="file-tsql_raiseerror-log-LC6" class="blob-code blob-code-inner js-file-line">  Error in script execution</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L7" class="blob-num js-line-number js-blob-rnum" data-line-number="7"></td>
<td id="file-tsql_raiseerror-log-LC7" class="blob-code blob-code-inner js-file-line">  Msg 596, Level 21, State 1, Line 36</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L8" class="blob-num js-line-number js-blob-rnum" data-line-number="8"></td>
<td id="file-tsql_raiseerror-log-LC8" class="blob-code blob-code-inner js-file-line">  Cannot continue the execution because the session is in the kill state.</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L9" class="blob-num js-line-number js-blob-rnum" data-line-number="9"></td>
<td id="file-tsql_raiseerror-log-LC9" class="blob-code blob-code-inner js-file-line">  Msg 0, Level 20, State 0, Line 36</td>
</tr>
<tr>
<td id="file-tsql_raiseerror-log-L10" class="blob-num js-line-number js-blob-rnum" data-line-number="10"></td>
<td id="file-tsql_raiseerror-log-LC10" class="blob-code blob-code-inner js-file-line">  A severe error occurred on the current command. The results, if any, should be discarded.</td>
</tr>
</table>
</div></div>
</p></div>
</div></div>
<div class="gist-meta">
        <a href="https://gist.github.com/dsibinski/7144531626859183d598f83fa772f3f4/raw/de27605afd2dfb5a438e7e65910308aa75cb20b9/TSQL_raiseerror.log" style="float:right" class="Link--inTextBlock">view raw</a><br />
        <a href="https://gist.github.com/dsibinski/7144531626859183d598f83fa772f3f4#file-tsql_raiseerror-log" class="Link--inTextBlock"><br />
          TSQL_raiseerror.log<br />
        </a><br />
        hosted with &#10084; by <a class="Link--inTextBlock" href="https://github.com">GitHub</a>
      </div>
</p></div>
</div>
<p>and <strong>neither the table nor the procedure is created in the database</strong>. That&#8217;s what we need.</p>
<h1>Summary</h1>
<p>I found this issue confusing and weird, especially that I could not find any equivalent to <em>XACT_ABORT</em> which would satisfy my needs and affect also syntax errors. The solution with checking for errors after each <em>GO</em> statement works fine, but is far from perfect.</p>
<p><span style="text-decoration: underline;"><strong>Maybe you have some more experience in this topic and know any better solution? It&#8217;d be appreciated!</strong></span></p>
<p>&nbsp;</p>
<p>The post <a href="https://www.codejourney.net/t-sqlssms-transaction-rollback-in-scripts-with-xact_abort-on-go-statements-and-syntax-errors/">T-SQL/SSMS: transaction rollback in scripts with XACT_ABORT ON, GO statements and syntax errors</a> appeared first on <a href="https://www.codejourney.net">CodeJourney.net</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codejourney.net/t-sqlssms-transaction-rollback-in-scripts-with-xact_abort-on-go-statements-and-syntax-errors/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2425</post-id>	</item>
	</channel>
</rss>
