Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Update for a certain condition

Posted on 2009-06-30
9
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 800 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

610 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