Solved

sql if statement always executing - help

Posted on 2014-09-11
3
166 Views
Last Modified: 2014-10-30
Hi, i have this T SQL


declare @recount int
set @recount = 3
print @recount

if @recount < 1
Print 'sending email'
USE [msdb]
    EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'WltProfile',
      @recipients = 'me@domain.com',
      @subject = 'import exe may of has crashed on dev ',
      @body = 'This is an automatic email ...' 

Open in new window


It SHOULD ignore the IF and step over it
however, when i Run this, i get this in results pane:
3
Mail (Id: 1043) queued.

Open in new window



Its never printing the text 'sending email'
And its always sending the email even though it shouldn't execute that code as its inside the if

What is wrong with this code?
0
Comment
Question by:websss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Accepted Solution

by:
Akilandeshwari N earned 500 total points
ID: 40316478
You need to GROUP the multiple statements using BEGIN and END. Otherwise it will execute only the first command that succeeds right after the IF.
if @recount < 1
begin
   ---you statements
end

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40316482
I agree with above: BEGIN END is required here
note that the USE will not work (if I remember correctly), and should be avoided any now
declare @recount int
set @recount = 3
print @recount

IF @recount < 1
BEGIN
  Print 'sending email'

    EXEC msdb.dbo.sp_send_dbmail
      @profile_name = 'WltProfile',
      @recipients = 'me@domain.com',
      @subject = 'import exe may of has crashed on dev ',
      @body = 'This is an automatic email ...' 
END -- if @recount < 1

Open in new window

note that I do a comment on the END to inform what condition this is coming from, this is especially useful when debugging this kind of code, even more if you have several IF blocks within each other...
0
 
LVL 1

Expert Comment

by:aneesa83
ID: 40343854
Use Begin...End Block after IF statement
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question