SqlShare.com Logo
 
Skip Navigation Links
Home
Video List
Events
Classes
About Us
Login / Register
Subscribe RSS Feed 

Idera SQL Check

Using IF and ELSE in TSQL - The Basics of Conditional Logic

IF / ELSE is a basic construct in TSQL, but not necessarily simple to use. Did you know that you if you want to run multiple statements after an IF that you have to put them inside a BEGIN/END block? Or that if you don't use BEGIN/END that only the next statement after IF or ELSE will be executed as part of the branching logic? I added this to my list of lessons to build after helping a friend fix a problem caused by commenting out a single line of TSQL. Everything continued to run, there were no errors thrown, but the results became inconsistent, and it took some time to track down. Good formatting is your friend when using IF/ELSE, especially if you have multiple levels of nesting.

Duration:
4 mins 29 secs
Skill Level:
100
Rating:
4.47 out of 5
Publish Date:
September 29, 2010
Using IF and ELSE in TSQL - The Basics of Conditional Logic You must be logged in to view this video.  
Bookmark and Share
 
1=Poor, 3=Good, 5=Excellent

About the Author

Image of Andy Warren
Andy Warren is a software trainer focusing on SQL Server, a member of the PASS Board of Directors, and a principal in this site - SQLShare.com.

References



Comments
Mark Allman on 9/29/2010
Good examples and the video clarity was fine.

Amy Neumann on 9/29/2010
Very good! Wish I would have had that a few weeks ago! :)

Leonard Peoples on 9/29/2010
That was a perfect example. One question though. Is this true in a store procedure as well?

Leonard Peoples on 9/29/2010
Is this true in a stored procedure as well? Thanks, Leonard Peoples peoplesl@co.kern.ca.us

Robert Wine on 9/29/2010
Good stuff and very informative.

Randy on 9/29/2010
Along the lines for the separate if statements. Does SQL offer an else if or as Select case statement. Good job showing the pitfalls, I am beginning to use conditional logic in stored procedures - your video helps!

dfortier on 9/29/2010
Nice job!

Kyle on 9/29/2010
Very precise and easy to understand. I would suggest a good description of the Begin/End would be that they provide the scope of the if clause. Similar to programming practice (ie - in VB you would have If()Then...End; in C# you would have If(){}). Then/End sets scope for VB and the curly brackets {} set the scope for C#. Hope that helps and thanks for the video. Very nicely done!!!!

John H Davis on 9/29/2010
Not quite as good quality as usual.

Joe Tigeleiro on 9/29/2010
good explanation of the pitfalls that can occur

Theodore Callahan on 9/29/2010
Great - I had struggled with this to the point of enraged frustration. I still struggle with nested IF statements. I seem to be able to make them work, but BOY is it ugly. Would love to see a video on it and see if I'm just plain doing it wrong. If x = 1 Begin if y = 3 begin if z > 0 etc... Really? Is that just what happens when you mix procedural and non-procedural code?

Joe on 9/29/2010
Excellent presentation.

Jim Jesska on 9/29/2010
Great tip for a beginner. Who would have known?

Rod on 9/29/2010
Great job!

Deepak on 9/29/2010
Very helpful.... one of the best blogs on sql server. Thanks so much for sharing. Cheers - DV

berl on 9/29/2010
Great, I like it.

Adam Right on 9/29/2010
too simple

Mary Myers-Devlugt on 9/29/2010
I would use a different example but you did a great job in describing what happens when you don't format it properly. Your voice is amazing - clear and crisp with music in it. Nice and memorable - you need to do more videos.

Craig on 9/29/2010
I needed this two days ago. I discovered the answer through a forum online, but this video helped me understand a bit better. Thanks!

Steve Harris on 9/29/2010
Very basic but clear and informative.

Jeff Edge on 9/29/2010
Better use of my time than most of today!

Bruce Wendler on 9/29/2010
This was great. I never could figure out why my IF / ELSE didn't work, so I always used CASE statements.

Jay on 9/29/2010
You have a very pleasant voice and a good way of explaining things, but at the point where your execution pulled back two results, you really should have explained why, or started over. Given that this tutorial is targeted at the inexperienced, they could easily be confused.

Robert Neal on 9/29/2010
good topic. Have made errors with if/else a few times.

Will Casey on 9/30/2010
Never underestimate a good explanation of the basics. Nice demo

Nancy Simmons on 9/30/2010
One more time, a short video with a great explanation!

Rob Das on 9/30/2010
It's all in the syntax!

Gary Murphy on 9/30/2010
thanks so much for your knowledge on this topic, as i was only trying to do what you have demonstrated a few days ago and I kept getting to many weird things. So I changed my ode to use the GOTO functions

Mark Bowen on 9/30/2010
Thanks Andy, as tsql newb this was helpful.

Alok Joshi on 9/30/2010
You are good. Congratulations

Terry MacLean on 9/30/2010
Thank you - I've been working with TSql for years and this still gets me

Blaithin Surgeoner on 9/30/2010
Excellent as always Andy

Mohamed Irshad on 9/30/2010
hi, you did it perfectly, if/else with and without begin/end blocks; its impacts; explained clearly; well done.

zahid on 9/30/2010
very good fro basic

Brent Templeton on 10/1/2010
video good, extra content in email GREAT!!!

Abayomi Olukoya on 10/1/2010
Thanks, i have had that exact problem before and couldnt figure out why my if condition was not executing correctly

32DE12EB89 on 10/3/2010
Excellent information. I always enjoy your videos and information. This one, while very simple in message, filled a hole I didn't know I had in my knowledge. Thanks a ton for all you do for the SQL community!

SQLSharma on 10/4/2010
I have been in the SQL business for a while and this webcast is one of the Best uses of my time. Thank you Andy for doing this!

wcrabb on 10/4/2010
good stuff!

Trevor Lewis on 10/5/2010
I would never have guessed that you would need the begin and end for the if else statements. I guess you need those like brackets in other languages. Excellent video.

Dana on 10/5/2010
good selection of basic functionality that can be confusing

Prathap on 10/17/2010
Useful video

ML Stephens on 10/25/2010
just what I wanted

Satyabodhi Densmore on 10/25/2010
This should have a warning that it purely for beginners. The subject matter here is too simple to be useful.

Jeff on 10/26/2010
Great videos. I am old to programming but new to sql.

Al Martinez on 10/29/2010
You did great.

Steve Culshaw on 11/4/2010
Good overview. I tend to think of it as ... its going to run one statement after the IF and the begin/end makes a single composite statement For me, you zoomed in to far in some sections, so the code lines were trimmed

Carlos on 11/7/2010
Excellent job Andy, as always!

Tonci on 11/9/2010
you did fine.

nilesh bilimoria on 11/16/2010
cool..great job

Derek on 11/22/2010
A bit different from PL/SQL--good to know.

Don Nesbitt on 11/24/2010
You explained it well; the design of the coding seems wrong and counter-intuitive.

Eugene on 11/26/2010
Truly amazing. Thank you very much.

Loren Scherbak on 12/2/2010
You did great! I needed this just yesterday so this video is quite timely and you did a great job of explaining the functionality. thanks!

Pa'a Nunes on 12/12/2010
Clear and Concise

Stephen Morgan on 12/27/2010
Good job. I'm looking to review SQL basics as I have not been able to code much over the past year or two. Thanks!



Must Be Logged In
 

How Do I Become a Video Author? |  Newsletter History

Copyright © Fourdeuce, Inc., 2005-2009. All Rights Reserved | Privacy Policy | Terms & Conditions