UPDATE With Top is not working

时间:2016-10-20 13:01:37

标签: sql sql-server tsql sql-server-2012

Hi I am trying to Update Column in a table with Different static date values. I try to do an update statement like below

UPDATE  f 
SET  f.CLOSEDATE = '2016-10-23'
FROM office  f
inner join
(
select OFFICEID from paroffice
where active = 1
and RowStatus ='A'
AND DistrictID = 50000
)ofc
on ( f.OfficeID = ofc.OfficeID)
where CloseDate IS NOT NULL

But I am getting

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Error. I googled and found this syntax is correct for updating specific numbers of rows. What I am missing here?

Thank you

3 个答案:

答案 0 :(得分:-1)

Try writing it without the subquery (and unnecessary parentheses)

UPDATE  f 
SET  f.CLOSEDATE = '2016-10-23'
FROM office  f
inner join paroffice ofc
  on  f.OfficeID = ofc.OfficeID
where f.CloseDate IS NOT NULL
and ofc.active = 1
and ofc.RowStatus ='A'
AND ofc.DistrictID = 50000

答案 1 :(得分:-1)

You can try this way

UPDATE  f 
SET  f.CLOSEDATE = '2016-10-23'
FROM office  f
where exists 
(
select 1 from paroffice
inner join f.OfficeId = officeid
where active = 1
and RowStatus ='A'
AND DistrictID = 50000
)
and CloseDate IS NOT NULL

答案 2 :(得分:-1)

Your query should be fine. But, you don't need the subquery at all:

update  f 
    set  f.CLOSEDATE = '2016-10-23'
    from office f inner join
         paroffice p
         on p.OfficeId = p.OfficeId
    where p.active = 1 and p.RowStatus ='A' and p.DistrictID = 50000 and
          f.CloseDate IS NOT NULL;