第一次创建存储过程

时间:2014-02-05 10:47:50

标签: sql sql-server tsql stored-procedures

我正在尝试创建一个存储过程,以便第一次在Microsoft SSRS中生成试算平衡报告(是的,它也让我感到害怕!)并且遇到了一些困难。以前,我使用Business Objects或MS Access来促进源数据库的报告要求。

以下CREATE SP语句中的SQL查询正确运行,但在尝试将其转换为新的存储过程时遇到问题:

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GDM_TrialBalanceEL3_DEV_CVB]

 -- Add the parameters for the stored procedure here
@grpcode varchar(12),
@company varchar(12),
@currency varchar(12),
@begperiod int,
@startperiod int,
@endperiod int

AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;


--- DECLARE VARIABLES ----

DECLARE @NominalLevel int,
        @Currency varchar(3),
        @cmpcode varchar (12),
        @FromYear int,
        @FromPeriod int,
        @FromPeriodMMMM int,
        @ToYear int,
        @ToPeriod int,
        @ToPeriodMMMM int,
        @BookingType varchar (2)

--- SET PARAMETERS  (enter values) ---        

SET @NominalLevel = 3
SET @Currency = 'EUR'
SET @cmpcode = 'BEC001'
SET @FromYear = 2013
SET @FromPeriod = 1
SET @ToYear = 2013
SET @ToPeriod = 12
SET @BookingType = 'STCO'

--- COMBINE PARAMETERS ----

SET @FromPeriodMMMM = @FromYear * 10000 + @FromPeriod
SET @ToPeriodMMMM = @ToYear * 10000 + @ToPeriod

------OPENING BALANCE ----

SELECT a.cmpcode
   ,g.grpcode
   , SUBSTRING(a.el1,1,2) AS 'ST/CO'
   , a.el2
   , a.el3
   , CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END AS 'Account'
   , el.sname AS AccountName
   ,'Opening Balance' AS Movement
   , CASE WHEN @Currency='EUR' THEN SUM(a.valuehome) ELSE SUM(a.valuedual) END AS 'Amount'
   , CASE WHEN @Currency='EUR' AND a.deb_cred_ind=160 THEN SUM(a.valuehome)WHEN @Currency<>'EUR' AND a.deb_cred_ind=160 THEN SUM(a.valuedual)ELSE 0 END AS 'Credit'
   , CASE WHEN @Currency='EUR' AND a.deb_cred_ind=161 THEN SUM(a.valuehome)WHEN @Currency<>'EUR' AND a.deb_cred_ind=161 THEN SUM(a.valuedual)ELSE 0 END AS 'Debit'   
   , c.l1hdrtxt
   , c.l2hdrtxt
   , c.l3hdrtxt
   , c.leafhdrtxt

FROM        CODATEST_EU1.dbo.oas_agmlist b,
CODATEST_EU1.dbo.oas_docline a,
CODATEST_EU1.dbo.oas_himlist c,
CODATEST_EU1.dbo.oas_dochead d,
CODATEST_EU1.dbo.oas_perlist e,
CODATEST_EU1.dbo.oas_company f,
CODATEST_EU1.dbo.oas_grplist g,
CODATEST_EU1.dbo.oas_element el

WHERE 
((a.el3 BETWEEN b.el3incfrom AND b.el3incto) 
AND((b.el3excfrom is NULL) OR (a.el3 NOT BETWEEN b.el3excfrom AND b.el3excto)))
AND c.code='TRIALBALANCE'
AND c.grpcode=b.code 
AND a.cmpcode = d.cmpcode
AND a.doccode = d.doccode
AND a.docnum = d.docnum 
AND d.yr = e.yr 
AND d.period = e.period
AND d.cmpcode = e.cmpcode 
AND d.cmpcode = f.code
AND a.el1 = g.code 
AND a.cmpcode= g.cmpcode
AND g.elmlevel = 1
AND d.cmpcode = el.cmpcode 
AND el.code = CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END
AND el.elmlevel= @NominalLevel
AND a.cmpcode = @cmpcode
AND d.yr*10000+d.Period BETWEEN f.yrmin*10000  AND (@FromPeriodMMMM-1)
AND        g.grpcode = @BookingType
AND a.el3 < '60000000'        

