Solved

sql if statement always executing - help

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

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

860 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