2017-08-28 29 views
0

我使用的是帶兩個參數(@活動,@發送resultCode)查詢來填充表3列(「CAMPAIGNNAME」,「處置」,」計數「),但是當這些參數中的任何一個參數不存在於數據庫中時,表中都沒有填充。有沒有辦法讓它填充兩個參數的計數爲0?另外,我已經設置了多個參數可供選擇。我已經嘗試過IIF(IsNothing()...,IIF(***。value = null或「」),但仍然沒有做我想做的事情。迴應:Visual Studio 2010的報表服務 - 如何填充東西時,不返回任何

SELECT databasename, callresultdescription, count(*) as Count 
FROM bpsql00.[histCallCenterStats].[dbo].[CallResults] 
WHERE databasename IN(@campaign) AND callresultcode IN(@resultcode) 
GROUP BY databasename, callresultdescription 

的callresultdescription是AKA配置

+0

你可以分享你現在在主查詢什麼代碼或等同? –

+0

@JerryRitcey --select數據庫名稱,callresultdescription,從bpsql00計數(*)作爲計數[histCallCenterStats] [DBO] [CallResults]其中數據庫名稱中(@campaign)和callresultcode在(@resultcode)基團由數據庫名稱,callresultdescription。 - ----本callresultdescription是AKA處置 - –

+0

'IIF(***值= NOTHING,0,***價值。)'? – BJones

回答

0

所以終於想通了。我得出的結論是,該計劃是有限的,我想要做的。所以...爲什麼不讓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 
0

您可以使用IF語句在SQL查詢中實現這一點:在註釋每個問題

IF EXISTS (SELECT 1 FROM bpsql00.[histCallCenterStats].[dbo].[CallResults] WHERE databasename IN (@campaign) AND callresultcode IN (@resultcode)) 
    SELECT databasename 
      , callresultdescription 
      , [Count] = COUNT(*) 
    FROM bpsql00.[histCallCenterStats].[dbo].[CallResults] 
    WHERE databasename IN (@campaign) 
      AND callresultcode IN (@resultcode) 
    GROUP BY databasename , 
      callresultdescription; 
ELSE 
    SELECT databasename = @campaign 
     , callresultdescription = @resultcode 
     , [Count] = 0 

編輯:

它時,你需要返回多值參數得到棘手。如果您使用的是SQL 2016,則可以使用新的TSQL STRING_SPLIT函數來分隔它們的逗號分隔選擇。您還可以在the interwebs上找到以前版本的SQL的拆分函數。但最簡單的解決方案是讓查詢不返回任何內容,並將Tablix的NoRowsMessage設置爲通知客戶端。您可以使用表達式像

="No records found in the selected campaigns (" & _ 
    Parameters!campaign.Value & ") and result codes (" & _ 
    Parameters!resultcode.Value & ")." 

,爲用戶被搜查什麼的記錄,並沒有什麼發現,以符合他們的標準。

+0

是的!真棒。我無法弄清楚,因爲我感到困惑。 SSRS使用IIF就好像出於某種原因,我正在做類似的事情,但是當它只應該是IF時,我進入了IIF。謝謝@Russell Fox –

+0

是的,在語言之間切換可能會很痛苦。顯然,他們將IIF添加到SQL 2012中,所以它只會變得更糟!一定要將此標記爲答案,以便具有相同問題的其他人可以找到答案。 –

+0

哦,你知道羅素嗎,我已經設置好了,這樣就可以選擇參數中的多個廣告系列和結果碼了。如果選擇了多個,它將​​顯示那些返回值而不顯示那些沒有的值。只有當它沒有返回一個值的時候,它纔會轉到其他地方。即便如此,無論您選擇多少個沒有任何價值的廣告系列,它只會在表格中顯示1行。有任何想法嗎? –

0

你可以聯合在一起:

--create table [CallResults] (databasename varchar(10),callresultdescription 
varchar(10),myvalue int) 

--insert into [CallResults] 
--values ('a','AA',1), 
--('b','BB',2), 
--('c','CC',3) 

--select * from [CallResults] 

declare @campaign varchar(10)='d',@resultcode varchar(10)='dd' ; 

SELECT databasename, callresultdescription, 
count(1) as [Count] 
FROM [CallResults] 
WHERE databasename IN (@campaign) 
AND callresultdescription IN (@resultcode) 
GROUP BY databasename, callresultdescription 
UNION 
SELECT [email protected], 
[email protected], 
0 as [Count] 
from [CallResults] 
where databasename not IN (@campaign) 
AND callresultdescription not IN (@resultcode)