Note that there are some explanatory texts on larger screens.

plurals
  1. POT-SQL: Stop processing rest of the query
    primarykey
    data
    text
    <p>I wonder if I could stop processing the rest of the query on a specific condition.</p> <p><strong>Scenario</strong></p> <pre><code>IF NOT EXISTS (SELECT * FROM [dbo].[Updates] WHERE RevisionNumber='12.2457.2') BEGIN IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestProcedure]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[TestProcedure] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[TestProcedure] AS BEGIN PRINT 'Test'; END GO INSERT [dbo].[Updates] SELECT '12.2457.2' END </code></pre> <p>The above query will not work because a line following "GO" is treated as a new query. We release updates every day and want simplicity of installing updates.</p> <p>I am searching for something like "RETURN" statement. It works, but not with "GO"s.</p> <pre><code>IF EXISTS (SELECT * FROM [dbo].[Updates] WHERE RevisionNumber='12.2457.2') BEGIN PRINT 'The update ''12.2457.2'' was already installed.' RETURN END GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestProcedure]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[TestProcedure] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[TestProcedure] AS BEGIN PRINT 'Test'; END GO INSERT [dbo].[Updates] SELECT '12.2457.2' GO </code></pre> <p><strong>Workaround</strong></p> <pre><code>BEGIN TRANSACTION GO IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TestProcedure]') AND type in (N'P', N'PC')) DROP PROCEDURE [dbo].[TestProcedure] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[TestProcedure] AS BEGIN PRINT 'Test'; END GO IF EXISTS(SELECT TOP 1 1 FROM [dbo].[Updates] WHERE RevisionNumber='12.2457.2') BEGIN ROLLBACK TRANSACTION PRINT 'The update was already present in the database. The transaction was rolled back.' END ELSE BEGIN INSERT [dbo].[Updates] SELECT '12.2457.2' COMMIT TRANSACTION PRINT 'The update was sucessfully installed.' END </code></pre> <p>What I want is an idea to stop the execution of the rest of the lines, equivalent to "RETURN/END" keyword in Visual Basic. Please help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload