The Alpha and the Omega. The BEGIN and the END.

The End
The End
The BEGIN/END block is a fairly standard thing if you’ve ever done any coding, but it never hurts to do a little review. Not to mention that there are always people just starting to learn about a subject.

So what is a BEGIN/END block? They are boundaries that define a group of SQL statements.

   Put your SQL Code here

But what do they do?!?

Just that, define a block of code. They are not a transaction, they do not affect the order the statements run. So what use are they?

Well, let’s say, for example, that you have an IF statement:

IF 1=0
    PRINT 'a'
    PRINT 'b'

As you may very well already know, b is going to be printed. Now this isn’t true for all languages. In Python for example formatting actually affects how the code runs. In T-SQL, however, white space doesn’t do anything. In addition, statements like the IF..ELSE statement, and the WHILE statement only affect the single line of code after them.

Yep. Just one single line of code.

You might be asking, How exactly does that work?, I have to be able to run a bunch of code after an IF. In fact, I’ve seen it done before. And yes. You have. Sort of.

What you’ve actually seen is the IF, ELSE or WHERE running just one single line of code. The BEGIN statement. The BEGIN then tells SQL to run everything until the END of the block.

Last but not least you can nest them. Which is great until the end of your code looks something like this.

Continue reading at

(1 vote. Average 5 of 5)
Leave a reply

Your email address will not be published. Required fields are marked *