Solved

SSRS Exec Storeprocedure -- ERROR

Posted on 2011-03-01
6
936 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Earlier I wrote an article about the new lookup functions (http://www.experts-exchange.com/A_3433.html) that ship with SQL Server 2008 R2.  In this article I’m going to show you another new feature of SSRS 2008 R2, this time in the vis…
A recent questions about how to add SSRS named instances, couldn't find any that talks about SQL server 2008, anyway I decided to help by creating some screen shots. The installation is straightforward, you just pop the SQL server 2008 installati…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 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

9 Experts available now in Live!

Get 1:1 Help Now