what causes errror Must declare the scalar variable "@Subsidiary".

Posted on 2010-01-08
Last Modified: 2013-11-16
DECLARE            @BeginDate            DATETIME      = '07/01/2009';
                  @EndDate            DATETIME      = '01/05/2010';
                  @Subsidiary     INT                  = 2;
                  @Contract            VARCHAR(20)      = 'KD228';

SET @BeginDate = '07/01/2009';
      @EndDate = '01/05/2010';
      @Subsidiary = 2;
      @Contract= 'KD228';


                  WHEN @Subsidiary = 1
                  AND (RIGHT (RTRIM(R.CSTBTablCode),3)= 'DCF' AND TN.CSTBTablCode != '3')
                  THEN 'Error'
                  WHEN @Subsidiary = 1
                  AND (RIGHT (RTRIM(R.CSTBTablCode),3)= 'TNF' AND TN.CSTBTablCode = '3')
                  THEN 'Error'
                  WHEN @Subsidiary = 2
                  AND (SUBSTRING(R.CSTBTablCode,4,1)!= 'T' AND TN.CSTBTablCode != '3')
                  THEN 'Error'
                  WHEN @Subsidiary = 2
                  AND (SUBSTRING(R.CSTBTablCode,4,1) = 'T' AND TN.CSTBTablCode = '3')
                  THEN 'Error'
                  WHEN @Subsidiary = 2
                  AND (C.CSTBTablCode = '01' OR C.CSTBTablCode = '1')
                  THEN 'Error'
                  ELSE ''
            END                                                            AS ErrorFlag,
            TN.CSTBTablCode+'-'+TN.CSTBTablDesc            AS TANFType,
            R.CSTBTablCode                                          AS RU,
            C.CSTBTablCode                                          AS CostCenter,
                  WHEN ((@Subsidiary = 2 AND LEFT (RIGHT (R.CSTBTablCode,6),1) = '1')
                         OR (@Subsidiary = 1 AND RIGHT (LEFT (R.CSTBTablCode,3),1) = '1')) THEN 'AMH'
                  WHEN ((@Subsidiary = 2 AND LEFT (RIGHT (R.CSTBTablCode,6),1) = '2')
                         OR (@Subsidiary = 1 AND RIGHT (LEFT  (R.CSTBTablCode,3),1) = '2')) THEN 'ASA'
                  WHEN ((@Subsidiary = 2 AND LEFT (RIGHT (R.CSTBTablCode,6),1) = '3')
                         OR (@Subsidiary = 1 AND RIGHT (LEFT  (R.CSTBTablCode,3),1) = '3')) THEN 'CMH'
                  WHEN ((@Subsidiary = 2 AND LEFT (RIGHT (R.CSTBTablCode,6),1) = '4')
                         OR (@Subsidiary = 1 AND RIGHT (LEFT  (R.CSTBTablCode,3),1) = '4')) THEN 'CSA'
            END                                                      AS Prog,
            P.CSTBTablCode                                    AS ProcCode,
            P.CSTBStatOvcd                                    AS StatCode,
            F.CSTBTablDesc                                    AS Funding,
            CONVERT (varchar,T.ARTRTranDate,101)      AS BatchDate,
                  WHEN (T.ARTRDaodFlag = 1 AND T.ARTREditEror = 2) THEN 'Yes'
                  ELSE 'Error'
            END                                                      AS Transmit,
            T.ARTRTranCtrl                                    AS Control,
            S.ARSLOldcNumb                                    AS StaffID,
            S.ARSLSlmnDesc                                    AS Staff,
            R.CSTBStatCntr                                    AS RUContract,
            T.ARTRStatCntr                                    AS TransContract

FROM      KIS1.dbo.ARTran AS T

INNER       Join KIS1.dbo.CSTabl AS R ON R.CSTBRecdCtrl = T.ARTRReptUnit

INNER      JOIN KIS1.dbo.CSTabl AS F ON F.CSTBRecdCtrl =  R.CSTBSrvcFnd1

INNER       Join KIS1.dbo.CSTabl AS P ON P.CSTBRecdCtrl = T.ARTRSrvcCode

INNER      JOIN KIS1.dbo.CSTabl AS C ON C.CSTBRecdCtrl = T.ARTRStffCscn

INNER       JOIN KIS1.dbo.ARCust AS X ON X.ARCSCustCtrl = T.ARTRCustCtrl

INNER       JOIN KIS1.dbo.ARSale AS S ON S.ARSLSlmnCtrl = T.ARTRSaleCtrl

INNER      JOIN KIS1.dbo.CSAdmn AS A ON T.ARTRAdmsCtrl = A.CSADRecdCtrl

LEFT      JOIN KIS1.dbo.CSTabl AS TN ON TN.CSTBRecdCtrl = A.CSADTanfFlag

WHERE      T.ARTRSbsdCtrl = @Subsidiary AND
            T.ARTRPendFlag = 2 AND
            T.ARTRRecdType = 0 AND
            T.ARTREntyType = 191 AND
            --T.ARTRSendStat = 1 AND
            T.ARTRDaodFlag = 1 AND
            ((T.ARTRStatCntr = 'KD228') OR (R.CSTBStatCntr  LIKE '%KD228%'  AND T.ARTRStatCntr Is Null )) AND
            T.ARTRTranDate BETWEEN @BeginDate AND @EndDate

ORDER BY 1,4,3,T.ARTRTranCtrl
Question by:aircon
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    a typo?
    LVL 41

    Accepted Solution

    The problem is somewhere else:

    Msg 139, Level 15, State 1, Line 0
    Cannot assign a default value to a local variable.

    and some more problems...

    So, fix your declarations:

                   @EndDate DATETIME,
                   @Subsidiary INT,
                   @Contract VARCHAR(20);

    SET      @BeginDate = '07/01/2009'
    SET      @EndDate = '01/05/2010'
    SET      @Subsidiary = 2
    SET      @Contract= 'KD228'

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 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

    19 Experts available now in Live!

    Get 1:1 Help Now