T-SQL笔记---视图

use SaleManagerDB
go
--普通查询
select Products.ProductId,ProductName,Unit,UnitPrice,TotalCount,MaxCount,MinCount,StatusDesc
from Products
inner join ProductInventory on ProductInventory.ProductId=Products.ProductId
inner join InventoryStatus on InventoryStatus.StatusId=ProductInventory.StatusId

--将前面的查询保存到视图
if exists(select * from sysobjects where  name='view_QueryInventoryInfo')
drop view view_QueryInventoryInfo
go
create view view_QueryInventoryInfo
as
      select Products.ProductId,ProductName,Unit,UnitPrice,TotalCount,MaxCount,MinCount,StatusDesc
from Products
inner join ProductInventory on ProductInventory.ProductId=Products.ProductId
inner join InventoryStatus on InventoryStatus.StatusId=ProductInventory.StatusId

go

--视图创建好以后,我们可以把它看成表一样查询
select * from view_QueryInventoryInfo where MaxCount>400
--视图不要嵌套

--实际开发中,我们可以根据查询的需要,创建很多视图,对于已经存在的视图,也可以修改。

--实际开发中我们查询的可能比较复杂

猜你喜欢

转载自blog.csdn.net/qq_41617901/article/details/112489865