IF EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('[dbo].[xdjcb]') AND type = 'V')
DROP VIEW [dbo].[xdjcb]
GO



CREATE view  [dbo].[xdjcb]  as
(
select  b1.cu_account No,b.*  from  (select  *  from  (SELECT t.ord_creator Name,t.ord_dpbh,t.ord_cuid,t.ord_dpmc ShopName,t.ord_type TableName,t.ord_no 单据编号,t.ord_updated AddTime,t1.ord2_jcnr Type,t1.ord2_jcjg TypeContent,t.ord_status 表单状态,'主检查人' 检查人类型
FROM order_196  t
FULL JOIN order_196_1  t1
ON t.ord_id=t1.ord2_ordid  where  t.ord_dpbh!=''  and  t.ord_no  not like '%临%'

UNIONALL


SELECT t.ord_creator Name,t.ord_dpbh,t.ord_cuid,t.ord_dpmc ShopName,t.ord_type TableName,t.ord_no 单据编号,t.ord_updated AddTime,t1.ord2_jcnr Type,t1.ord2_jcjg TypeContent,t.ord_status 表单状态,'主检查人' 检查人类型
FROM order_196  t
FULL JOIN order_196_2  t1
ON t.ord_id=t1.ord2_ordid  where  t.ord_dpbh!=''  and  t.ord_no  not like '%临%'

UNION ALL


SELECT t.ord_creator Name,t.ord_dpbh,t.ord_cuid,t.ord_dpmc ShopName,t.ord_type TableName,t.ord_no 单据编号,t.ord_updated AddTime,t1.ord2_jcnr Type,t1.ord2_jcjg TypeContent,t.ord_status 表单状态,'主检查人' 检查人类型
FROM order_196  t
FULL JOIN order_196_3  t1
ON t.ord_id=t1.ord2_ordid  where  t.ord_dpbh!='' and  t.ord_no  not like '%临%') x )  b  left  join  mps_employee  b1  on  b.ord_cuid=b1.cu_id  )


UNION ALL

select b.No,cu_name NAME,b.ord_dpbh,b1.cu_id ord_cuid,b.ShopName, b.TableName,b.单据编号,b.AddTime,b.Type,b.TypeContent,b.表单状态,'共同检查人' 检查人类型  from (select  w.*,t1.ord2_jcnr Type,t1.ord2_jcjg TypeContent  from (SELECT t.ord_id,No=substring(t.ord_gtxdrgh,b.number,charindex(',',t.ord_gtxdrgh+',',b.number)-b.number),t.ord_dpbh,t.ord_cuid,t.ord_dpmc ShopName,t.ord_type TableName,t.ord_no 单据编号,t.ord_updated AddTime,t.ord_status 表单状态
FROM (select * from  order_196 where ord_gtxdrgh !='')  t
join master..spt_values b on b.type='P'
where charindex(',',','+t.ord_gtxdrgh,b.number)=b.number ) w
FULL JOIN order_196_1  t1
ON w.ord_id=t1.ord2_ordid  where  w.ord_dpbh!=''  and  w.单据编号  not like '%临%')  b left  join  mps_employee  b1  on  b.No=b1.cu_account 

UNION ALL

select b.No,cu_name NAME,b.ord_dpbh,b1.cu_id ord_cuid,b.ShopName, b.TableName,b.单据编号,b.AddTime,b.Type,b.TypeContent,b.表单状态,'共同检查人' 检查人类型  from (select  w.*,t1.ord2_jcnr Type,t1.ord2_jcjg TypeContent  from (SELECT t.ord_id,No=substring(t.ord_gtxdrgh,b.number,charindex(',',t.ord_gtxdrgh+',',b.number)-b.number),t.ord_dpbh,t.ord_cuid,t.ord_dpmc ShopName,t.ord_type TableName,t.ord_no 单据编号,t.ord_updated AddTime,t.ord_status 表单状态
FROM (select * from  order_196 where ord_gtxdrgh !='')  t
join master..spt_values b on b.type='P'
where charindex(',',','+t.ord_gtxdrgh,b.number)=b.number ) w
FULL JOIN order_196_2  t1
ON w.ord_id=t1.ord2_ordid  where  w.ord_dpbh!=''  and  w.单据编号  not like '%临%')  b left  join  mps_employee  b1  on  b.No=b1.cu_account 


UNION ALL

select b.No,cu_name NAME,b.ord_dpbh,b1.cu_id ord_cuid,b.ShopName, b.TableName,b.单据编号,b.AddTime,b.Type,b.TypeContent,b.表单状态 ,'共同检查人' 检查人类型 from (select  w.*,t1.ord2_jcnr Type,t1.ord2_jcjg TypeContent  from (SELECT t.ord_id,No=substring(t.ord_gtxdrgh,b.number,charindex(',',t.ord_gtxdrgh+',',b.number)-b.number),t.ord_dpbh,t.ord_cuid,t.ord_dpmc ShopName,t.ord_type TableName,t.ord_no 单据编号,t.ord_updated AddTime,t.ord_status 表单状态
FROM (select * from  order_196 where ord_gtxdrgh !='')  t
join master..spt_values b on b.type='P'
where charindex(',',','+t.ord_gtxdrgh,b.number)=b.number ) w
FULL JOIN order_196_3 t1
ON w.ord_id=t1.ord2_ordid  where  w.ord_dpbh!=''  and  w.单据编号  not like '%临%')  b left  join  mps_employee  b1  on  b.No=b1.cu_account