GROUP BY        a.cmpcode
,g.grpcode
, SUBSTRING(a.el1,1,2) 
, a.el2
, a.el3
, CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END
, EL.sname
, a.deb_cred_ind
, c.l1hdrtxt
, c.l2hdrtxt
, c.l3hdrtxt
, c.leafhdrtxt

UNION ALL

----MOVEMENT ----

SELECT a.cmpcode
       ,g.grpcode
   , SUBSTRING(a.el1,1,2) AS 'ST/CO'
   , a.el2
   , a.el3
   , CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END AS Account
   , el.sname as AccountName
   ,'Movement' AS Movement
   , CASE WHEN @Currency='EUR' THEN SUM(a.valuehome) ELSE SUM(a.valuedual) END AS 'Amount'
   , CASE WHEN @Currency='EUR' AND a.deb_cred_ind=160 THEN SUM(a.valuehome)WHEN @Currency<>'EUR' AND a.deb_cred_ind=160 THEN SUM(a.valuedual)ELSE 0 END AS 'Credit'
   , CASE WHEN @Currency='EUR' AND a.deb_cred_ind=161 THEN SUM(a.valuehome)WHEN @Currency<>'EUR' AND a.deb_cred_ind=161 THEN SUM(a.valuedual)ELSE 0 END AS 'Debit'
   , c.l1hdrtxt
   , c.l2hdrtxt
   , c.l3hdrtxt
   , c.leafhdrtxt

FROM        CODATEST_EU1.dbo.oas_agmlist b,
CODATEST_EU1.dbo.oas_docline a,
CODATEST_EU1.dbo.Oas_himlist c,
CODATEST_EU1.dbo.oas_dochead d,
CODATEST_EU1.dbo.oas_perlist e,
CODATEST_EU1.dbo.oas_company f,
CODATEST_EU1.dbo.oas_grplist g,
CODATEST_EU1.dbo.oas_element el

WHERE 
((a.el3 BETWEEN b.el3incfrom AND b.el3incto) 
AND((b.el3excfrom is NULL) OR (a.el3 NOT BETWEEN b.el3excfrom AND B.el3excto)))
AND c.code='TRIALBALANCE'
AND c.grpcode=b.code 
AND a.cmpcode = d.cmpcode
AND a.doccode = d.doccode
AND a.docnum = d.docnum 
AND d.yr = e.yr 
AND d.period = e.period
AND d.cmpcode = e.cmpcode 
AND d.cmpcode = f.code
AND a.el1 = g.code 
AND a.cmpcode= g.cmpcode
AND g.elmlevel = 1
AND d.cmpcode = el.cmpcode 
AND el.code = Case When @NominalLevel=2 Then a.el2 else a.el3 end
AND el.elmlevel= @NominalLevel
AND a.cmpcode = @cmpcode
AND d.yr*10000+d.Period between @FromPeriodMMMM and @ToPeriodMMMM
AND        g.grpcode = @BookingType

GROUP BY        a.cmpcode
            ,g.grpcode
, SUBSTRING(a.el1,1,2) 
, a.el2
, a.el3
, CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END
, EL.sname
, a.deb_cred_ind
, c.l1hdrtxt
, c.l2hdrtxt
, c.l3hdrtxt
, c.leafhdrtxt

UNION ALL

---- RETAINED EARNINGS ---        

SELECT        a.cmpcode
,g.grpcode
,SUBSTRING(a.el1,1,2) as 'ST/CO'
,(SELECT el1.subselm from oas_element AS el1 WHERE el1.elmlevel=3 AND el1.cmpcode = @cmpcode AND el1.code='53100000')
,'53100000'
, CASE WHEN @NominalLevel=2 THEN (SELECT el1.subselm from oas_element AS el1 WHERE el1.elmlevel=3 AND el1.cmpcode = @cmpcode AND el1.code='53100000')  ELSE '53100000' END AS Account
, CASE WHEN @NominalLevel=2 THEN (SELECT el2.sname FROM oas_element AS el1 ,oas_element AS el2 WHERE el1.elmlevel=3 AND el2.elmlevel=2 AND el1.cmpcode = @cmpcode AND el1.code='53100000' AND el2.code=el1.subselm AND el2.cmpcode = el1.cmpcode)
  ELSE (SELECT el1.sname from oas_element AS el1 WHERE el1.elmlevel=3 AND el1.cmpcode = @cmpcode AND el1.code='53100000')END AS 'AccountName'
