如何使用SQL Server INSTEAD-OF触发器
2007-05-19 09:43:04 来源:WEB开发网核心提示: 为了更好地说明可更新视图概念,我们提供一个示例,如何使用SQL Server INSTEAD-OF触发器(2),在本例中,我们设计一个产品表(记录产品),<b>Listing B</b>INSERT INTO Products(Description, Price)
为了更好地说明可更新视图概念,我们提供一个示例。在本例中,我们设计一个产品表(记录产品),一个购买表(记录购买)。Listing A包含了创建表的脚本,运行此脚本后则得到示例中所要用到的表。运行Listing B脚本向表中添加数据。
<b>Listing B</b>
INSERT INTO Products(Description, Price) VALUES('Television',500)
INSERT INTO Products(Description, Price) VALUES('VCR',100)
INSERT INTO Products(Description, Price) VALUES('DVD_Player',125)
INSERT INTO Products(Description, Price) VALUES('Alarm_Clock',40)
INSERT INTO Products(Description, Price) VALUES('Camera',325)
INSERT INTO Products(Description, Price) VALUES('Projector',1500)
INSERT INTO Products(Description, Price) VALUES('XBox',400)
GO
INSERT INTO Purchases(ProductID, PurchasePrice) VALUES(1, 500)
INSERT INTO Purchases(ProductID, PurchasePrice) VALUES(5, 325)
INSERT INTO Purchases(ProductID, PurchasePrice) VALUES(1, 525)
GO
现在表中已经有数据了,我可以为这些表创建一些有意义的视图。请查看Listing C。
<b>isting C</b>
CREATE VIEW vw_ProductPurchases
AS
SELECT
pr.ProductID,
pr.Description,
pr.Price AS ProductPrice,
pu.PurchasePrice,
pu.PurchaseDate
FROM
Products pr
INNER JOIN Purchases pu ON pr.ProductID = pu.ProductID
GO
赞助商链接