Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When do I need to use Begin / End Blocks and the Go keyword in SQL Server?

What are the guidelines as to when to use begin and end blocks in SQL Server?

Also, what exactly does the Go keyword do?

like image 765
Tarik Avatar asked Jul 24 '09 21:07

Tarik


People also ask

When to use begin and end in SQL Server?

BEGIN and END are used in Transact-SQL to group a set of statements into a single compound statement, so that control statements such as IF … ELSE, which affect the performance of only a single SQL statement, can affect the performance of the whole group.

When should I use go in SQL Server?

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. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.

Do you need to use go in SQL?

They're not strictly required - they're just instructions for the SQL Server Management Studio to execute the statements up to this point now and then keep on going. GO is not a T-SQL keyword or anything - it's just an instruction that works in SSMS.

Do I need begin end in stored procedure?

END at the start and end of a stored procedure and function. But it is not strictly necessary. However, the BEGIN... END is required for the IF ELSE statements, WHILE statements, etc., where you need to wrap multiple statements.

What is the use of begin and END keywords in SQL Server?

Applies to: SQL Server (all supported versions) Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW) Encloses a series of Transact-SQL statements so that a group of Transact-SQL statements can be executed. BEGIN and END are control-of-flow language keywords.

Is the Transact-SQL keyword begin and end control-of-flow?

BEGIN and END are control-of-flow language keywords. To view Transact-SQL syntax for SQL Server 2014 and earlier, see Previous versions documentation. Is any valid Transact-SQL statement or statement grouping as defined by using a statement block. BEGIN...END blocks can be nested.

Can begin and end blocks be nested?

BEGIN...END blocks can be nested. Although all Transact-SQL statements are valid within a BEGIN...END block, certain Transact-SQL statements should not be grouped together within the same batch, or statement block. Examples. In the following example, BEGIN and END define a series of Transact-SQL statements that execute together.

How to write multiple BEGIN END statements in SQL?

Multiple BEGIN END statements can be written inside the same method or stored procedure, etc. We can even write nested BEGIN END statements in SQL where the logical block of statements defined between BEGIN and END keywords can be executed on a conditional basis or inside the loops and other functions according to the use case and requirement.


1 Answers

GO is like the end of a script.

You could have multiple CREATE TABLE statements, separated by GO. It's a way of isolating one part of the script from another, but submitting it all in one block.


BEGIN and END are just like { and } in C/++/#, Java, etc.

They bound a logical block of code. I tend to use BEGIN and END at the start and end of a stored procedure, but it's not strictly necessary there. Where it IS necessary is for loops, and IF statements, etc, where you need more then one step...

IF EXISTS (SELECT * FROM my_table WHERE id = @id) BEGIN    INSERT INTO Log SELECT @id, 'deleted'    DELETE my_table WHERE id = @id END 
like image 196
MatBailie Avatar answered Oct 06 '22 23:10

MatBailie