Solved

sql if statement always executing - help

Posted on 2014-09-11
3
145 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
Comment Utility
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]
Comment Utility
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
Comment Utility
Use Begin...End Block after IF statement
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now