Solved

SSRS Exec Storeprocedure -- ERROR

Posted on 2011-03-01
6
937 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
  • 3
  • 3
6 Comments
 
LVL 39

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 39

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

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

Accepted Solution

by:
lcohan earned 250 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now