Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Why use BEGIN...END for only 1 line of code?

Author  Topic 

Aleph_0
Yak Posting Veteran

79 Posts

Posted - 2011-05-04 : 13:08:56
I saw something like this:

IF NOT EXISTS (--select blah blah etc.)
BEGIN
EXEC SomeStoredProcedure @SomeParameter
END
IF NOT EXISTS (--another select blah blah etc.)
BEGIN
EXEC ADifferentProcedure @AndItsParameter
END
etc.


Why would you wrap a single statement in BEGIN...END? There's no danger of nesting the IFs, right?

Thanks!

robvolk
Most Valuable Yak

15732 Posts

Posted - 2011-05-04 : 13:11:58
It's a good habit that you don't have to change in case you add more than 1 line.
Go to Top of Page

Aleph_0
Yak Posting Veteran

79 Posts

Posted - 2011-05-04 : 16:26:34
Good point, thanks!
Go to Top of Page

ajthepoolman
Constraint Violating Yak Guru

384 Posts

Posted - 2011-05-06 : 10:16:29
I also think it makes it easier to read on the programmer who sees it after you have written it. I personally have skimmed over conditional blocks that didn't have begin/end statements (or even worse in C or VB where there is no curly braces or parantheses) and lost my place in the conditional logic because my eyes didn't catch the fact that the next line terminates a function.

Hey, it compiles.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-05-06 : 11:27:33
It's easy to change this:
IF NOT EXISTS (--select blah blah etc.)
EXEC SomeStoredProcedure1 @SomeParameter1
to this:
IF NOT EXISTS (--select blah blah etc.)
EXEC SomeStoredProcedure1 @SomeParameter1
EXEC SomeStoredProcedure2 @SomeParameter2
and not realize that SomeStoredProcedure2 will always be executed.




CODO ERGO SUM
Go to Top of Page

jcelko
Esteemed SQL Purist

547 Posts

Posted - 2011-05-06 : 20:50:56
This is a habit from the punch card days if you use the "BEGIN <one statement> END" stuff it makes your program look physically bigger and easier to slip in more cards -- one command per line.

We have text editors today. Stop doing this silliness.

I spent years doing research for AIRMICS to measure this sort of stuff. Read my SQL PROGRAMMING STYLE book for some details.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2011-05-09 : 12:01:29
quote:
Originally posted by jcelko

This is a habit from the punch card days if you use the "BEGIN <one statement> END" stuff it makes your program look physically bigger and easier to slip in more cards -- one command per line.

We have text editors today. Stop doing this silliness.

I spent years doing research for AIRMICS to measure this sort of stuff. Read my SQL PROGRAMMING STYLE book for some details.

--CELKO--
Books in Celko Series for Morgan-Kaufmann Publishing
Analytics and OLAP in SQL
Data and Databases: Concepts in Practice
Data, Measurements and Standards in SQL
SQL for Smarties
SQL Programming Style
SQL Puzzles and Answers
Thinking in Sets
Trees and Hierarchies in SQL





I seriously doubt that many of the people posting here have coding habits that date back to the "punch card days".




CODO ERGO SUM
Go to Top of Page
   

- Advertisement -