有条件地更新多个列

时间:2015-12-10 12:17:36

标签: c# sql sql-server

美好的一天!我目前在更新数据库方面遇到了麻烦。我在c#.net中编码并使用SQL服务器。

我的问题是我尝试有条件地更新多个列。这是我的代码:

"Update Eureka Set Milestone1 = Case When Milestone1 = '" + araw.Text + "' then Milestone_status = '" + m1 + "' End, Milestone2 = When Milestone2 = '" + araw.Text + "' then Milestone_status = '" + m2 + "' End, Milestone3 = When Milestone3 = '" + araw.Text + "' Then Milestone_status = '" + m3 + "' End, Milestone4 = When Milestone4 = '" + araw.Text + "' Then Milestone_status = '" + m4 + "' End, Milestone5 = When Milestone5 = '" + araw.Text + "' Then Milestone_status = '" + m5 + "' End Where Eureka_id = '" + eid.Text + "'"

如果Milestone1等于今天的日期(araw.text),那么里程碑状态将会改变。如果没有,则里程碑_状态不会被更改。那么里程碑2到里程碑5也是如此。

我的代码是SQL语句无法正常工作。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:1)

将它从C#中取出并将其视为单独的SQL语句,并且为了简单起见只考虑里程碑1,您的SQL必须是这样的:

UPDATE Eureka
   SET milestone_status = 
      CASE WHEN milestone1 = @todays_date then @m1
      ELSE milestone_status
      END

即。如果milestone1是今天的日期,则将milestone_status设置为@ m1,否则将其设置为自身(即不要更改它)。

SQL文档有几个关于CASE语法使用的好例子,为清晰起见,可能值得一看:https://msdn.microsoft.com/en-us/library/ms181765.aspx

希望有所帮助

答案 1 :(得分:1)

尝试;

Update Eureka 
Set Milestone_status = 
    Case 
        When Milestone1 = @araw_Text then @m1
        when Milestone2 = @araw_Text then @m2
        when Milestone3 = @araw_Text then @m3
        when Milestone4 = @araw_Text then @m4
        when Milestone5 = @araw_Text then @m5
        else Milestone_status
    end
Where Eureka_id = @eid_Text

这将更新
如果Milestone_statusm1 Milestone1 = araw.Text Milestone_status 如果m2Milestone2 = araw.Text Milestone_status m3 Milestone3 = araw.Text Milestone_status <?php comment_form(); ?> ,如果<?php comment_form(array('title_reply' => 'Comment with image' )); ?> ....

如果没有满足条件,则func fetchWithPredicate(entityName: String, argumentArray: [AnyObject]) -> [NSManagedObject] { let fetchRequest = NSFetchRequest(entityName: entityName) fetchRequest.predicate = NSPredicate(format: "%K == %@", argumentArray: argumentArray) do { return try self.managedContext.executeFetchRequest(fetchRequest) as! [NSManagedObject] } catch { let fetchError = error as NSError print(fetchError) return [NSManagedObject]() } } 将不会被修改

答案 2 :(得分:0)

您的CASE语句是蠕虫语法:

Update Eureka 
Set Milestone1 = Case When Milestone1 = 'araw.Text' then Milestone_status = 'm1' End
,   Milestone2 = When Milestone2 = 'araw.Text' then Milestone_status = 'm2' End
,   Milestone3 = When Milestone3 = 'araw.Text' Then Milestone_status = 'm3' End
,   Milestone4 = When Milestone4 = 'araw.Text' Then Milestone_status = 'm4' End
,   Milestone5 = When Milestone5 = 'araw.Text' Then Milestone_status = 'm5' End 
Where Eureka_id = 'eid.Text'

正确的语法必须像

CASE expression

   WHEN value_1 THEN result_1
   WHEN value_2 THEN result_2
   ...
   WHEN value_n THEN result_n

   ELSE result

END

在您的情况下,您可以使用嵌套CASE检查每个里程碑(Milestone1,Milestone2,..)

"Update Eureka
set Milestone_status = case when Milestone1 = '" + araw.Text + "' then '" + m1 + "' else 
                         case when Milestone2 = '" + araw.Text + "' then '" + m2 + "' else
                           case when Milestone3 = '" + araw.Text + "' then '" + m3 + "' else
                             case when Milestone4 = '" + araw.Text + "' then '" + m4 + "' else
                               case when Milestone5 = '" + araw.Text + "' then '" + m5 + "' else Milestone_status end
                              end
                            end                              
                          end
                        end
Where Eureka_id = '" + eid.Text + "'"