I'm working on creating a report that will tell me how many emails are assigned to a report subscription , for example a report called "Monthly Customer Activity - Default Setup" can have 2 email subscriptions and each subscription can have different email addresses , lets say 5 emails each subscription , at the end my report will display on one column 10 times the report Name and on the other column the 10 email addresses.
I'm working using the code below and is working most of the time but on some rows is not doing the email split . I'm using SQL Management Studio 2016 and I'm connecting to my SSRS 2016 Database
Use [ReportServer]
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
drop table #Temp
select
c.Name,
-- 'two' as OtherID,
Convert(XML,[ExtensionSettings]).value('(//ParameterValue/Value[../Name="TO"])[1]','nvarchar(max)') as email
INTO #Temp
FROM
dbo.[Catalog] c
INNER JOIN dbo.[Subscriptions] S ON c.ItemID = S.Report_OID
INNER JOIN dbo.ReportSchedule R ON S.SubscriptionID = R.SubscriptionID
INNER JOIN msdb.dbo.sysjobs J ON Convert(nvarchar(128),R.ScheduleID) = J.name
INNER JOIN msdb.dbo.sysjobschedules JS ON J.job_id = JS.job_id
;
WITH tmp(/*SomeID, OtherID,*/ Name, email,string) AS
(
SELECT
Name,
--OtherID,
LEFT(email, CHARINDEX(';', email + ';') - 1),
STUFF(email, 1, CHARINDEX(';', email + ';'), '')
FROM #Temp
UNION all
SELECT
Name,
--OtherID,
LEFT(email, CHARINDEX(';', email + ';') - 1),
STUFF(email, 1, CHARINDEX(';', email + ';'), '')
FROM tmp
WHERE
email != email
and email is not NULL
)
select * from #Temp
order by email
The desired results will be: https://imggmi.com/full/2019/1/17/a528cf472a96f5d2eff2759413b79814-full.png.html
Results I'm getting:
https://imggmi.com/full/2019/1/17/4ae835b3a877ad015af10372cf7f82e9-full.png.html
As you can see on the picture , is doing the split email on some rows but some others are still showing the emails together
Below is a sample test:
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL drop table #Temp
CREATE TABLE #Temp
(Name varchar (50) , LastStatus varchar (max) , IBOAccount varchar (10) , Email varchar (max))
GO
INSERT INTO #Temp
Select 'Report A','Email sent to [email protected]','47213','[email protected]' UNION ALL
Select 'Report A','Email sent to [email protected]','13983','[email protected]' UNION ALL
Select 'Report A','Email sent to [email protected]','437707','[email protected]' UNION ALL
Select 'Report B','Email sent to [email protected]','NULL','[email protected]' UNION ALL
Select 'Report C','Email sent to [email protected]','NULL','[email protected]' UNION ALL
Select 'Report C','Email sent to [email protected]','NULL','[email protected]' UNION ALL
Select 'Report C','Email sent to [email protected];[email protected]','170891','[email protected];[email protected]' UNION ALL
Select 'Report D','Done: 1 processed of 1 total; 0 errors.','NULL','[email protected];[email protected];[email protected];[email protected]'
GO