在使用 SSRS 时,今天,我遇到了 2 个问题 1 仍未解决,我将发布另一个可怕的问题 :) 好吧,问题是:我有一个存储过程,它创建一个 #Temp 并最终使用 PIVOT 的数据表达。而且,存储过程本身在 SSMS 和 Visual Basic 6.0 中也可以正常运行,但是在使用 SSRS 中的该过程时报告它在 Pivot Expression 处显示错误。以下是屏幕截图,请查看并建议我一个想法。
这是一个存储过程:
ALTER PROCEDURE [dbo].[S_NRB_9_8_REPORT](@SCRCODE AS VARCHAR(20),
@CUREDATE VARCHAR(10),
@DTNAME VARCHAR(50),
@BR_CODE VARCHAR(50),
@CENTRALIZED VARCHAR(3))
WITH RECOMPILE
AS BEGIN
SET NOCOUNT ON;
DECLARE @BRCODE VARCHAR(3)
DECLARE @DTBASE VARCHAR(50)
DECLARE @CAT_TYPE_CODE VARCHAR(50)
DECLARE @AC_TYPE_SUB_TYPE_NAME VARCHAR(200)
DECLARE @CODESTR VARCHAR (1000)
DECLARE @CODESTR1 VARCHAR (1000)
SET @BRCODE=''
SET @DTBASE=''
SET @AC_TYPE_SUB_TYPE_NAME=''
SET @CODESTR=''
SET @CODESTR1=''
SELECT TOP 1 @CAT_TYPE_CODE=CAT_TYPE_CODE FROM REPORT_CAT_TYPE_CODE WHERE SCREEN_CODE =@SCRCODE
IF @CAT_TYPE_CODE='' OR @CAT_TYPE_CODE IS NULL
RETURN
CREATE TABLE [dbo].[#TEMPACTYPE](
[BR_CODE] [varchar](3) NULL,
[CN] [varchar](50) NULL,
[CS] [varchar](50) NULL,
[BAL] decimal(18, 2) NULL,
[AC_TYPE_SUB_TYPE_NAME] [varchar](50) NULL
) ON [PRIMARY]
IF LEN(@BR_CODE)>0
EXEC('DECLARE CUR INSENSITIVE CURSOR FOR SELECT BR_CODE FROM '+@DTBASE+'.DBO.BRANCH B (NOLOCK) WHERE BR_CODE='''+@BR_CODE+''' AND INTEGRATED=''YES'' AND APPROVED=''YES'' ORDER BY BR_CODE')
ELSE
EXEC('DECLARE CUR INSENSITIVE CURSOR FOR SELECT BR_CODE FROM '+@DTBASE+'.DBO.BRANCH B (NOLOCK) WHERE INTEGRATED=''YES'' AND APPROVED=''YES'' ORDER BY BR_CODE')
OPEN CUR
FETCH NEXT FROM CUR INTO @BRCODE
While @@FETCH_STATUS = 0
Begin
IF @CENTRALIZED='YES'
SET @DTBASE = @DTNAME
ELSE
SET @DTBASE = Left(@DTNAME, 13) + @BRCODE
EXEC('INSERT INTO #TEMPACTYPE
SELECT '''+@BRCODE+''' AS BR_CODE,T1.CAT_NAME AS CN,T1.CODES AS CS,SUM(T1.C_BAL)AS BAL,T1.AC_TYPE_SUB_TYPE_NAME FROM
(SELECT C_BAL,ATST.AC_TYPE_SUB_TYPE_NAME,CD.CAT_NAME,CD.CODE_STRING AS CODES
FROM
(SELECT AC_GROUP_CODE,CUR_CODE,GL_CODE FROM '+@DTBASE+'.dbo.AC_GROUP_GL_MAP WHERE NAMED_AC_CODE =''0301'') MAP,
(SELECT AC_GROUP_CODE,CUR_CODE,AC_NO FROM '+@DTBASE+'.dbo.DEPOSIT_AC_MAST WHERE BR_CODE='''+@BRCODE+''') DAM,
(SELECT TRAN_DATE,AC_NO,GL_CODE,PRODUCT_CODE,SUM(CLS_BAL) AS C_BAL FROM '+@DTBASE+'.dbo.AC_BAL WHERE BR_CODE='''+@BRCODE+''' GROUP BY TRAN_DATE,AC_NO,GL_CODE,PRODUCT_CODE) WD,
(SELECT * FROM '+@DTBASE+'.dbo.CAT_CODING where BR_CODE='''+@BRCODE+''' AND CAT_TYPE_CODE ='''+@CAT_TYPE_CODE+''') AS CC,
(SELECT * FROM '+@DTBASE+'.dbo.AC_TYPE_SUB_TYPE) AS ATST,
(SELECT * FROM '+@DTBASE+'.dbo.AC_GROUP) AS AG,
(SELECT * FROM '+@DTBASE+'.dbo.CAT_DETL) AS CD
WHERE
DAM.AC_GROUP_CODE =MAP.AC_GROUP_CODE
AND DAM.CUR_CODE =MAP.CUR_CODE
AND WD.GL_CODE =MAP.GL_CODE
AND CC.ENTITY_NO=DAM.AC_NO
AND ATST.AC_TYPE_CODE=AG.AC_TYPE_CODE
AND ATST.AC_TYPE_SUB_TYPE_CODE=AG.AC_TYPE_SUB_TYPE_CODE
AND AG.AC_GROUP_CODE=DAM.AC_GROUP_CODE
AND CD.CAT_TYPE_CODE=CC.CAT_TYPE_CODE
AND CD.CAT_CODE=CC.CAT_CODE
AND CD.CAT_TYPE_CODE='''+@CAT_TYPE_CODE+'''
AND WD.TRAN_DATE = (SELECT MAX(TRAN_DATE) FROM '+@DTBASE+'.dbo.AC_BAL WHERE BR_CODE ='''+@BRCODE+''' AND AC_NO = DAM.AC_NO AND TRAN_DATE <='''+@CUREDATE+''' AND GL_CODE=MAP.GL_CODE)
AND DAM.AC_NO=WD.AC_NO
UNION ALL
SELECT 0,ATST.AC_TYPE_SUB_TYPE_NAME,CAT_NAME,CODE_STRING AS CODES FROM '+@DTBASE+'.dbo.CAT_DETL AS CD,'+@DTBASE+'.dbo.AC_TYPE_SUB_TYPE AS ATST
WHERE CAT_TYPE_CODE='''+@CAT_TYPE_CODE+''' AND CAT_CODE NOT IN (SELECT CAT_CODE FROM '+@DTBASE+'.dbo.CAT_CODING WHERE BR_CODE='''+@BRCODE+''' AND CAT_TYPE_CODE='''+@CAT_TYPE_CODE+''')
AND ATST.AC_TYPE_CODE=''03''
) T1
GROUP BY T1.AC_TYPE_SUB_TYPE_NAME,CAT_NAME,CODES
ORDER BY CODES
')
FETCH NEXT FROM CUR INTO @BRCODE
END
DEALLOCATE CUR
DECLARE CUR INSENSITIVE CURSOR FOR SELECT DISTINCT AC_TYPE_SUB_TYPE_NAME FROM #TEMPACTYPE
OPEN CUR
Fetch Next from CUR Into @AC_TYPE_SUB_TYPE_NAME
While @@FETCH_STATUS = 0
Begin
IF @CODESTR =''
BEGIN
SET @CODESTR = 'ISNULL(['+@AC_TYPE_SUB_TYPE_NAME+'],0) AS ['+@AC_TYPE_SUB_TYPE_NAME+']'
SET @CODESTR1 = '['+@AC_TYPE_SUB_TYPE_NAME+']'
END
ELSE
BEGIN
SET @CODESTR = @CODESTR+',ISNULL(['+@AC_TYPE_SUB_TYPE_NAME+'],0) AS ['+@AC_TYPE_SUB_TYPE_NAME+']'
SET @CODESTR1 = @CODESTR1+',['+@AC_TYPE_SUB_TYPE_NAME+']'
END
Fetch Next from CUR Into @AC_TYPE_SUB_TYPE_NAME
END
DEALLOCATE CUR
EXEC ('Select CS,CN,'+@CODESTR+',TOTAL
from (Select CN,CS,BAL,[AC_TYPE_SUB_TYPE_NAME] from #TEMPACTYPE) ps pivot (SUM([BAL])
for [AC_TYPE_SUB_TYPE_NAME] in ('+@CODESTR1+',TOTAL)) pvt
Order by CS')
DROP TABLE #TEMPACTYPE
END
GO
并且,数据集设计面板:
但是,存储过程在 SSMS 中运行良好:
我正在使用 SSRS 2008 R2。
请帮帮我。并且,提前致谢。