SQLserver 在存储过程中创建函数,创建视图

alter PROCEDURE usp_sumgoods(@supplierid VARCHAR(50)) as
BEGIN
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetMain]') and xtype in (N'FN', N'IF', N'TF')) 
-- 删除函数 
	drop function [dbo].[GetMain] 
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sumgoods]') and OBJECTPROPERTY(id, N'IsView') = 1) 
	drop view [dbo].[sumgoods] 
DECLARE @sql VARCHAR(MAX)
SET @sql = 'CREATE view [dbo].[sumgoods]
with schemabinding
as
(
select id,mainid,goodsNo,(select modal from dbo.bas_goods where goodsno = a.goodsNo) as modal,goodscnname,color,
(select cnname from dbo.bas_qtyunit where id=qtyunit) as qtyunit,swareqty,thispurchaseqty,Inqty,backqty,
(inqty-backqty) as realInqty,waitOutqty,freezeqty,safeqty from 
(select id,mainid,goodsNo,cnname as  goodscnname,color,qtyunit,
case when goodstype=1 then
isnull((select isnull(balanceqty,0) from dbo.bas_goods_center where warehouseid=2 and style=1 and goodsid=a.goodsid),0)
when goodstype=2 then
isnull((select isnull(balanceqty,0) from dbo.M_property_warehouse where warehouseid=1 and style=1 and wid=a.goodsid),0)
end  as swareqty,  thispurchaseqty,
isnull((select SUM(m.instockqty) from dbo.wm_stock_goods m where m.purchasegoodsid =a.id and instocktype<17),0) as inqty,
isnull((select sum(outstockqty)  from dbo.wm_stock_goods_out m where purchasecontractgoodsid=a.id and outtype=4 and m.printId=a.printid),0)  as backqty,
case when goodstype=2 then isnull(dbo.Get_material_needqty(a.goodsid,1,1),0) 
when goodstype=1 then isnull(dbo.Get_material_needqty(a.goodsid,2,1),0) end 
as waitOutqty,
(case when goodstype=1 then (select freezeqty from dbo.bas_goods_center where style=1 and warehouseid=1 and goodsid=a.goodsid)
when goodstype=2 then (select freezeqty from dbo.M_property_warehouse where style=1 and warehouseid=1 and wid=a.goodsid) end) as freezeqty,
isnull((case when goodstype=1 then (select isnull(safeqty,0) from dbo.bas_goods where id=a.goodsid)  
when goodstype=2 then  (select isnull(savesafeqty,0) from dbo.M_property where id=a.goodsid) end ),0) as safeqty 
from dbo.purchase_contract_goods a where mainid in 
(select id from dbo.purchase_contract 
where (type=1 or type=2 or type=11 or type=12 or type=13 or type=14) and   status=8 and instockstatus<4 and  supplierid = '+@supplierid+')) a where (inqty-backqty)<thispurchaseqty
)'

EXEC(@sql)

DECLARE @sql1 VARCHAR(MAX)
SET @sql1 = '
CREATE  FUNCTION [dbo].[GetMain](@goodsno varchar(50))
RETURNS varchar(max)
with schemabinding
	AS
BEGIN
    DECLARE @values varchar(max)    
    SELECT @values = Isnull(@values + '';'', '''') + cast(mainid as varchar(50)) FROM dbo.sumgoods WHERE goodsno=@goodsno
    RETURN @values
END'
EXEC(@sql1)
select goodsMain,goodsNo,(select modal from dbo.bas_goods where goodsno = x.goodsNo) as modal,goodscnname,color,qtyunit,swareqty,SUM(thispurchaseqty) thispurchaseqty,SUM(inqty) inqty,SUM(backqty) backqty,SUM(realInqty) realInqty,waitOutqty,freezeqty,safeqty
from
(
select id,mainid,goodsNo,dbo.GetMain(goodsNo) goodsMain,(select modal from dbo.bas_goods where goodsno = a.goodsNo) as modal,goodscnname,color,
(select cnname from dbo.bas_qtyunit where id=qtyunit) as qtyunit,swareqty,thispurchaseqty,Inqty,backqty,
(inqty-backqty) as realInqty,waitOutqty,freezeqty,safeqty from 
(select id,mainid,goodsNo,cnname as  goodscnname,color,qtyunit,
case when goodstype=1 then
isnull((select isnull(balanceqty,0) from dbo.bas_goods_center where warehouseid=2 and style=1 and goodsid=a.goodsid),0)
when goodstype=2 then
isnull((select isnull(balanceqty,0) from dbo.M_property_warehouse where warehouseid=1 and style=1 and wid=a.goodsid),0)
end  as swareqty,  thispurchaseqty,
isnull((select SUM(m.instockqty) from dbo.wm_stock_goods m where m.purchasegoodsid =a.id and instocktype<17),0) as inqty,
isnull((select sum(outstockqty)  from dbo.wm_stock_goods_out m where purchasecontractgoodsid=a.id and outtype=4 and m.printId=a.printid),0)  as backqty,
case when goodstype=2 then isnull(dbo.Get_material_needqty(a.goodsid,1,1),0) 
when goodstype=1 then isnull(dbo.Get_material_needqty(a.goodsid,2,1),0) end 
as waitOutqty,
(case when goodstype=1 then (select freezeqty from dbo.bas_goods_center where style=1 and warehouseid=1 and goodsid=a.goodsid)
when goodstype=2 then (select freezeqty from dbo.M_property_warehouse where style=1 and warehouseid=1 and wid=a.goodsid) end) as freezeqty,
isnull((case when goodstype=1 then (select isnull(safeqty,0) from dbo.bas_goods where id=a.goodsid)  
when goodstype=2 then  (select isnull(savesafeqty,0) from dbo.M_property where id=a.goodsid) end ),0) as safeqty 
from dbo.purchase_contract_goods a where mainid in 
(select id from dbo.purchase_contract 
where (type=1 or type=2 or type=11 or type=12 or type=13 or type=14) and   status=8 and instockstatus<4 and  supplierid = @supplierid)) a where (inqty-backqty)<thispurchaseqty
) x  group by goodscnname,goodsNo,goodsMain,modal,waitOutqty,freezeqty,safeqty,color,qtyunit,swareqty
order by goodsNo desc
END

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值