Robb Hill
asked on
Reformat SQL - so SSRS can read the columns
I have a stored proc..and due to the structure of this proc...the wizard in Reporting Services cannot detect this as a dataset.
Does anyone know how I might rewrite this so SSRS will read it and build columns, rows ..etc.
Also any suggestions on a better way to do this is ALWAYS welcome:)
Does anyone know how I might rewrite this so SSRS will read it and build columns, rows ..etc.
Also any suggestions on a better way to do this is ALWAYS welcome:)
USE [database]
GO
/****** Object: StoredProcedure [dbo].[spGetEmailListByClientSiteCode] Script Date: 3/23/2017 1:58:27 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[spGetEmailListByClientSiteCode]
@cSiteID varchar(Max)
,@StartDate datetime = '1/1/1970'
,@EndDate datetime = '1/1/9999'
AS
if OBJECT_ID('tempdb.dbo.#tmpEmail') is not null
drop table #tmpEmail
if OBJECT_ID('tempdb.dbo.#tmpConcat') is not null
drop table #tmpConcat
if OBJECT_ID('tempdb.dbo.#tmpPivot') is not null
drop table #tmpPivot
DECLARE @SQL varchar(max)
SET NOCOUNT ON;
select distinct
entity.entityid,
tc.cName,
tc.cFEIN,
'something' as Link
,email.subject
,email.sent
,email.EmailDate
,isnull(email.fileId,0) fileId
,xAddType.EmailAddressTypeId
,addr.EmailAddress
,case
when ea.[file] like '%Attachment(s).%'
then
1
else
0
end as AttachCnt
into #tmpEmail
from dbo.email
inner join EmailXEmailAddressType xAddType on xAddType.EmailId = email.EmailId
inner join EmailAddressType aType on aType.EmailAddressTypeId = xAddType.EmailAddressTypeId
inner join EmailAddress addr on addr.EmailAddressId = xAddType.EmailAddressId
left join EntityXEmail entity on entity.emailId = email.emailid
left join cadoc_system.dbo.tSite on tSite.nid = entity.entityid
left join emailattachment ea on ea.emailid = email.emailid
inner join
(SELECT distinct cadoc_system.dbo.tSite.cSiteCode, cadoc_system.dbo.tSite.cSiteName, cadoc_crm.dbo.tClient.cFEIN, cadoc_crm.dbo.tClient.cName
FROM cadoc_system.dbo.tSite
INNER JOIN cadoc_system.dbo.tSiteXCrmClient ON cadoc_system.dbo.tSite.nid = cadoc_system.dbo.tSiteXCrmClient.nidSite
INNER JOIN cadoc_crm.dbo.tClient ON cadoc_system.dbo.tSiteXCrmClient.nIdClient = cadoc_crm.dbo.tClient.nid
WHERE tClient.cType = 'Client' and cPSiteCode != '')tc
on tc.cSiteCode = entity.EntityId
where EmailDate between @StartDate and @EndDate
and tc.cSiteCode = @cSiteID or @cSiteID is null
--and email.fileId is not null and EntityId is not null
order by 1
--Concatenate the email addresses
SELECT p1.EntityId,p1.cName,p1.cFEIN,p1.Link,emailaddresstypeid, subject,EmailDate, fileId, sent, attachcnt,
(SELECT emailaddress + ', '
FROM #tmpemail p2
WHERE p2.emailaddresstypeid = p1.emailaddresstypeid
and p2.fileId = p1.fileId
ORDER BY emailaddress
FOR XML PATH('')
) AS emails
into #tmpConcat
FROM #tmpemail p1
GROUP BY EntityId,cName,cFEIN,Link, emailaddresstypeid, subject,EmailDate, fileId, sent, attachcnt
--pivot results into separate From, To and CC fields
select EntityId,cName,cFEIN,Link, subject , EmailDate, fileId, sent, attachcnt
,[1] as [From]
,[2] as [To]
,[3] as [CC]
,[4] as [BCC]
,[5] as [Cataloged]
into #tmpPivot
from #tmpConcat
pivot
( max(emails)
for EmailAddressTypeId in ([1],[2],[3],[4],[5])
) as PivotTable
--Strip trailing comma off From, To, CC, BCC & Cataloged
select EntityId as SiteCode,cName as ClientName, cFEIN as ClientId, Link as EmailLink,subject as EmailSubject,EmailDate as EmailSentDate,fileID as nDocument,sent AttachCnt,
coalesce(cast(left([from],len([from])-1) as text),'') as [from],
coalesce(cast(left([to],len([to])-1) as text),'') as [To],
coalesce(cast(left(cc,len(cc)-1) as text),'') as CC,
coalesce(cast(left(bcc,len(bcc)-1) as text),'') as BCC,
coalesce(cast(left(Cataloged,len(Cataloged)-1) as text),'') as Cataloged,
sent
from #tmpPivot
order by EntityId
if OBJECT_ID('tempdb.dbo.#tmpEmail') is not null
drop table #tmpEmail
if OBJECT_ID('tempdb.dbo.#tmpConcat') is not null
drop table #tmpConcat
if OBJECT_ID('tempdb.dbo.#tmpPivot') is not null
drop table #tmpPivot
Oh, it's already there at the beginning, just remove from the end.
ASKER
ahh..so basically I am killing it in my ssrs report...lol..duh...let me try:)
ASKER
Actually that didnt work.
So I removed the ending.
Then I open up report builder...I chose the stored proc....it recognized the 3 params...but it doesnt populate any column data
So I removed the ending.
Then I open up report builder...I chose the stored proc....it recognized the 3 params...but it doesnt populate any column data
Starting with SQL Server 2008, you can use table variable in place of temp table. Stored procedure might have written in earlier version. You can modify to use table variable.
Table variables are discarded automatically and are visible only in the sp they are declared. Neat and clean!
Table variables are discarded automatically and are visible only in the sp they are declared. Neat and clean!
Hope you already checked that you are getting data in SQL Server Management Studio when called with identical parameters! Please confirm.
Almost certainly drop not required at both beginning and at the end. Just keep it at the beginning.
Almost certainly drop not required at both beginning and at the end. Just keep it at the beginning.
ASKER
What do you mean identitcal parameters?
As far as the table variable would you just define them all at the top...what would be best practice?
Can I just make sum assumptions on this data...or would you map it the same as the column its pulled from in the db?
DECLARE @Email TABLE
29(
30
34);
As far as the table variable would you just define them all at the top...what would be best practice?
Can I just make sum assumptions on this data...or would you map it the same as the column its pulled from in the db?
DECLARE @Email TABLE
29(
30
34);
ASKER
If your asking if the current stored proc works the answer is yes.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes...I do...going to take a few to do these table variables...have to dig through every table to find the the types..etc.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I went for just changing the last table that stores the results to a table varialbe..but syntax is still off...I am getting incorrect syntax at
this line:
into @EmailPivot
this line:
into @EmailPivot
DECLARE @EmailPivot TABLE
(
[EntityId] [int] NULL,
[cName] [varchar](80) NOT NULL,
[cFEIN] [varchar](50) NOT NULL,
[Link] [varchar](64) NULL,
[subject] [varchar](250) NOT NULL,
[EmailDate] [datetime] NOT NULL,
[fileId] [int] NOT NULL,
[sent] [bit] NOT NULL,
[attachcnt] [int] NOT NULL,
[From] [nvarchar](max) NULL,
[To] [nvarchar](max) NULL,
[CC] [nvarchar](max) NULL,
[BCC] [nvarchar](max) NULL,
[Cataloged] [nvarchar](max) NULL
)
--pivot results into separate From, To and CC fields
select EntityId,cName,cFEIN,Link, subject , EmailDate, fileId, sent, attachcnt
,[1] as [From]
,[2] as [To]
,[3] as [CC]
,[4] as [BCC]
,[5] as [Cataloged]
into @EmailPivot
from #tmpConcat
pivot
( max(emails)
for EmailAddressTypeId in ([1],[2],[3],[4],[5])
) as PivotTable
--Strip trailing comma off From, To, CC, BCC & Cataloged
select EntityId as SiteCode,cName as ClientName, cFEIN as ClientId, Link as EmailLink,subject as EmailSubject,EmailDate as EmailSentDate,fileID as nDocument,sent AttachCnt,
coalesce(cast(left([from],len([from])-1) as text),'') as [from],
coalesce(cast(left([to],len([to])-1) as text),'') as [To],
coalesce(cast(left(cc,len(cc)-1) as text),'') as CC,
coalesce(cast(left(bcc,len(bcc)-1) as text),'') as BCC,
coalesce(cast(left(Cataloged,len(Cataloged)-1) as text),'') as Cataloged,
sent
from @EmailPivot
order by EntityId
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Will not think that nvarchar(max) is a overkill, considering it can potentially accommodate 2gb of data? May be you should bit more pragmatic data lengths there once issue is resolved. May be nvarchar(4000).
If @ fails to resolve the original issue (which i think it will, if # is the issue then @ is also issue because technically it's the same) you may please take physical table path as suggested earlier ONLY FOR the last table to begin with.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So maybe this will work..havent tested it on the report....but as this is below...the peformance just went from less than 10 seconds to
still going at 2 minutes..
still going at 2 minutes..
--Concatenate the email addresses
SELECT p1.EntityId,p1.cName,p1.cFEIN,p1.Link,emailaddresstypeid, subject,EmailDate, fileId, sent, attachcnt,
(SELECT emailaddress + ', '
FROM #tmpemail p2
WHERE p2.emailaddresstypeid = p1.emailaddresstypeid
and p2.fileId = p1.fileId
ORDER BY emailaddress
FOR XML PATH('')
) AS emails
into #tmpConcat
FROM #tmpemail p1
GROUP BY EntityId,cName,cFEIN,Link, emailaddresstypeid, subject,EmailDate, fileId, sent, attachcnt
DECLARE @EmailPivot TABLE
(
[EntityId] [int] NULL,
[cName] [varchar](80) NOT NULL,
[cFEIN] [varchar](50) NOT NULL,
[Link] [varchar](64) NULL,
[subject] [varchar](250) NOT NULL,
[EmailDate] [datetime] NOT NULL,
[fileId] [int] NOT NULL,
[sent] [bit] NOT NULL,
[attachcnt] [int] NOT NULL,
[From] [nvarchar](max) NULL,
[To] [nvarchar](max) NULL,
[CC] [nvarchar](max) NULL,
[BCC] [nvarchar](max) NULL,
[Cataloged] [nvarchar](max) NULL
)
--pivot results into separate From, To and CC fields
insert into @EmailPivot ([EntityId], [cName],[cFEIN],[Link],[subject],[EmailDate],[fileId],[sent],[attachcnt],[From],[To],[CC],[BCC],[Cataloged])
select EntityId,cName,cFEIN,Link, subject , EmailDate, fileId, sent, attachcnt
,[1] as [From]
,[2] as [To]
,[3] as [CC]
,[4] as [BCC]
,[5] as [Cataloged]
from #tmpConcat
pivot
( max(emails)
for EmailAddressTypeId in ([1],[2],[3],[4],[5])
) as PivotTable
--Strip trailing comma off From, To, CC, BCC & Cataloged
select EntityId as SiteCode,cName as ClientName, cFEIN as ClientId, Link as EmailLink,subject as EmailSubject,EmailDate as EmailSentDate,fileID as nDocument,sent AttachCnt,
coalesce(cast(left([from],len([from])-1) as text),'') as [from],
coalesce(cast(left([to],len([to])-1) as text),'') as [To],
coalesce(cast(left(cc,len(cc)-1) as text),'') as CC,
coalesce(cast(left(bcc,len(bcc)-1) as text),'') as BCC,
coalesce(cast(left(Cataloged,len(Cataloged)-1) as text),'') as Cataloged,
sent
from @EmailPivot
order by EntityId
ASKER
well making the last result a table variable did not make it show up....
I read the article you linked to earlier..how would you write that query to get the fields in..that query is so complex I kept getting errors on making the columns as null
I
I read the article you linked to earlier..how would you write that query to get the fields in..that query is so complex I kept getting errors on making the columns as null
I
Sorry about that! Considering I think I am good at improving performance!
Lunch time...expect delays in responses. Meanwhile, please post only the portion of query you are referring to.
ASKER
ok thanks...
ASKER
Thanks!!!
So to trick it...I actually did the select like this versus the example.
Select 'ColumnName' as samplevalue,, 'ColumnName2' as samplevalue
this worked better than the example...
then I replaced this embedded SQl with the stored proc....now I can start off with the wizard.
Its still buggy trying to do advanced changes...but this is ok if your just trying to do a simple wizard.
Thanks again,
So to trick it...I actually did the select like this versus the example.
Select 'ColumnName' as samplevalue,, 'ColumnName2' as samplevalue
this worked better than the example...
then I replaced this embedded SQl with the stored proc....now I can start off with the wizard.
Its still buggy trying to do advanced changes...but this is ok if your just trying to do a simple wizard.
Thanks again,
Cannot agree more!
Just to console you, let me add this:
I had to design a SSRS where the called sp had a last select statement as shown below:
Just to console you, let me add this:
I had to design a SSRS where the called sp had a last select statement as shown below:
if flag = 1
select col1 from ctable
else if flag = 2
select val1, val2 from vtable
else if flag = 3
select some1, some2, some3 from sometable
ASKER
that one looks rather interesting. So in this case I am assuming the wizard had no clue what the layout would be as it was totally dynamic.
To begin with, try moving last 3 if...drop statements at the beginning of the stored proc.
To my knowledge, your last statement should be select.