|
sql
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Command to quit executing query through script
I have a huge script with a lot of go statements. In one of those
batches how can I stop further processing of this file if for e.g. some condition pertaining to my processing logic is met? Tried quit, exit nothing works. Tried using return but processing continues from the next batch. --deostroll. There is no command that will exit the batch and stop processing the
other batches. Actually there is, but most likely you do not want that. You can raise an error with high severity level and SQL Server will terminate the process/connection, that way skipping the remaining batches. For example: RAISERROR('Exit', 20, 1) WITH LOG; To raise such error the user has to be a member of the sysadmin fixed server role. A better approach will be to create some kind of flag that you can check in the beginning of each batch. You can use a temp table: PRINT 'Step 1' -- to exit create flag table CREATE TABLE #flag (col INT); GO -- check and skip batch if flag is set IF OBJECT_ID('tempdb..#flag', 'U') IS NOT NULL RETURN; PRINT 'Step 2' GO Also, you can use CONTEXT_INFO: PRINT 'Step 1' -- to exit set CONTEXT_INFO DECLARE @flag VARBINARY(50); SET @flag = CAST('Exit' AS VARBINARY(50)); SET CONTEXT_INFO @flag; GO -- check and skip batch if CONTEXT_INFO is set IF CONTEXT_INFO() = CAST('Exit' AS VARBINARY(50)) RETURN; PRINT 'Step 2' GO Assuming one is using SQLCMD/OSQL then we can raise and error with
state 127. This is less dramatic than a very high severity level. This is a tool thing where those tools exit on error with state 127. -- Show quoteHide quoteTibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://sqlblog.com/blogs/tibor_karaszi "Plamen Ratchev" <Pla***@SQLStudio.com> wrote in message news:C8adneU_0Mloz97UnZ2dnUVZ_sfinZ2d@speakeasy.net... > There is no command that will exit the batch and stop processing the > other batches. Actually there is, but most likely you do not want > that. You can raise an error with high severity level and SQL Server > will terminate the process/connection, that way skipping the > remaining batches. For example: > > RAISERROR('Exit', 20, 1) WITH LOG; > > To raise such error the user has to be a member of the sysadmin > fixed server role. > > A better approach will be to create some kind of flag that you can > check in the beginning of each batch. You can use a temp table: > > PRINT 'Step 1' > > -- to exit create flag table > CREATE TABLE #flag (col INT); > > GO > > -- check and skip batch if flag is set > IF OBJECT_ID('tempdb..#flag', 'U') IS NOT NULL > RETURN; > > PRINT 'Step 2' > > GO > > Also, you can use CONTEXT_INFO: > > PRINT 'Step 1' > > -- to exit set CONTEXT_INFO > DECLARE @flag VARBINARY(50); > SET @flag = CAST('Exit' AS VARBINARY(50)); > SET CONTEXT_INFO @flag; > > GO > > -- check and skip batch if CONTEXT_INFO is set > IF CONTEXT_INFO() = CAST('Exit' AS VARBINARY(50)) > RETURN; > > PRINT 'Step 2' > > GO > > > -- > Plamen Ratchev > http://www.SQLStudio.com
Other interesting topics
High CPU Use
text to number <--SQL rookie here Options dbcc shrinkfile SQL 2005: "Function argument count error." error SQL Server 2008 - remote connection problems sql server hung Pending Reboot Requirement html encoding How ca I Read the select Statement of a view ? Commands SQL Server 2000 EM to SQL Server 2005 SSMS |
|||||||||||||||||||||||