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
Solved

Update for a certain condition

Posted on 2009-06-30
9
166 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

861 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