, 'Opening Balance'
    , CASE WHEN @Currency='EUR' THEN SUM(a.valuehome) ELSE SUM(a.valuedual) END AS 'Amount'
    , CASE WHEN @Currency='EUR' AND SUM(a.valuehome)<0  THEN SUM(a.valuehome) WHEN @Currency<>'EUR' AND SUM(a.valuedual)<0  THEN SUM(a.valuedual)ELSE 0 END AS 'Credit'
    , CASE WHEN @Currency='EUR' AND SUM(a.valuehome)>0  THEN SUM(a.valuehome) WHEN @Currency<>'EUR' AND SUM(a.valuedual)<0  THEN SUM(a.valuedual)ELSE 0 END AS 'Debit'
, 'BALANCE SHEET'        
, 'EQUITY'
, 'TOTAL RETAINED EARNINGS'
, 'RETAINED EARNINGS'

FROM CODATEST_EU1.dbo.oas_agmlist b,
CODATEST_EU1.dbo.oas_docline a,
CODATEST_EU1.dbo.Oas_himlist c,
CODATEST_EU1.dbo.oas_dochead d,
CODATEST_EU1.dbo.oas_perlist e,
CODATEST_EU1.dbo.oas_company f,
CODATEST_EU1.dbo.oas_grplist g,
CODATEST_EU1.dbo.oas_element el

WHERE 
((a.el3 BETWEEN b.el3incfrom AND b.el3incto) 
AND((b.el3excfrom is NULL) OR (a.el3 NOT BETWEEN b.el3excfrom AND B.el3excto)))
AND c.code='TRIALBALANCE'
AND c.grpcode=b.code 
AND a.cmpcode = d.cmpcode
AND a.doccode = d.doccode
AND a.docnum = d.docnum 
AND d.yr = e.yr 
AND d.period = e.period
AND d.cmpcode = e.cmpcode 
AND d.cmpcode = f.code
AND a.el1 = g.code 
AND a.cmpcode= g.cmpcode 
AND g.elmlevel = 1
AND d.cmpcode = el.cmpcode 
AND el.code = CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END
AND el.elmlevel= @NominalLevel
AND a.cmpcode = @cmpcode
AND d.yr*10000+d.Period between f.yrmin*10000 and (@FromPeriodMMMM-1)
AND        g.grpcode = @BookingType
AND a.el3 > '60000000'

GROUP BY        a.cmpcode,g.grpcode,substring(a.el1,1,2)


UNION ALL


---- CY PROFIT/LOSS ---        


SELECT        a.cmpcode
, g.grpcode
, substring(a.el1,1,2) as 'ST/CO'
, (SELECT el1.subselm from oas_element AS el1 WHERE el1.elmlevel=3 AND el1.cmpcode = @cmpcode AND el1.code='53100001')
, '53100001'
, CASE WHEN @NominalLevel=2 THEN(SELECT el1.subselm from oas_element AS el1 WHERE el1.elmlevel=3 AND el1.cmpcode = @cmpcode AND el1.code='53100001') 
  ELSE '53100001' 
  END AS Account
, CASE WHEN @NominalLevel=2 THEN (SELECT el2.sname FROM oas_element AS el1 ,oas_element AS el2 WHERE el1.elmlevel=3 AND el2.elmlevel=2 AND el1.cmpcode = @cmpcode AND el1.code='53100001' AND el2.code=el1.subselm AND el2.cmpcode = el1.cmpcode)
  ELSE (SELECT el1.sname from oas_element AS el1 WHERE el1.elmlevel=3 AND el1.cmpcode = @cmpcode AND el1.code='53100001')END AS 'AccountName'
, 'Movement'
    , CASE WHEN @Currency='EUR' THEN SUM(a.valuehome) ELSE SUM(a.valuedual) END AS 'Amount'
, CASE WHEN @Currency='EUR' AND SUM(a.valuehome)<0  THEN SUM(a.valuehome) WHEN @Currency<>'EUR' AND SUM(a.valuedual)<0  THEN SUM(a.valuedual)ELSE 0 END AS 'Credit'
        , CASE WHEN @Currency='EUR' AND SUM(a.valuehome)>0  THEN SUM(a.valuehome) WHEN @Currency<>'EUR' AND SUM(a.valuedual)<0  THEN SUM(a.valuedual)ELSE 0 END AS 'Debit'
