我有这个观点:
CREATE VIEW vwView
AS
SELECT
ac.ac_id
,ac.Company
,ac.no
,ac.ContractID
,ac.Seller
,ac.AcquistionDate
,ac.Village
,ac.Commune
,ac.Area
,ac.PlotArea
,ac.FieldNo
,ac.Topo1
,ac.Topo2
,ac.Topo3
,ac.Topo4
,ac.Topo5
,ac.TotalAreaSqm
,ac.OwnershipTitle
,ac.CadastralNO
,ac.Type
,ac.Price
,ac.NotaryCosts
,ac.LandTax
,ac.OtherTaxes
,ac.AgentFee
,ac.CadastralFee
,ac.TabulationFee
,ac.CertSarcini
,ac.ProcuraNO
,cast((isnull(ac.price,0)+isnull(ac.notarycosts,0)+isnull(ac.landtax,0)+isnull(ac.othertaxes,0)+isnull(ac.agentfee,0)+isnull(ac.cadastralfee,0)+isnull(ac.tabulationfee,0)+isnull(ac.certsarcini,0)) as decimal(12,4)) as TotalCosts
,cast((isnull(ac.price,0)+isnull(ac.notarycosts,0)+isnull(ac.landtax,0)+isnull(ac.othertaxes,0)+isnull(ac.agentfee,0)+isnull(ac.cadastralfee,0)+isnull(ac.tabulationfee,0)+isnull(ac.certsarcini,0))/(NULLIF(ac.TotalAreaSqm,0)/10000) as decimal(12,4)) as RonPerHa
,cast((isnull(ac.price,0)+isnull(ac.notarycosts,0)+isnull(ac.landtax,0)+isnull(ac.othertaxes,0)+isnull(ac.agentfee,0)+isnull(ac.cadastralfee,0)+isnull(ac.tabulationfee,0)+isnull(ac.certsarcini,0))/(NULLIF(ac.TotalAreaSqm, 0)/10000*NULLIF(ac.FixHist, 0)) as decimal(12,4)) as EurPerHa
,ac.DeclImpunere
,ac.FixHist
,cast((isnull(ac.price,0)+isnull(ac.notarycosts,0)+isnull(ac.landtax,0)+isnull(ac.othertaxes,0)+isnull(ac.agentfee,0)+isnull(ac.cadastralfee,0)+isnull(ac.tabulationfee,0)+isnull(ac.certsarcini,0))/NULLIF(ac.FixHist,0) as decimal(12,4)) as EurHist
,ac.LandStatus
,ac.Arenda
,UPDATE nbAchizitii set ac.Arenda=CASE WHEN ac.PlotArea=ar.PlotArea then 'yes'
else 'no'
END
FROM nbAchizitii ac
LEFT JOIN nbArenda ar
ON
ac.PlotArea=ar.PlotArea
我想在我的视图中包含应该动态执行的更新(它应该使用yes或no更新ac.Arenda的每一行)但我不知道应该如何将它放在视图中。 有人可以帮帮我吗?
答案 0 :(得分:1)
我希望在我的视图中包含应该动态行动的更新
你不能这样做。您不能以UPDATE
方式查看视图中的数据。
但是,您可以更新视图中引用的那些表的基础数据,即modifying data through a view。
您必须以同样的方式创建视图,而不使用UPDATE
子句:
SELECT
...
FROM your tables
..
然后UPDATE
您的观点。类似的东西:
UPDATE YourViewName
SET arenda = CASE WHEN PlotArea IS NULL THEN 'no'
ELSE 'yes'
END;
请注意,此方法受限于CREATE VIEW
文档中可更新视图部分中定义的限制和限制。
如果您想动态执行此操作,则必须使用触发器,就像您在评论中所做的那样:
CREATE TRIGGER Arenda_update
ON nbAchizitii
for update AS
BEGIN
UPDATE ac
SET Arenda = CASE
WHEN ac.PlotArea IS NULL then 'no'
else 'Yes' END
FROM nbAchizitii ac
LEFT JOIN nbArenda ar ON ac.PlotArea = ar.PlotArea
end;