I need to take the data from the customData column of the temp table #Camps and use it to run a selection. The data in the customData column is the full table name that I need to select from ( asdm.dbo.xxxx). I need the selection to do something like
select count(acctno) from asdm.dbo.xxxx
I am attaching the full code.
Thanks in advance
Text
declare@start datetime,@end datetime,@campaignId uniqueidentifier,@campaignName varchar(250),@segment varchar(50),@customData varchar(250)set @start = '2016-02-16'set @end = '2016-02-17'set @campaignId = '9FD97B9F-CB82-4C7F-91BE-50911B3319BE'set @segment = 'All'set nocount onif @start is null begin set @start = getdate() set @start = convert( datetime, convert(varchar, @start, 110 ) ) --remove time portionendif @end is null or @end < @start begin set @end = @start + 1endcreate table #camps( Seq int identity(1,1), Id uniqueidentifier, Name varchar(50), CustomData varchar(250) )if @campaignId is null and @campaignName is null begin insert #camps exec asdm.dbo.[sp_GetActiveCampaignsInPrd] @start, @endend else if @campaignId is not null begin insert #camps select Id, Name, CustomData from asdm.dbo.campaigns c with (nolock) where c.id = @campaignIdend else begin insert #camps select Id, Name, CustomData from asdm.dbo.campaigns c with (nolock) where c.Name = @campaignNameendcreate table #segments( Seq int identity(1,1), Segment varchar(50))if @segment != '_X_NONE_X_' begin if @segment = 'All' begin insert #segments exec clients.dbo.spx_CampaignSegments @campaignId, 'SEG' end else begin insert #segments select item from dbo.SplitStrings_CTE(@segment, N',') endend else begin set @segment = nullendcreate table #Stats( Campaign varchar(250) null, Segment varchar(50) null, Contacts int null, Pledges int null, Turndowns int null, ZBs int null, DNCs int null)create table #Leads( Campaign varchar(250) null, Segment varchar(50) null, Leads int null)beginInsert into #Stats (Campaign, Segment, Contacts, Pledges, Turndowns, ZBs, DNCs) select [Campaign] = case when grouping(c.Name)=1 then 'Total' else c.Name end, [Segment] = case when grouping(cell.CustomField)=1 then 'Total' else cell.CustomField end, [Contacts] = sum( coalesce(results.CO,0) ), [Pledges] = sum(coalesce(results.SA,0) ), [Turndowns] = sum(coalesce(results.TD,0) ), [ZBs] = sum(coalesce(results.ZB,0) ), [DNCs] = sum(coalesce(results.DNC,0) ) from asdm.dbo.statCell cell with (nolock) left join asdm.dbo.StatResults results with (nolock) on cell.Id = results.CellId left join #camps c with (nolock) on cell.campaignId = c.Id where cell.start >= @start and cell.start < @end and cell.CampaignId in ( select Id from #camps ) and cell.CustomField in ( select Segment from #segments ) and c.Name is not null group by c.Name, cell.CustomField with rollup order by grouping(c.Name), c.Name, grouping(cell.CustomField), cell.CustomField Insert into #Leads (Campaign, Segment, Leads) select [Campaign] = case when grouping(c.Name)=1 then 'Total' else c.Name end, [Segment] = case when grouping(cell.CustomField)=1 then 'Total' else cell.CustomField end, [Leads] = c.customData from asdm.dbo.statCell cell with (nolock) left join #camps c with (nolock) on cell.campaignId = c.Id and cell.CampaignId in ( select Id from #camps ) and cell.CustomField in ( select Segment from #segments ) and c.Name is not null group by c.Name, cell.CustomField with rollup order by grouping(c.Name), c.Name, grouping(cell.CustomField), cell.CustomField --SUMMARY SECTION select [Campaign] = s.Campaign, [Segment] = s.Segment, [Leads] = l.Leads, [Pledges] = ISNULL(s.Pledges,0), [Turndowns] = s.Turndowns, [ZBs] = s.ZBs, [DNCs] = s.DNCs, [Contacts] = s.Contacts, [PledgeRate] = coalesce(round((convert(float,s.Pledges) / nullif(convert(float,s.Contacts),0))*100.,2),0) from #stats as s with (nolock) left outer join #leads as l with (nolock) on s.Campaign = l.Campaign and s.Segment = l.Segment end--debug--select * from #stats--select * from #leadsdrop table #campsdrop table #statsdrop table #leadsdrop table #segments
drop table #segments