Solved

SSRS Exec Storeprocedure -- ERROR

Posted on 2011-03-01
6
945 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL, add where clause 5 24
Scheduled bat file step with psexec in SQLServer agent job only 2 20
TSQL XML Namespaces 7 24
SQL R 21 28
Introduction In the following article I’ll be discussing and demonstrating several different ways of how images can be put on a report. I’m using SQL Server Reporting Services 2008 R2 CTP, more precisely version 10.50.1352.12, but the methods ex…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 the fundamental information of how to create a table.

828 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