FOR XML PATH的用法

时间:2024-03-27 19:37:02

USE [ChangHongWMS612]
GO
/****** Object: StoredProcedure [dbo].[st_WMS_SelStockInBillList] Script Date: 09/30/2015 10:32:23 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Author:zhuss
Date: 2014-12-18
Remark:Get Product StockIn Bill Information
原存储过程名称:SelStockInBillList
Update:hz
UpdateTime:2014-12-27
Remark:添加分页
exec st_WMS_SelStockInBillList 'WX20150603002','2014-05-01','2016-06-04','','','test',20,1
exec st_WMS_SelStockInBillList '','2014-05-01','2016-06-04','','','test',1000,1
*/
ALTER proc [dbo].[st_WMS_SelStockInBillList]
(
@BillNO varchar(50) = '',
@StartDate varchar(19) = '',
@EndDate varchar(19) = '',
@MO varchar(50) ='',
@Status varchar(20) = '',
@ItemNO varchar(50) = '',
@ItemName varchar(100)='',
@BillType varchar(10)='',
@Pagecount int=20,
@Pageindex int=1
)
as
select ID = IDENTITY(int,1,1),i.BillNo, BillName = b.[Desc],p.StockName , i.Date, i.TotalQty
, Status = case when i.status = 'new' then '已保存' when i.status ='appr' then '已审核' else '' end,s.ItemNO
, ProductNoList = convert(varchar(8000),null),ProductList = convert(varchar(8000),null),ItemTypeList=convert(varchar(4000),null),
MoList = convert(varchar(8000),null),RemarkList = convert(varchar(8000),null),QtyList = convert(varchar(8000),null)
, ItemCount = (select count(*) from WMS_StockInItem where BillNo = i.BillNo),i.SupplyNO as SupplyNo,i.MaterialBill
into #t_StockInBill
from WMS_StockInBill i
join WMS_StockInItem s on s.billno=i.billno
join WMS_ProductStorage p on i.StockNo = p.StockNO
join WMS_BillCategory b on i.BillType = b.Name and b.Type = 'ProdIn'
join WMS_Product pt on pt.ItemNo=s.ItemNO

where (@BillNO = '' or i.BillNO like '%' + @BillNO + '%')
and ((@StartDate = '' and @EndDate = '') or i.Date between @StartDate and @EndDate)
and (@MO = '' or s.MO like '%' +@Mo + '%')
and (@Status = '' or i.Status = @Status)
and (s.ItemNo like '%'+@ItemNO+'%' or @ItemNO='')
and (@ItemName='' or pt.ItemDesc like '%'+@ItemName+'%')
and (@BillType='' or i.BillType like '%'+@BillType+'%')
order by i.Date desc, i.BillNO desc

--删除一个入库单号有多个产品的,只保留最大的那一条记录
delete from #t_StockInBill
where id not in(select max(id) from #t_StockInBill group by BillNo)

update t set ProductNoList=STUFF((
SELECT ','+ m.ItemNO FROM WMS_StockInItem m WHERE m.billno=t.billno FOR XML PATH('')
),1 ,1, '')
,ProductList=STUFF((
SELECT ','+ ItemDesc from(select billno, ItemDesc from WMS_StockInItem m join WMS_Product p on m.ItemNO=p.ItemNo) m WHERE m.billno=t.billno FOR XML PATH('')
),1 ,1, '')
,ItemTypeList=STUFF((
SELECT ','+ ItemType from(select billno, ItemType from WMS_StockInItem m join WMS_Product p on m.ItemNO=p.ItemNo) m WHERE m.billno=t.billno FOR XML PATH('')
),1 ,1, '')
,MoList=STUFF((
SELECT ','+ m.Mo FROM WMS_StockInItem m WHERE m.billno=t.billno FOR XML PATH('')
),1 ,1, '')
,RemarkList=STUFF((
SELECT ','+ m.Remark FROM WMS_StockInItem m WHERE m.billno=t.billno FOR XML PATH('')
),1 ,1, ''),
QtyList=STUFF((
SELECT ','+ convert(varchar(10),m.InQty) FROM WMS_StockInItem m WHERE m.billno=t.billno FOR XML PATH('')
),1 ,1, '')
from #t_StockInBill t

-- update t set ProductList = p.ItemDesc,ProductNoList=p.Itemno,MoList=i.Mo,RemarkList=i.Remark, QtyList = convert(varchar(100),i.InQty)
--from #t_StockInBill t
--join WMS_StockInItem i on t.BillNo = i.BillNo
--join WMS_Product p on p.ItemNo = i.ItemNo
--where t.ItemCount =1

-- declare @ProductList varchar(8000),@ProductNoList varchar(8000),@MoList varchar(8000), @RemarkList varchar(8000),
-- @QtyList varchar(8000), @vBillNo varchar(50)
-- declare CursorD cursor local
-- For select b.BillNo
-- from #t_StockInBill b
-- where ItemCount>1
--Open CursorD
--Fetch next From CursorD Into @vBillNo
--While(@@Fetch_Status = 0)
-- Begin
-- select @ProductList = '',@ProductNoList='',@MoList='',@RemarkList='', @QtyList= ''
-- select @ProductList = case when @ProductList = '' then p.ItemDesc else @ProductList+','+p.ItemDesc end,
-- @ProductNoList = case when @ProductNoList = '' then p.ItemNo else @ProductNoList+','+p.ItemNo end,
-- @MoList = case when @MoList = '' then i.Mo else @MoList+','+i.Mo end,
-- @RemarkList = case when @RemarkList = '' then i.Remark else @RemarkList+','+i.Remark end
-- , @QtyList = case when @QtyList = '' then convert(varchar(100),i.InQty) else @QtyList + ','+convert(varchar(100),i.InQty) end
-- from WMS_StockInItem i
-- join WMS_Product p on p.ItemNo = i.ItemNo
-- where i.BillNo = @vBillNo
-- update #t_StockInBill set ProductList = @ProductList,ProductNoList = @ProductNoList,
-- RemarkList=@RemarkList,QtyList = @QtyList where BillNo = @vBillNo
-- Fetch next From CursorD Into @vBillNo
--End
--Close CursorD
--Deallocate CursorD

select ID = IDENTITY(int,1,1),BillNo, BillName,MoList, StockName, Date, TotalQty
, Status, ProductNoList,ProductList,ItemTypeList,QtyList, ItemCount,SupplyNo,RemarkList,MaterialBill
into #t_StockInBill2
from #t_StockInBill

declare @row_from int, @row_to int,@out_total_rows int
select @out_total_rows = count(*) from #t_StockInBill2

execute st_MES_RS_Pages @out_total_rows, @Pagecount, @Pageindex, @row_from output, @row_to output

select * from #t_StockInBill2
where ID between @row_from and @row_to
order by ID

select @out_total_rows