Solved

Update for a certain condition

Posted on 2009-06-30
9
163 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
 

Author Comment

by:JaimeJegonia
ID: 24751994
appari:

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


Thanks,
0
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 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

16 Experts available now in Live!

Get 1:1 Help Now