<?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>T-SQL Archives - CodeJourney.net</title>
	<atom:link href="https://www.codejourney.net/tag/t-sql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codejourney.net/tag/t-sql/</link>
	<description>Become a better .NET full stack web developer</description>
	<lastBuildDate>Sat, 17 Nov 2018 12:37:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</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>T-SQL Archives - CodeJourney.net</title>
	<link>https://www.codejourney.net/tag/t-sql/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">123174533</site>	<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" 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" 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" 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" 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" 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" 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" 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" 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" 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" 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>
<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" fetchpriority="high" 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-medium-file="https://i0.wp.com/www.codejourney.net/wp-content/uploads/2017/11/SSMS_ObjectsCreated.png?fit=300%2C136&amp;ssl=1" 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="(max-width: 560px) 100vw, 560px" /></a><figcaption id="caption-attachment-2430" class="wp-caption-text">Objects created despite an error</figcaption></figure>
<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" 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" 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" 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" 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>
