从SQL 2005迁移到2012年

时间:2015-03-09 20:28:59

标签: sql sql-server sql-server-2005 sql-server-2012 outer-join

这是我在SQLServer 2005中编写的存储过程。现在我们转向SQL2012并在使用 *= 时遇到大量错误。我知道我必须使用LEFT OUTER JOIN语法,但不知怎的,我很难过。有人可以帮帮我吗 ?我在where子句中使用ABS的位置也会出错。

These 2 lines in question in WHERE CLAUSE BELOW
currhold.current_hold__001 *= #tmp_transac.current_hold__001 and
((abs(t_quantity_c)> 0.01 and #tmp_transac.current_hold__001 is null) or

Full SP就在这里

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[usp_getEPMData_GFS]
AS
  BEGIN
      SET NOCOUNT ON;

      IF OBJECT_ID('tempdb..#factortemp', 'u') IS NOT NULL
        BEGIN
            DROP TABLE #factortemp
        END

      IF OBJECT_ID('tempdb..#transactemp', 'u') IS NOT NULL
        BEGIN
            DROP TABLE #transactemp
        END

      SELECT *
      INTO   #transactemp
      FROM   transac
      WHERE  tran_type IN ( 'BUY', 'SHORT' )

      SELECT curr_factor,
             cusip
      INTO   #factortemp
      FROM   mbs_fact A
      WHERE  A.factor_date = (SELECT max(B.factor_date)
                              FROM   mbs_fact B
                              WHERE  B.cusip = A.cusip --and B.factor_date <= '10/30/2008'
                              GROUP  BY B.cusip)

      IF OBJECT_ID('tempdb..#pricetemp', 'u') IS NOT NULL
        BEGIN
            DROP TABLE #pricetemp
        END

      CREATE TABLE #pricetemp
        (
           price          NUMERIC(19, 8),
           cusip          VARCHAR(20),
           price_source   VARCHAR(20),
           Max_Price_Date SMALLDATETIME
        )

      IF OBJECT_ID('tempdb..#pricetemp1', 'u') IS NOT NULL
        BEGIN
            DROP TABLE #pricetemp1
        END

      IF OBJECT_ID('tempdb..#pricetemp2', 'u') IS NOT NULL
        BEGIN
            DROP TABLE #pricetemp2
        END

      SELECT cusip,
             max(price_date) AS MX_pxdate
      INTO   #pricetemp1
      FROM   pricing
      GROUP  BY cusip
      ORDER  BY 1

      SELECT A.cusip,
             B.Mx_pxdate,
             count(*) AS dupcount
      INTO   #pricetemp2
      FROM   pricing A,
             #pricetemp1 B
      WHERE  A.cusip = B.cusip
             AND A.price_date = B.MX_pxdate
      GROUP  BY A.cusip,
                B.Mx_pxdate

      INSERT INTO #pricetemp
      SELECT A.price,
             A.cusip,
             A.price_source,
             B.MX_pxdate
      FROM   pricing A,
             #pricetemp2 B
      WHERE  A.price_date = B.MX_pxdate
             AND A.cusip = B.cusip
             AND B.dupcount = 1

      INSERT INTO #pricetemp
      SELECT A.price,
             A.cusip,
             A.price_source,
             B.MX_pxdate
      FROM   pricing A,
             #pricetemp2 B
      WHERE  A.price_date = B.MX_pxdate
             AND A.cusip = B.cusip
             AND A.price_source = 'MANUAL'
             AND B.dupcount = 2

      IF OBJECT_ID('tempdb..#tmp_transac', 'u') IS NOT NULL
        BEGIN
            DROP TABLE #tmp_transac
        END

      SELECT DISTINCT current_hold__001,
                      sum(abs(transac.total_amount)) AS total_amount,
                      broker
      INTO   #tmp_transac
      FROM   transac
      WHERE  setl_y_n = 'N'
             AND replace(tran_type, ' ', '') IN (SELECT DISTINCT replace(tran_type, ' ', '') AS tran_type
                                                 FROM   ut_transac_types
                                                 WHERE  amount_sign = 'POSITIVE')
      GROUP  BY current_hold__001,
                broker

      DECLARE @System_Date SMALLDATETIME

      SELECT @System_Date = CONVERT(VARCHAR(12), dbo.uf_getGLDate(), 112)

      --ISB
      IF OBJECT_ID('tempdb..#tmpISB', 'u') IS NOT NULL
        BEGIN
            DROP TABLE #tmpISB
        END

      SELECT DISTINCT iss.user_desc1       AS Issuer_ISB,
                      Guarantor.user_desc1 AS Guarantor_ISB,
                      Security.Cusip
      INTO   #tmpISB
      FROM   Security
             LEFT OUTER JOIN Issuer Iss
               ON ( Security.issuer = Iss.issuer )
             LEFT OUTER JOIN Issuer Guarantor
               ON ( Security.Guarantor = Guarantor.issuer )

      --
      -----Existing EPM on Top
      SELECT CASE
               WHEN #factortemp.curr_factor = 0.00
                 THEN 0.00
               WHEN #factortemp.curr_factor IS NULL
                 THEN 0.00
               ELSE #factortemp.curr_factor
             END                                                                                                   AS curr_factor,
             CASE
               WHEN #pricetemp.price = 0.00
                 THEN 100.000000000
               WHEN #pricetemp.price IS NULL
                 THEN 100.000000000
               ELSE #pricetemp.price
             END                                                                                                   AS price,
             replace(security.description1, ',', ' ')                                                              AS description1,
             security.isin,
             portfoli.MGR1_TITLE                                                                                   AS branch,
             currhold.portfolio,
             replace(currhold.sec_type, ' ', '')                                                                   AS sec_type,
             CONVERT(FLOAT, currhold.CURRENT_HOLD__001) * 10000                                                    AS Trade_num,
             currhold.cusip,
             securtyp.prn_curr,
             Chartacc.securities,
             securitiesamt=CASE
                             WHEN SECURTYP.SEC_TYPE = 'RESIDCERT'
                               THEN CONVERT(DECIMAL(18, 2), CURRHOLD.GORIG_COST_C)
                             ELSE CONVERT(DECIMAL(18, 2), CURRHOLD.T_QUANTITY_C)
                           END,
             chartacc.INT_DUE_ACCRUED,
             INT_DUE_ACCRUEDAmt = ( CURRHOLD.T_ACCR_INTRST_C + CURRHOLD.T_INT_DIV_DUE_C ),
             chartacc.Discount,
             DiscountAmt = CASE
                             WHEN SECURTYP.SEC_TYPE = 'RESIDCERT'
                               THEN 0
                             WHEN ( CURRHOLD.GBOOK_VALUE_C - CURRHOLD.T_QUANTITY_C ) > 0
                               THEN 0
                             ELSE CONVERT(DECIMAL(18, 2), CURRHOLD.GBOOK_VALUE_C - CURRHOLD.T_QUANTITY_C)
                           END,
             chartacc.Premium,
             PREMIUMAmt=CASE
                          WHEN SECURTYP.SEC_TYPE = 'RESIDCERT'
                            THEN 0
                          WHEN ( CURRHOLD.GBOOK_VALUE_C - CURRHOLD.T_QUANTITY_C ) < 0
                            THEN 0
                          ELSE CONVERT(DECIMAL(18, 2), CURRHOLD.GBOOK_VALUE_C - CURRHOLD.T_QUANTITY_C)
                        END,
             chartacc.UNRLZD_GL_ASSET,
             UNRLZD_GL_ASSETAmt=isnull(CONVERT(DECIMAL(18, 2), ( CURRHOLD.GMARKET_VALUE_C - CURRHOLD.GBOOK_VALUE_C )), 0),
             chartacc.UNRLZD_GL_INC,
             UNRLZD_GL_INCAmt = isnull(CONVERT(DECIMAL(18, 2), ( CURRHOLD.GBOOK_VALUE_C - CURRHOLD.GMARKET_VALUE_C )), 0),
             chartacc.INTEREST_INCOME,
             Interest_incomeamt= CONVERT(DECIMAL(18, 2), CURRHOLD.GAMZ_YTD_P + CURRHOLD.T_ID_EARN_YTD_P),
             chartacc.RECVABLE_BROKER,
             RECVABLE_BROKERAmt= CASE
                                   WHEN CURRHOLD.STATUS IN ( 'SELL', 'SELL TBA' )
                                        AND ( CURRHOLD.GSETTLE_DATE ) >= dbo.uf_getGLDate()
                                     THEN isnull(( CURRHOLD.GORIG_COST_C ) - ( CURRHOLD.T_DUE_ACCR_CY_C ), 0)
                                   ELSE isnull(#tmp_transac.total_amount, 0) -- - isnull(seccash.prin_amount, 0)
                                 END,
             chartacc.PAYABLE_BROKER,
             PAYABLE_BROKERAmt= CASE
                                  WHEN CURRHOLD.STATUS IN ( 'BUY', 'BUY TBA' )
                                       AND ( CURRHOLD.GSETTLE_DATE ) >= dbo.uf_getGLDate()
                                    THEN isnull(-( CURRHOLD.GORIG_COST_C ) - ( CURRHOLD.T_DUE_ACCR_CY_C ), 0)
                                  ELSE 0
                                END,
             CurrStatus=CASE
                          WHEN currhold.status IN ( 'BUY', 'SELL' )
                               AND currhold.closed_date > dbo.uf_getGLDate()
                               AND currhold.T_QUANTITY_C <> 0
                            THEN 1
                          WHEN currhold.status IN ( 'BUY', 'SELL' )
                               AND currhold.gsettle_date > dbo.uf_getGLDate()
                               AND currhold.T_QUANTITY_C <> 0
                            THEN 3
                          ELSE 2
                        END,
             myPrincBalAL= CASE
                             WHEN currhold.T_QUANTITY_C > 0
                               THEN 'A'
                             ELSE 'L'
                           END,
             myAccruedAmtAL= CASE
                               WHEN currhold.t_accr_intrst_c + currhold.t_int_div_due_c > 0
                                 THEN 'A'
                               ELSE 'L'
                             END,
             myUnearnedpremAL= CASE
                                 WHEN ( currhold.T_QUANTITY_C - currhold.GBOOK_VALUE_C ) < 0
                                   THEN 'A'
                                 ELSE 'L'
                               END,
             myUnearnedDiscAL = CASE
                                  WHEN ( currhold.T_QUANTITY_C - currhold.GBOOK_VALUE_C ) < 0
                                    THEN ''
                                  ELSE 'L'
                                END,
             myUnrealProfitAL= CASE
                                 WHEN currhold.Gunrl_gl_ytd_c > 0
                                   THEN 'A'
                                 ELSE 'L'
                               END,
             myUnrealLossAL = CASE
                                WHEN currhold.Gunrl_gl_ytd_c > 0
                                  THEN ''
                                ELSE 'L'
                              END,
             myCouponRate = CASE
                              WHEN ltrim(rtrim(currhold.sec_group)) = 'SHORT TERM'
                                THEN CONVERT(DECIMAL(20, 7), currhold.COUPON_RATE / 100)
                              ELSE CONVERT(DECIMAL(20, 7), security.COUPON_RATE / 100)
                            END,
             myFixRate = CASE
                           WHEN [security].Float_Rate = 'Y'
                             THEN CONVERT(DECIMAL(20, 7), [security].index_rate_add / 100)
                           ELSE
                             CASE
                               WHEN ltrim(rtrim(currhold.sec_group)) = 'SHORT TERM'
                                 THEN CONVERT(DECIMAL(20, 7), currhold.COUPON_RATE / 100)
                               ELSE CONVERT(DECIMAL(20, 7), security.COUPON_RATE / 100)
                             END
                         END,
             myFixVarInd=CASE
                           WHEN Security.float_rate = 'Y'
                             THEN 'V'
                           ELSE 'F'
                         END,
             myIntIncomeAL = CASE
                               WHEN ( currhold.t_accr_intrst_c + currhold.Gchange_amz_c ) > 0
                                 THEN 'I'
                               ELSE ''
                             END,
             CONVERT(VARCHAR(12), currhold.gTrade_Date, 112)                                                       AS RB_VALDATE,
             CONVERT(VARCHAR(12), currhold.maturity_date, 112)                                                     AS Maturity_date,
             CONVERT(VARCHAR(12), currhold.gTrade_Date, 112)                                                       AS RB_TRADEDATE,
             Replace([security].sec_type, ' ', '')                                                                 AS Product,
             [security].guarantor                                                                                  AS CIF_No,
             myTradeType = CASE
                             WHEN currhold.Portfolio = 'ABST'
                               THEN 'TRADING'
                             ELSE 'SALE'
                           END,
             Open_Flag='Y',
             CONVERT(VARCHAR(12), currhold.GSETTLE_DATE, 112)                                                      AS GSETTLE_DATE,
             currhold.t_quantity_c                                                                                 AS Quantity,
             INT_RATE= CASE
                         WHEN ltrim(rtrim(currhold.sec_group)) = 'SHORT TERM'
                           THEN currhold.Coupon_Rate - ( security.Index_Rate_Add / 100 )
                         ELSE security.Coupon_Rate - ( security.Index_Rate_Add / 100 )
                       END,
             security.Index_Rate_Add,
             CONVERT(VARCHAR(12), security.Next_PMT_Date, 112)                                                     AS Next_PMT_Date,
             currhold.status,
             currhold.account,
             SECURITY.FLOAT_FREQ,
             'SOURCE'                                                                                              AS Source_Code,
             CONVERT(VARCHAR(12), @System_Date, 112)                                                               AS System_Date,
             #transactemp.broker,
             security.issuer,
             CONVERT(DECIMAL(20, 8), currhold.gorig_price)                                                         AS Orig_Price,
             isnull(portfoli.MGR2_TITLE, '')                                                                       AS OBU,
             isnull(#tmpISB.Issuer_ISB, '')                                                                        AS Issuer_ISB,
             Security.Author,
             isnull(CONVERT(VARCHAR(12), #pricetemp.Max_Price_Date, 112), CONVERT(VARCHAR(12), @System_Date, 112)) AS Max_Price_Date,
             Security.Symb_pool,
             isnull(CONVERT(VARCHAR(12), Currhold.Issue_Date, 112), '')                                            AS Issue_Date,
             isnull(CONVERT(VARCHAR(12), Security.Last_pmt_date, 112), CONVERT(VARCHAR(12), @System_Date, 112))    AS Last_pmt_date,
             Security.Accrual,
             isnull(CONVERT(VARCHAR(12), Security.Last_float_Date, 112), '')                                       AS Last_float_date,
             isnull(CONVERT(VARCHAR(12), Security.Next_Float_Date, 112), '')                                       AS Next_float_date,
             isnull([security].guarantor, '')                                                                      AS guarantor,
             isnull(CONVERT(VARCHAR(12), Currhold.Maturity_date, 112), '')                                         AS LAST_PRIN_PMT,
             Security.Country,
             isnull(currhold.ANT_HEDGE, '')                                                                        AS ANT_HEDGE,
             isnull(Broker.User_desc1, '')                                                                         AS Broker_ISB,
             isnull(#tmpISB.Guarantor_ISB, '')                                                                     AS Guarantor_ISB
      FROM   securtyp,
             chartacc,
             security,
             portfoli,
             #factortemp,
             #pricetemp,
             currhold,
             #tmp_transac,
             issuer,
             #transactemp,
             broker,
             #tmpISB
      WHERE  #tmpISB.cusip = security.cusip
             AND #transactemp.broker = broker.broker
             AND security.cusip = #transactemp.cusip
             AND #transactemp.current_hold__001 = currhold.current_hold__001
             AND #transactemp.cusip = currhold.cusip
             AND currhold.current_hold__001 *= #tmp_transac.current_hold__001
             AND ( ( abs(t_quantity_c) > 0.01
                     AND #tmp_transac.current_hold__001 IS NULL )
                    OR #tmp_transac.current_hold__001 IS NOT NULL )
             AND portfoli.account = currhold.Account
             AND portfoli.portfolio = currhold.Portfolio
             AND securtyp.sec_type = currhold.sec_type
             AND security.cusip = currhold.cusip
             AND currhold.account = chartacc.account
             AND currhold.portfolio = chartacc.portfolio
             AND currhold.sec_group = chartacc.sec_group
             AND currhold.sec_type = chartacc.sec_type
             AND currhold.custodian = chartacc.custodian
             AND currhold.cusip *=# factortemp.cusip
             AND currhold.cusip *=# pricetemp.cusip
             AND security.issuer = issuer.issuer
             AND CURRHOLD.PORTFOLIO NOT IN (SELECT portfolio
                                            FROM   ut_portfolio_exception
                                            WHERE  proc_name = 'SKY')
             AND abs(CURRHOLD.T_QUANTITY_C) > 0.01
             AND currhold.account NOT IN ( 'SEC PUR', 'SEC SOLD', 'RABO SEC', 'GOSMORE' )
  END 

1 个答案:

答案 0 :(得分:5)

这是一种相当古老的语法(仅在SQL-Server中使用),自2005版以来已被弃用。要进行转换,您应该执行以下操作:

从这里开始:

select * 
from dbo.x9,dbo.sometables2
where dbo.x9.x1 *=   dbo.sometables2.x1 

为:

select *   
from dbo.x9 left join dbo.sometables2
on  dbo.x9.x1 =   dbo.sometables2.x1