HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

T-SQL tool that requires a semicolon to end statements

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
sqlstatementsrequiressemicolonthatendtool

Problem

I have what is probably a rather unusual question. Is there a T-SQL tool that requires using a semicolon to terminate statements? I know that it's not required to use semicolons with SQL Server 2008 R1, which is what I'm connecting to, but I actually would like to be forced to use it for running update queries. I'm sure we're all aware of the dangers of writing a query like the one below in the middle of a sql pad, and then highlighting it and running it (which is unfortunately something I tend to do a lot):

UPDATE dbo.Customers
SET ZipCode = '40223'
WHERE Name = 'Bob Loblaw'


... because you only have to miss-select your text once to nuke all your customer's zipcodes!

So, ideally, I'd like to continue doing most of my sql work in SQL Server Management Studio, but have a separate pad open (in a tool that requires semicolons) and only allow myself to write data manipulation queries over there. This would give me great peace of mind, because I'd know I could never miss-select my commonly used update queries. If I did, it wouldn't run, because it wouldn't have a semicolon.

Solution

Rather than relying on semicolons (a misplaced semicolon could also cause the problem you are describing), better to rely on transactions.

Set up your query tool to require a commit, and you will have the option rollback if necessary.

In SSMS, you can do this by checking the option SET IMPLICIT TRANSACTIONS under Tools/Options/Query Execution/SQL Server/ANSI. Or, you can turn it on with this command:

SET IMPLICIT_TRANSACTIONS ON


SET IMPLICIT_TRANSACTIONS (Transact-SQL)


When ON, SET IMPLICIT_TRANSACTIONS sets the connection into implicit
transaction mode. When OFF, it returns the connection to autocommit
transaction mode.


When a connection is in implicit transaction mode and the connection
is not currently in a transaction, executing any of the following
statements starts a transaction:


ALTER TABLE FETCH REVOKE BEGIN TRANSACTION GRANT SELECT CREATE INSERT
TRUNCATE TABLE DELETE OPEN UPDATE DROP


If the connection is already in
an open transaction, the statements do not start a new transaction.


Transactions that are automatically opened as the result of this
setting being ON must be explicitly committed or rolled back by the
user at the end of the transaction. Otherwise, the transaction and all
of the data changes it contains are rolled back when the user
disconnects. After a transaction is committed, executing one of the
statements above starts a new transaction.

Code Snippets

SET IMPLICIT_TRANSACTIONS ON

Context

StackExchange Database Administrators Q#11863, answer score: 6

Revisions (0)

No revisions yet.