IF EXISTS(SELECT 1 FROM #_datasource#.sys.Tables WHERE Name = '#_baseTableName##_tpfSuffix#') BEGIN SELECT 1 AS RES END ELSE BEGIN SELECT 0 AS RES END select DISTINCT ts.distributorId, ts.DistributorName from bi.dbo.tbl_TPM_TradeSummary_MFR#attributes.ownerid# ts with (nolock) where ts.ownerid = and ts.ownerType = order by ts.DistributorName