Solved

sql if statement always executing - help

Posted on 2014-09-11
3
154 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
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 142

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

773 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