-- Tables

-- Alters

ALTER TABLE [STOR].[T_SHIPPING_CONTAINER] ADD [DisbandDocument] bit NOT NULL CONSTRAINT [df_DisbandDocument] DEFAULT 0 WITH VALUES
GO
ALTER TABLE [STOR].[T_SHIPPING_CONTAINER] DROP CONSTRAINT [df_DisbandDocument]
GO

select mm.[ObjId]
into #MM
from STOR.T_MARK_MESSAGE mm
where mm.[Schema] = 912
group by [ObjId]


update sc 
set DisbandDocument = 1 
from STOR.T_SHIPPING_CONTAINER sc
join #MM mm on mm.[ObjId] = sc.ShippingContainerID
where sc.[Recursive] = 0

;with ShippingContainer
as
(
	SELECT	
		sc.ShippingContainerID
	FROM STOR.T_SHIPPING_CONTAINER sc
	join #MM mm on mm.[ObjId] = sc.ShippingContainerID
	where sc.[Recursive] = 1
	union all
	SELECT
		sc.ShippingContainerID
	FROM STOR.T_SHIPPING_CONTAINER sc
	join ShippingContainer scR on scR.ShippingContainerID = sc.SSCC_Parent
)
update sc
set sc.DisbandDocument = 1
from STOR.T_SHIPPING_CONTAINER sc
join ShippingContainer scc on scc.ShippingContainerID = sc.ShippingContainerID


update sc 
set DisbandDocument = 1 
from STOR.T_SHIPPING_CONTAINER sc
join #MM mm on mm.[ObjId] = sc.StorageInvoice

drop table #MM