, 'BALANCE SHEET'
, 'EQUITY'
, 'TOTAL RETAINED EARNINGS'
, 'RETAINED EARNINGS'

FROM CODATEST_EU1.dbo.oas_agmlist b,
CODATEST_EU1.dbo.oas_docline a,
CODATEST_EU1.dbo.Oas_himlist c,
CODATEST_EU1.dbo.oas_dochead d,
CODATEST_EU1.dbo.oas_perlist e,
CODATEST_EU1.dbo.oas_company f,
CODATEST_EU1.dbo.oas_grplist g,
CODATEST_EU1.dbo.oas_element el

WHERE 
((a.el3 BETWEEN b.el3incfrom AND b.el3incto) 
AND((b.el3excfrom is NULL) OR (a.el3 NOT BETWEEN b.el3excfrom AND B.el3excto)))
AND c.code='TRIALBALANCE'
AND c.grpcode=b.code 
AND a.cmpcode = d.cmpcode
AND a.doccode = d.doccode
AND a.docnum = d.docnum 
AND d.yr = e.yr 
AND d.period = e.period
AND d.cmpcode = e.cmpcode 
AND d.cmpcode = f.code
AND a.el1 = g.code 
AND a.cmpcode= g.cmpcode
AND g.elmlevel = 1
AND d.cmpcode = el.cmpcode 
AND a.el3 >= '60000000'
AND el.code = CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END
AND el.elmlevel= @NominalLevel
AND a.cmpcode = @cmpcode
AND d.yr*10000+d.Period between @FromPeriodMMMM and @ToPeriodMMMM
AND        g.grpcode = @BookingType

GROUP BY a.cmpcode ,g.grpcode , substring(a.el1,1,2)

当尝试运行SQL来创建存储过程时,我收到消息:

Msg 134,Level 15,State 1,Procedure GDM_TrialBalanceEL3_DEV_CVB,Line 26 变量名'@ Currency'已经被声明。变量名在查询批处理或存储过程中必须是唯一的。 消息137,级别15,状态1,过程GDM_TrialBalanceEL3_DEV_CVB,第40行 必须声明标量变量“@cmpcode”。

N.B。对于不同的变量,还有大约10个Msg 137的例子,但我想如果我理解1的逻辑,我应该能够解析其他9个?

从我的谷歌研究到目前为止,这可能是由于各种问题(标量变量= Go语句强制重新声明变量等)但在检查时没有GO语句,只有UNION和JOIN条件所以我是不确定它可能适用于可能的原因。

如果有人可以提供帮助(并解释为什么我没有再犯同样的错误),我将非常感激,因为我迫切需要立即创建此SP,而且似乎DBA资源因流感而下降..

2 个答案:

答案 0 :(得分:0)

您已将@Currency声明为两次..删除一个

最后添加一个括号

答案 1 :(得分:0)

将您的上一个WHERE更改为

WHERE 
((a.el3 BETWEEN b.el3incfrom AND b.el3incto) 
AND((b.el3excfrom is NULL) OR (a.el3 NOT BETWEEN b.el3excfrom AND B.el3excto)) -- one to many here (removed)
AND c.code='TRIALBALANCE'
AND c.grpcode=b.code 
AND a.cmpcode = d.cmpcode
AND a.doccode = d.doccode
AND a.docnum = d.docnum 
AND d.yr = e.yr 
AND d.period = e.period
AND d.cmpcode = e.cmpcode 
AND d.cmpcode = f.code
AND a.el1 = g.code 
AND a.cmpcode= g.cmpcode
AND g.elmlevel = 1
AND d.cmpcode = el.cmpcode 
AND a.el3 >= '60000000'
AND el.code = CASE WHEN @NominalLevel=2 THEN a.el2 ELSE a.el3 END
AND el.elmlevel= @NominalLevel
AND a.cmpcode = @cmpcode
AND d.yr*10000+d.Period between @FromPeriodMMMM and @ToPeriodMMMM
AND        g.grpcode = @BookingType) -- closing bracket here added
GROUP BY a.cmpcode ,g.grpcode , substring(a.el1,1,2)