select uc.UserContextID 
INTO #Context
from VCLIB.T_USER_CONTEXTS uc
join VCLib.T_ENTITIES e on e.EntityID = uc.EntityID 
where e.EntName = 'PATIENT_APPOINTMENT_EXECUTION' and uc.Context = 'RemainsContextMass'

select '{Remains / BillOfGoods / DisplayName}' as AttributePath, 'Номенклатура товаров' as DisplayName,1 as ColumnIndex,344 as ColumnWidth,1 as Visible,0 as SortingIndex,0 as Sorting
into #Columns
union select '{Remains / Series}', 'Серия',2,50,1,0,0
union select '{Remains / ExpirationDate}', 'Срок годности',3,104,1,1,2
union select '{Remains / Amount}', 'Остаток',4,44,1,0,0
union select '{Remains / PackageType}', 'Вид упаковки',5,60,1,0,0
union select '{Amount}', 'Количество',6,55,1,0,0
union select '{Remains / SupplierContractAbr / Abr}', 'АБР',7,86,1,0,0

delete from 
VCLib.T_USER_COLUMNS where UserContextID in (select UserContextID from #Context) 

INSERT INTO [VCLib].[T_USER_COLUMNS]
           ([UserColsID]
           ,[AttributePath]
           ,[DisplayName]
           ,[ColumnIndex]
           ,[ColumnWidth]
           ,[Visible]
           ,[SortingIndex]
           ,[Sorting]
           ,[UserContextID])
select		newid()
           ,[AttributePath]
           ,[DisplayName]
           ,[ColumnIndex]
           ,[ColumnWidth]
           ,[Visible]
           ,[SortingIndex]
           ,[Sorting]
           ,[UserContextID]
from #Columns cc
join #Context c on c.UserContextID is not null


drop table #Columns
drop table #Context
