Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using "GO" within a transaction

I'm building a web app that attempts to install/upgrade the database on App_Start. Part of the installation procedure is to ensure that the database has the asp.net features installed. For this I am using the System.Web.Management.SqlServices object.

My intention is to do all the database work within an SQL transaction and if any of it fails, roll back the transaction and leave the database untouched.

the SqlServices object has a method "Install" that takes a ConnectionString but not a transaction. So instead I use SqlServices.GenerateApplicationServicesScripts like so:

string script = SqlServices.GenerateApplicationServicesScripts(true, SqlFeatures.All, _connection.Database); SqlHelper.ExecuteNonQuery(transaction, CommandType.Text, script, ...); 

and then I use the SqlHelper from the Enterprise Library.

but this throws an Exception with a butt-load of errors, a few are below

Incorrect syntax near 'GO'. Incorrect syntax near 'GO'. Incorrect syntax near 'GO'. Incorrect syntax near 'GO'. Incorrect syntax near the keyword 'USE'. Incorrect syntax near the keyword 'CREATE'. Incorrect syntax near 'GO'. The variable name '@cmd' has already been declared. Variable names must be unique within a query batch or stored procedure. 

I'm presuming it's some issue with using the GO statement within an SQL Transaction.

How can I get this generated script to work when executed in this way.

like image 672
Greg B Avatar asked Jun 09 '09 16:06

Greg B


People also ask

Does go commit a transaction?

GO is just a command to the Client SQL program (Query Analyzer, SSMS, etc.) to terminate the current batch and execute it. Go does not terminate the current session or process and transactions are session-scoped entities. So, transactions are not COMMITted when a GO is encountered, as the Previous script demonstrates.

What is the use of go in Transact SQL?

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor. SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server.

Is it correct best practice to have the try catch block inside the transaction or should the transaction be inside the try block?

Option A is the correct choice. It is possible for all statements in a transaction to work and then the actual COMMIT to fail, so you keep the COMMIT inside your TRY block so that any failure of the COMMIT will be caught and you can gracefully handle this error and rollback.

Can we use transaction in SQL function?

1 Answer. Show activity on this post. That's why transactions are unnecessary for sql-server functions. However, you can change transaction isolation level, for example, you may use NOLOCK hint to reach "read uncommitted" transaction isolation level and read uncommitted data from other transactions.


1 Answers

GO is not a SQL keyword.

It's a batch separator used by client tools (like SSMS) to break the entire script up into batches

You'll have to break up the script into batches yourself or use something like sqlcmd with "-c GO" or osql to deal with "GO"

like image 193
gbn Avatar answered Oct 22 '22 02:10

gbn