Hi guys can we update data (Edit) in view table in sql server? If Yes How ,if No Why?
Thanks in advance......
Hi guys can we update data (Edit) in view table in sql server? If Yes How ,if No Why?
Thanks in advance......
Short answer for (and thanks to for the pointer): yes — SQL Server allows INSERT/UPDATE/DELETE through a view, but only when the view is “updatable” (the engine can unambiguously map the change to the underlying table). Complex views (aggregates, DISTINCT, GROUP BY, set ops, computed columns, etc.) are generally read‑only unless additional work is done. (learn.microsoft.com)
Key restrictions to check before trying to update a view:
If the view is not directly updatable, use an INSTEAD OF trigger on the view to implement the required DML logic (the trigger runs instead of the attempted INSERT/UPDATE/DELETE and can route changes to one or more base tables). Example pattern (trimmed):
CREATE TRIGGER tr_vwOrders_IOU
ON dbo.vwOrderDetails
INSTEAD OF UPDATE
AS
BEGIN
UPDATE od
SET od.UnitPrice = i.UnitPrice, od.Quantity = i.Quantity
FROM dbo.OrderDetails od
JOIN inserted i ON od.OrderID = i.OrderID AND od.ProductID = i.ProductID;
END; INSTEAD OF triggers are the supported way to make complex views accept DML. (learn.microsoft.com)
Quick troubleshooting checklist when an UPDATE fails:
Hi thines01, Trouble to open link
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.