use [test1_AlplaPROD2.0_Read] SELECT 'Alert! new blocking order: #' + cast(bo.HumanReadableId as varchar) + ' - ' + bo.ArticleVariantDescription as subject ,cast(bo.[HumanReadableId] as varchar) as blockingNumber ,bo.[ArticleVariantDescription] as article ,cast(bo.[CustomerHumanReadableId] as varchar) + ' - ' + bo.[CustomerDescription] as customer ,convert(varchar(10), bo.[BlockingDate], 101) + ' ' + convert(varchar(5), bo.[BlockingDate], 108) as blockingDate ,cast(ArticleVariantHumanReadableId as varchar) + ' - ' + ArticleVariantDescription as av ,case when bo.Remark = '' or bo.Remark is NULL then 'Please reach out to quality for the reason this was placed on hold as a remark was not entered during the blocking processs' else bo.Remark end as remark ,cast(FORMAT(TotalAmountOfPieces, '###,###') as varchar) + ' / ' + cast(LoadingUnit as varchar) as peicesAndLoadingUnits ,bo.ProductionLotHumanReadableId as lotNumber ,cast(osd.IdBlockingDefectsGroup as varchar) + ' - ' + osd.Description as mainDefectGroup ,cast(df.HumanReadableId as varchar) + ' - ' + os.Description as mainDefect ,lot.MachineLocation as line --,* FROM [blocking].[BlockingOrder] (nolock) as bo /*** get the defect details ***/ join [blocking].[BlockingDefect] (nolock) AS df on df.id = bo.MainDefectId /*** pull description from 1.0 ***/ left join [AlplaPROD_test1].[dbo].[T_BlockingDefects] (nolock) as os on os.IdGlobalBlockingDefect = df.HumanReadableId /*** join in 1.0 defect group ***/ left join [AlplaPROD_test1].[dbo].[T_BlockingDefectsGroups] (nolock) as osd on osd.IdBlockingDefectsGroup = os.IdBlockingDefectsGroup left join [productionControlling].[ProducedLot] (nolock) as lot on lot.id = bo.ProductionLotId where bo.[BlockingDate] between getdate() - 2 and getdate() + 3 and bo.BlockingTrigger = 1 -- so we only get the ir blocking and not coa --and HumanReadableId NOT IN ([sentBlockingOrders]) and bo.HumanReadableId > [lastBlocking]