Solved

Update for a certain condition

Posted on 2009-06-30
9
168 Views
Last Modified: 2012-05-07
I want to perform conditional update as follows:

BEGIN
      UPDATE tbMeters SET MAC = @MAC,  
        Model = @Model,  
        LastDataUpdateTime = @ReportTime
        /*---------------Conditional update to be done only when @model ="XC09"-----------*/
        ,R1 = @R1,
        R2 = @R2,
        DC = @DC,
        TT = @TT,
        IT = @IT
          /* --------------- end of -Conditional update----------- */
        WHERE Disable = '0'
END

Thanks,
0
Comment
Question by:JaimeJegonia
  • 4
  • 4
9 Comments
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24751898
Hope this helps:

IF @model ='XC09' AND R1 = @R1 AND R2 = @R2 AND DC = @DC AND TT = @TT AND IT = @IT
UPDATE tbMeters SET MAC = @MAC,  
        Model = @Model,  
        LastDataUpdateTime = @ReportTime
WHERE Disable = '0'
0
 
LVL 39

Accepted Solution

by:
appari earned 200 total points
ID: 24751900
try  this

UPDATE tbMeters SET MAC = @MAC,  
        Model = @Model,  
        LastDataUpdateTime = @ReportTime
        /*---------------Conditional update to be done only when @model ="XC09"-----------*/
        ,R1 = case when @model ='XC09'  then @R1 else R1 End ,
        R2  = case when @model ='XC09'  then @R2 else R2 End ,
        DC  = case when @model ='XC09'  then @DC else DC End ,
        TT  = case when @model ='XC09'  then @TT else TT End ,
        IT  = case when @model ='XC09'  then @IT else IT End
          /* --------------- end of -Conditional update----------- */
        WHERE Disable = '0'
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24751901
Or a more simple UPDATE statement like this would do:
UPDATE tbMeters SET MAC = @MAC,  
        Model = 'XC09',  
        LastDataUpdateTime = @ReportTime
WHERE Disable = '0'
 AND R1 = @R1 
 AND R2 = @R2 
 AND DC = @DC 
 AND TT = @TT 
 AND IT = @IT

Open in new window

0
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.

 

Author Comment

by:JaimeJegonia
ID: 24751994
appari:

You recommendation is pretty close but is there any way we can avoid repetition?


Thanks,
0
 

Author Comment

by:JaimeJegonia
ID: 24752061
Do you think this code will work:

If @Model = 'XC09'
    BEGIN
      UPDATE tbMeters SET MAC = @MAC,  
              Model = @Model,  
              LastDataUpdateTime = @ReportTime,
              R1 = @R1,
              R2 = @R2,
              DC = @DC,
              TT = @TT,
              IT = @IT
      WHERE Disable = '0'
      END
else
   BEGIN
     UPDATE tbMeters SET MAC = @MAC,  
              Model = @Model,  
              LastDataUpdateTime = @ReportTime
      WHERE Disable = '0'
END
          
Thanks
0
 
LVL 57

Assisted Solution

by:Raja Jegan R
Raja Jegan R earned 100 total points
ID: 24752073
Slight Change:
If @Model = 'XC09'
    BEGIN
      UPDATE tbMeters SET MAC = @MAC,  
              Model = 'XC09',  
              LastDataUpdateTime = @ReportTime,
              R1 = @R1,
              R2 = @R2,
              DC = @DC,
              TT = @TT,
              IT = @IT
      WHERE Disable = '0'
      END
else
   BEGIN
     UPDATE tbMeters SET MAC = @MAC,  
              Model = 'XC09',  
              LastDataUpdateTime = @ReportTime
      WHERE Disable = '0'
END

Open in new window

0
 

Author Comment

by:JaimeJegonia
ID: 24752079
What do you think If I remove one (BEGIN -END) block?

BEGIN
If @Model = 'XC09'
      UPDATE tbMeters SET MAC = @MAC,  
              Model = @Model,  
              LastDataUpdateTime = @ReportTime,
              R1 = @R1,
              R2 = @R2,
              DC = @DC,
              TT = @TT,
              IT = @IT
      WHERE Disable = '0'
     
else
   
        UPDATE tbMeters SET MAC = @MAC,  
              Model = @Model,  
              LastDataUpdateTime = @ReportTime
        WHERE Disable = '0'
END

Your comment is highly appreciated.

Thanks
0
 

Author Closing Comment

by:JaimeJegonia
ID: 31598656
Very helpful posts
0
 
LVL 57

Expert Comment

by:Raja Jegan R
ID: 24752136
Not a problem and you can remove it.

BEGIN.. END is required in IF ..ELSE condition, if you are performing more than one operation. Say if you perform two updates if it passes IF condition, then you need to enclose two update statements within BEGIN .. END.

In your case you have a single statement and you may or may not have a BEGIN.. END block itself.

Hope this clarifies
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

713 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