Sql自动更新

时间:2012-12-11 15:15:29

标签: sql sql-server database sql-server-2008 tsql

我有这个观点: 创建视图 AS

SELECT
         p.pr_id
        ,p.arenda
        ,p.PlotArea
        ,p.OwnershipTitle
        ,p.Price
        ,p.NotaryCosts
        ,p.AgentFee
        ,p.CtrNO
        ,isnull(p.Price,0)-isnull(a.Price,0) as Diferente
        ,isnull(p.Price,0)+isnull(p.NotaryCosts,0)+isnull(p.AgentFee,0) as TotalCosts

from  nbProcuri p
      left JOIN nbAchizitii a
      ON p.PlotArea = a.PlotArea and p.CtrNo=a.CtrNo
where a.CtrNO is null and a.PlotArea is null

我想将这两个表与另一个名为Cadastrial的表相关联,其中我还有一个名为PlotArea的列。如果p.PlotArea = c.PlotArea,则列p.arenda应该使用值'yes'更新自己,否则填充no。 这有可能吗? 谢谢!

1 个答案:

答案 0 :(得分:1)

像这样:

UPDATE p
SET p.arenda = CASE WHEN p.PlotArea IS NULL THEN 'no' -- For not matched 
                    ELSE 'yes' -- for matched
               END
FROM nbProcuri p
LEFT JOIN Cadastrial c ON p.PlotArea = c.PlotArea
LEFT JOIN nbAchizitii a
      ON p.PlotArea = a.PlotArea and p.CtrNo = a.CtrNo
WHERE a.CtrNO IS NULL 
  AND a.PlotArea IS NULL;