Solved

SSRS Exec Storeprocedure -- ERROR

Posted on 2011-03-01
6
947 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 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

740 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