IF object_id('tempdb..#tmpStoreConfigValue') IS NOT NULL
DROP TABLE [dbo].[#tmpStoreConfigValue]
        
CREATE TABLE #tmpStoreConfigValue
(
  EntityId BIGINT,
  Value VARCHAR(10),
  [Type] INT
)

INSERT INTO #tmpStoreConfigValue
EXEC qryMMSConfigurationSetting_GetByEnumForCache 100052 -- this is the config enum value for [Administration_InventoryItems_UseInFORMVendorItemAssignment]

select * from #tmpStoreConfigValue

DECLARE @IsGlobalChecked bit
SELECT @IsGlobalChecked =  CASE WHEN EXISTS(SELECT 1 FROM #tmpStoreConfigValue WHERE EntityId = 0 and Value = 'True')
                                THEN 1 ELSE 0
                            END

    select distinct e.EntityID, e.Entity, e.OwnerEntityID, e.EntityTypeID from tbEntity e
    left join tbvendorentity ve on e.EntityId = ve.EntityId
    left join tbvendor  v on ve.vendorid = v.vendorid
    left join #tmpStoreConfigValue scv on scv.EntityID = e.EntityID
    where v.VendorZoneId is not null
    and e.EntityTypeId = 4
    and e.[Status] = 'Open'
    and e.opendate >= DATEADD(YEAR,-10,GETDATE())
    and ( @IsGlobalChecked = 1 OR (scv.Value is not null and scv.Value = 'True'))
    and NOT EXISTS
    (SELECT vei.VendorCode FROM tbVendorEntityItem vei WHERE vei.vendorid = v.vendorid and vei.EntityID = e.EntityID)

DROP TABLE #tmpStoreConfigValue