简体   繁体   English

条件表更新SQL Server 2008

[英]Conditional table update SQL Server 2008

I want to update table column where value taken from different column of same table, with condition that CompletionDate='1900-01-01 00:00:00.000' 我想更新表列,其中值取自同一表的不同列,条件为CompletionDate='1900-01-01 00:00:00.000'

How can I do it? 我该怎么做?

update Details 
set CompletionDate = (select FixedDate from TblRequirementDetails)
where CompletionDate = '1900-01-01 00:00:00.000'
and StatusID = '10'
GO

produces an error 产生错误

Subquery returned more than 1 value. 子查询返回的值超过1。 This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression. 当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做。

It is very unclear on how you want to select the fixed date to set so. 您尚不清楚如何选择固定日期进行设置。 By your comments it is clear that you want to set the CompletionDate to FixedDate of same table when StausID=10 for fixedDate and I am assuming that It will give you the uniq row. 通过您的意见很清楚,你要设置的CompletionDateFixedDate同桌时StausID=10为fixedDate和我假设它会给你的uniq行。

        UPDATE Details
        SET CompletionDate = FixedDate
        WHERE
             CompletionDate='1900-01-01 00:00:00.000'
             AND StausID= '10'

I am including to make sure that The subquery returns only one row. 我包括确保子查询仅返回一行。 Bu this is very error prone approch and you need make sure that the Top 1 row is what you want to use. 但是,这很容易出错,因此您需要确保“前1行”是您要使用的行。


UPDATE UPDATE
As fromm comment I understood as you just want to update the same rows Complettiondate with that rows FixedDate value so updated the query. 作为fromm注释,我了解到您只想用该行FixedDate值更新相同的行Complettiondate ,因此更新了查询。

Your sub query returns many result. 您的子查询返回许多结果。 Your sub query should only give a 1 value result. 您的子查询应仅给出1值的结果。 See query below 请参阅下面的查询

UPDATE Details 
SET CompletionDate = (
SELECT 
      FixedDate 
FROM TblRequirementDetails <WHERE CLAUSE HERE THAT ENABLES YOUR QUERY TO RETURN ONLY ONE VALUE>
)
WHERE CompletionDate='1900-01-01 00:00:00.000'
AND StatusID='10

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM