use AlplaPROD_test1 declare @intervalCheck as int = '[interval]' /* Monitors alpla purchase for thing new. this will not update unless the order status is updated. this means if a user just reopens the order it will update but everything changed in the position will not be updated until the user reorders or cancels the po */ select IdBestellung as apo ,po.revision as revision ,po.Bestaetigt as confirmed ,po.status ,case po.Status when 1 then 'Created' when 2 then 'Ordered' when 22 then 'Reopened' when 4 then 'Planned' when 5 then 'Partly Delivered' when 6 then 'Delivered' when 7 then 'Canceled' when 8 then 'Closed' else 'Unknown' end as statusText ,po.Add_User ,po.Add_Date ,po.Upd_User ,po.Upd_Date ,po.Bemerkung as remark ,po.IdJournal as journal -- use this to validate if we used it already. ,isnull(( select o.IdArtikelVarianten as av ,a.Bezeichnung as alias ,Lieferdatum as deliveryDate ,cast(BestellMenge as decimal(18,2)) as qty ,cast(BestellMengeVPK as decimal(18,0)) as pkg ,cast(PreisProEinheit as decimal(18,0)) as price ,PositionsStatus ,case PositionsStatus when 1 then 'Created' when 2 then 'Ordered' when 22 then 'Reopened' when 4 then 'Planned' when 5 then 'Partly Delivered' when 6 then 'Delivered' when 7 then 'Canceled' when 8 then 'Closed' else 'Unknown' end as statusText ,o.upd_user ,o.upd_date from T_Bestellpositionen (nolock) as o left join T_Artikelvarianten as a on a.IdArtikelvarianten = o.IdArtikelVarianten where o.IdBestellung = po.IdBestellung for json path ), '[]') as postion --,* from T_Bestellungen (nolock) as po where po.Upd_Date > dateadd(MINUTE, -@intervalCheck, getdate())