1. 程式人生 > >【資料庫SQL】建立SQL檢視的使用案例

【資料庫SQL】建立SQL檢視的使用案例

use hbposev9 --資料庫名
go
--如果存在就先刪除檢視
if exists(select * from sys.views  where  name = 'view_name')
drop view view_name
go
--建立檢視
CREATE VIEW view_name AS 
--查詢語句
select a.item_clsno ,a.item_name,b.branch_no
,sum(b.real_qty)as 'APP訂單數量',sum(c.real_qty) as '門店要貨數量'
from t_bd_item_info a
full  join t_order_bill_weixin b on(A.item_no=B.item_no)
full  join t_pm_sheet_detail c on(A.item_no=c.item_no)  
where (b.real_qty+c.real_qty)<>0 
and b.branch_no like '%%'
and c.sheet_no like 'YH%'
group by a.item_clsno,a.item_name,b.branch_no
go




--最後直接就可以查詢此檢視
select * from view_name