Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SSRS Exec Storeprocedure -- ERROR

Posted on 2011-03-01
6
Medium Priority
?
955 Views
Last Modified: 2012-05-11
I am trying to create an SSRS report, buy I error.  Any I idea how to fix this?   I get the following error message ...

"There is an error in the query.  Invalid object name '#temp'
exec USP_CrossTab '
SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
FROM  dbo.VW_HPPATMFL HPP
INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP# AND HPP.PPPATno = MDP.PJPAT#
WHERE (HPP.PPTYPE = ''IP'') 
AND (HPP.PPMTCD <> ''D'') 
AND (mdp.PJAFTP = ''2'') 
AND (HPP.ccYRMM BETWEEN @YYYYMM1 AND @YYYYMM2)
AND (MDP.PDSPCL <> ''nur'') 
AND (HPP.ccGrpName = ''ELA'')

Open in new window

"

STORE PROCEDURE...
CREATE procedure USP_CrossTab (@Select varchar(1000),
    @PivotCol varchar(100),
    @Summaries varchar(100),
    @GroupBy varchar(100),
    @OtherCols varchar(100) = Null)
AS
set nocount on
set ansi_warnings off

declare @sql varchar(8000)
declare @Vals varchar(8000);
set @Vals = '';
set @OtherCols= isNull(', ' + @OtherCols,'')
create table #temp  (PV varchar(100))
set @sql = 'select distinct convert(varchar(100),' + @PivotCol + ') as PV FROM (' + @Select + ') A'

insert into #temp
exec (@sql)
select @Vals = @Vals + ', ' +
    replace(replace(@Summaries,'(','(CASE WHEN ' + @PivotCol + '=''' +
            PV +  ''' THEN '),')[', ' END) as [' + PV + '')
from #Temp
order by PV
drop table #Temp

set @sql = 'select ' + @GroupBy + @OtherCols + @Vals +
       ' from (' + @Select + ') A GROUP BY ' + @GroupBy
exec ( @sql )

set nocount off
set ansi_warnings on
GO

Open in new window

0
Comment
Question by:epicazo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 40

Expert Comment

by:lcohan
ID: 35012338
Try remove the # from the dynamic SQL like below:

 exec USP_CrossTab '
SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
FROM  dbo.VW_HPPATMFL HPP
INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP AND HPP.PPPATno = MDP.PJPAT#
WHERE (HPP.PPTYPE = ''IP'')
AND (HPP.PPMTCD <> ''D'')
AND (mdp.PJAFTP = ''2'')
AND (HPP.ccYRMM BETWEEN @YYYYMM1 AND @YYYYMM2)
AND (MDP.PDSPCL <> ''nur'')
AND (HPP.ccGrpName = ''ELA'')
0
 
LVL 40

Expert Comment

by:lcohan
ID: 35012351
oops - sorry I missed one...

 exec USP_CrossTab '
SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
FROM  dbo.VW_HPPATMFL HPP
INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRP AND HPP.PPPATno = MDP.PJPAT WHERE (HPP.PPTYPE = ''IP'')
AND (HPP.PPMTCD <> ''D'')
AND (mdp.PJAFTP = ''2'')
AND (HPP.ccYRMM BETWEEN @YYYYMM1 AND @YYYYMM2)
AND (MDP.PDSPCL <> ''nur'')
AND (HPP.ccGrpName = ''ELA'')
0
 

Author Comment

by:epicazo
ID: 35012494
I renamed the fields, but I still get the error message "There is an error in the query. Invalid Object name '#temp.'

exec USP_CrossTab '
SELECT HPP.ccGrpName, HPP.ccYRMM, MDP.PDSPCL, MDP.PDNAME AS DoctorName, 1 as cntr
FROM  dbo.VW_HPPATMFL HPP
INNER JOIN dbo.VW_MasterDoctorPt MDP ON HPP.PPGRPno = MDP.PJGRPno AND HPP.PPPATno = MDP.PJPATno
WHERE (HPP.PPTYPE = ''IP'') 
AND (HPP.PPMTCD <> ''D'') 
AND (mdp.PJAFTP = ''2'') 
AND (HPP.ccYRMM BETWEEN ''2009-01'' AND ''2010-12'')
AND (MDP.PDSPCL <> ''nur'') 
AND (HPP.ccGrpName = ''ELA'')
',
'ccYRMM',
'count(cntr)[]',
'PDSPCL,DoctorName'

Open in new window

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 

Author Comment

by:epicazo
ID: 35012508
It seems as if it does't like the temp table...
0
 
LVL 40

Accepted Solution

by:
lcohan earned 1000 total points
ID: 35018026
I believe the query below gives the error:

select @Vals = @Vals + ', ' +
    replace(replace(@Summaries,'(','(CASE WHEN ' + @PivotCol + '=''' +
            PV +  ''' THEN '),')[', ' END) as [' + PV + '')
from #Temp


try print instead of exec for command below see what you get to be executed - I think this exec errors out

--insert into #temp exec (@sql)
do this instead
print (@sql)


also check the column length fro #temp table as I don't think 100 varchar is sufficient - I would make it 8000
instead as it's a varchar:

#temp  (PV varchar(100))
0
 

Author Closing Comment

by:epicazo
ID: 35023352
Thanks.. ..  I still got an error with your suggestion, so I decited to dump the data into a TempTable which will be updated once a month anyway.  


DROP TABLE X_TEMP_Rpt_Admission20110302
-- ***************************************
-- The ReportName is YYYY-MM of previous month as DATEADD(m, - 1, GETDATE())
set @sql =
        'select  DATENAME(year, getdate()) + ''-'' + RIGHT (''0'' + CAST(DATEPART(month , DATEADD(m, - 1, GETDATE())) AS VarChar), 2) as ReportName, ' + @GroupBy + @OtherCols + @Vals +
       'into X_TEMP_Rpt_Admission20110302' +
       ' from (' + @Select + ') A GROUP BY ' + @GroupBy
exec ( @sql )
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question