Solved

Update for a certain condition

Posted on 2009-06-30
9
172 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 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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