所以終於想通了。我得出的結論是,該計劃是有限的,我想要做的。所以...爲什麼不讓SQL爲我做,我可以調用一個存儲過程。答對了。我也必須創建一個函數。對於任何需要這樣的東西的人來說。 存儲過程,我創建:
alter procedure [dbo].[rs_Query]
@campaign varchar (100),
@resultcode varchar (100)
as
Begin
declare @var_campaign varchar(100)
declare @var_resultcode varchar(100)
declare @c table(ID int identity, databasename varchar(100))
declare @r table(ID int identity, callresultcode varchar(100))
insert into @c select element from dbo.func_split(@campaign, ',')
insert into @r select element from dbo.func_split(@resultcode,',')
declare @dbcnt int --count of campaigns selected
declare @crcnt int --count of dispositions selected
declare @crrow int --row id for campaigns selected
declare @dbrow int --row id for dispositions selected
declare @tempdbname varchar(50) --temp campaign name
declare @tempcr varchar(50) --temp call result name
declare @t table (databasename varchar(100), callresultdescription varchar (100), Count int)
declare @count int
select @dbcnt = count(*) from @c
select @crcnt = count(*) from @r
select @dbrow = 1
select @crrow = 1
while @dbcnt >= @dbrow
begin
set @tempdbname = (select databasename
from bpsql00.callcenteraux.dbo.DailyReportsCampaign
where databasename = (select databasename from @c where id = @dbrow))
set @crrow = 1
while @crcnt >= @crrow
begin
set @tempcr = (select CallResultDescription
from CallResultCode
where CallResultCode = (select CallResultCode from @r where id = @crrow));
if exists(select 1 from bpsql00.[histCallCenterStats].[dbo].[CallResults]
where CallResultCode = (select CallResultCode from @r where id = @crrow) and databasename = @tempdbname)
begin
select @count = count(*) from bpsql00.[histCallCenterStats].[dbo].[CallResults]
where CallResultCode = (select CallResultCode from @r where id = @crrow) and databasename = @tempdbname
insert into @t values(@tempdbname,@tempcr,@count)
end
else
begin
insert into @t values(@tempdbname,@tempcr,0)
end
set @crrow = @crrow + 1
end
set @dbrow = @dbrow + 1
end
select * from @t
end
和功能,我創建:
ALTER FUNCTION [dbo].[func_Split]
(
@DelimitedString varchar(8000),
@Delimiter varchar(100)
)
RETURNS @tblArray TABLE
(
ElementID int IDENTITY(1,1), -- Array index
Element varchar(1000) -- Array element contents
)
AS
BEGIN
-- Local Variable Declarations
-- ---------------------------
DECLARE @Index smallint,
@Start smallint,
@DelSize smallint
SET @DelSize = LEN(@Delimiter + 'x') - 1
-- Loop through source string and add elements to destination table array
-- ----------------------------------------------------------------------
WHILE LEN(@DelimitedString) > 0
BEGIN
SET @Index = CHARINDEX(@Delimiter, @DelimitedString)
IF @Index = 0
BEGIN
INSERT INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(@DelimitedString)))
BREAK
END
ELSE
BEGIN
INSERT INTO
@tblArray
(Element)
VALUES
(LTRIM(RTRIM(SUBSTRING(@DelimitedString, 1,@Index - 1))))
SET @Start = @Index + @DelSize
SET @DelimitedString = SUBSTRING(@DelimitedString, @Start , LEN(@DelimitedString) - @Start + 1)
END
END
RETURN
END
你可以分享你現在在主查詢什麼代碼或等同? –
@JerryRitcey --select數據庫名稱,callresultdescription,從bpsql00計數(*)作爲計數[histCallCenterStats] [DBO] [CallResults]其中數據庫名稱中(@campaign)和callresultcode在(@resultcode)基團由數據庫名稱,callresultdescription。 - ----本callresultdescription是AKA處置 - –
'IIF(***值= NOTHING,0,***價值。)'? – BJones