需要从选择列中的值中进行选择

时间:2016-02-17 20:40:10

标签: sql sql-server

我需要从临时表#Camps的customData列中获取数据并使用它来运行选择。 customData列中的数据是我需要从中选择的完整表名(asdm.dbo.xxxx)。我需要选择做类似

的事情
select count(acctno) from asdm.dbo.xxxx

我附上完整的代码。

提前致谢

   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 on

if @start is null begin
     set @start = getdate()
     set @start = convert( datetime, convert(varchar, @start, 110 ) ) --remove time portion
end

if @end is null or @end < @start begin
     set @end = @start + 1
end

create 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, @end
end else if @campaignId is not null begin
    insert #camps
    select Id, Name, CustomData  from asdm.dbo.campaigns c with (nolock) where c.id = @campaignId
end else begin
     insert #camps
     select Id, Name, CustomData from asdm.dbo.campaigns c with (nolock) where c.Name = @campaignName
end

create 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',')
     end
end else begin
     set @segment = null
end



create 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
)

begin

Insert 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 #leads


drop table #camps
drop table #stats
drop table #leads
drop table #segments

1 个答案:

答案 0 :(得分:0)

您可以从其他字段(例如CustomData字段)构建varchar字符串,然后执行它。这样的事情(抱歉,如果语法不是100%,那么远离数据库)。

declare @execString nvarchar;
declare @tableName nvarchar;

set @tableName = 'users'
set @execString = 'select * from ' + @tableName
exec(@execString)