无法在SQL Server中使用左外连接和使用

时间:2016-09-16 15:04:55

标签: sql sql-server oracle

我在oracle中有一个SQL查询,我无法在sql server中重写相同的内容。我无法使用左外连接和组合使用。有人可以指出我正确的方向。

这是我在oracle中的查询:

(select QD.FY_ST_QUESTION_DTL_TABLE_ID,qi.display_number,QI.QUESTION_NUMBER,
                                qd.question_part_number
                            from FY_ST_QUESTION_INFO qi,
                                 FY_ST_QUESTION_DETAIL qd
                           where qi.region = cRegion
                             and qi.state_code = cState_Code
                             and qi.fy = nFY
                             and qi.report_id = nREPORT_ID
                             and qi.section_id = nSECTION_ID
                             and qi.subsection_id = nSUBSECTION_ID
                             and qi.display_number = nDISPLAY_NUMBER
                             and qi.QUESTION_NUMBER = nQUESTION_NUMBER
                             and ( QI.REPORTER_ID = sUSER_ID or
                                   QI.DELEGATE_ID = sUSER_ID or
                                   QI.SUB_DELEGATE_ID = sUSER_ID )
                             and QD.FY_ST_QUESTION_INFO_TABLE_ID = QI.FY_ST_QUESTION_INFO_TABLE_ID
                       )fq left outer join
                       ( select distinct FY_ST_QUESTION_DTL_TABLE_ID, AD.ROW_NUMBER
                           from UPLOAD_TEMP_DATA ad
                          where ad.region = cRegion
                            and ad.state_code = cState_Code
                            and ad.fy = nFY
                            and ad.report_id = nREPORT_ID
                            and ad.section_id = nSECTION_ID
                            and ad.subsection_id = nSUBSECTION_ID
                            and ad.display_number = nDISPLAY_NUMBER
                            and ad.QUESTION_NUMBER = nQUESTION_NUMBER
                            and ad.QUESTION_PART_NUMBER = nQUESTION_PART_NUMBER
                       )aq
                     using(FY_ST_QUESTION_DTL_TABLE_ID)
              WHERE ROW_NUMBER IS NOT NULL
             order by question_number,question_part_number,row_number ;

1 个答案:

答案 0 :(得分:0)

你不能在SQL Server中使用USING加入(有些dbms,比如mysql和Oracle,比如实现它,但是......不是Sql Server)

将其替换为

ON QD.FY_ST_QUESTION_DTL_TABLE_ID = aq.FY_ST_QUESTION_DTL_TABLE_ID

顺便说一下,示例代码看起来不是一个有效的查询,)fq似乎很奇怪。 查询的第一部分不需要(也可能不是)别名。

最后,主查询的where子句应放在所有连接之后。

尝试将其重写为

    select 
      QD.FY_ST_QUESTION_DTL_TABLE_ID,qi.display_number,QI.QUESTION_NUMBER,
      qd.question_part_number
    from FY_ST_QUESTION_INFO qi
    join FY_ST_QUESTION_DETAIL qd on QD.FY_ST_QUESTION_INFO_TABLE_ID = QI.FY_ST_QUESTION_INFO_TABLE_ID
    left outer join
        (select 
           distinct FY_ST_QUESTION_DTL_TABLE_ID, 
           AD.ROW_NUMBER
         from UPLOAD_TEMP_DATA ad
         where 
           ad.region = cRegion
           and ad.state_code = cState_Code
           and ad.fy = nFY
           and ad.report_id = nREPORT_ID
           and ad.section_id = nSECTION_ID
           and ad.subsection_id = nSUBSECTION_ID
           and ad.display_number = nDISPLAY_NUMBER
           and ad.QUESTION_NUMBER = nQUESTION_NUMBER
           and ad.QUESTION_PART_NUMBER = nQUESTION_PART_NUMBER
        )aq
             ON QD.FY_ST_QUESTION_DTL_TABLE_ID = aq.FY_ST_QUESTION_DTL_TABLE_ID

       WHERE ROW_NUMBER IS NOT NULL
      and qi.region = cRegion
      and qi.state_code = cState_Code
      and qi.fy = nFY
      and qi.report_id = nREPORT_ID
      and qi.section_id = nSECTION_ID
      and qi.subsection_id = nSUBSECTION_ID
      and qi.display_number = nDISPLAY_NUMBER
      and qi.QUESTION_NUMBER = nQUESTION_NUMBER
      and ( QI.REPORTER_ID = sUSER_ID or
            QI.DELEGATE_ID = sUSER_ID or
             QI.SUB_DELEGATE_ID = sUSER_ID )
       order by question_number,question_part_number,row_number ;

Actual code:

CREATE PROCEDURE [HSIP].[PROJECTLIST_RPT1](

@cRegion CHAR(2) = '00',
@cState_Code CHAR(2) = '00',
@nFY NUMERIC(4,0) = 0,
@nREPORT_ID NUMERIC(2,0) = 0,
@nSECTION_ID NUMERIC(2,0) = 0,
@nSUBSECTION_ID NUMERIC(2,0) = 0,
@nQUESTION_NUMBER NUMERIC(38,0) = 0,
@nDISPLAY_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_PART_NUMBER NUMERIC(38,0) = 0,
@sUSER_ID VARCHAR(25) = NULL,
@nFY_ST_QUESTION_DTL_TABLE_ID NUMERIC(38,0) = 0,
--@nrow_num integer  )
@nROW_NUMBER NUMERIC(38,0) = 0)


--coloffset integer := 17;

AS
BEGIN

SET NOCOUNT ON;

--declare @setval as varchar

declare @coloffset as integer = 17

select @nrow_number,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),1) as ProjectName,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),2) as ImprovementCategory,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),3) as SubCategory,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),4) as ProjectOutputs,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),5) as OutputType,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),6) as HsipProjectCost,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),7) as TotalProjectCost,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),8) as FundingCategory,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),9) as FunctionalClassification,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),10) as FunctionalClassificationOther,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),11) as Asdt,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),12) as Speed,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),13) as Ownership,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),14) as OwnershipOther,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),15) as RelevantShspEmphasisArea,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),16) as RelevantShspEmphasisAreaOther,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0),17) as RelevantShspStrategy,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+1) as ErrProjectName,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+2) as ErrImprovementCategory,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+3) as ErrSubCategory,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+4) as ErrProjectOutputs,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+5) as ErrOutputType,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+6) as ErrHsipProjectCost,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+7) as ErrTotalProjectCost,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+8) as ErrFundingCategory,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+9) as ErrFunctionalClassification,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+10) as ErrFunctionalClassificationOth,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+11) as ErrAsdt,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+12) as ErrSpeed,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+13) as ErrOwnership,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+14) as ErrOwnershipOther,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+15) as ErrRelevantShspEmphasisarea,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+16) as ErrRelevantShspEmphasisareaOth,

hsip.getTempResponseData(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+17) as ErrRelevantShspStrategy,

hsip.getErrorCount(@nFY_ST_QUESTION_DTL_TABLE_ID, ISNULL(@nROW_NUMBER, 0), @coloffset+17) as ValidationErrorCount,

@nFY_ST_QUESTION_DTL_TABLE_ID, @nDISPLAY_NUMBER, @nQUESTION_NUMBER, @nQUESTION_PART_NUMBER

from
(select QD.FY_ST_QUESTION_DTL_TABLE_ID,qi.display_number,QI.QUESTION_NUMBER,
                                qd.question_part_number
                            from FY_ST_QUESTION_INFO qi,
                                 FY_ST_QUESTION_DETAIL qd
                           where qi.region = @cRegion
                             and qi.state_code = @cState_Code
                             and qi.fy = @nFY
                             and qi.report_id = @nREPORT_ID
                             and qi.section_id = @nSECTION_ID
                             and qi.subsection_id = @nSUBSECTION_ID
                             and qi.display_number = @nDISPLAY_NUMBER
                             and qi.QUESTION_NUMBER = @nQUESTION_NUMBER
                             and ( QI.REPORTER_ID = @sUSER_ID or
                                   QI.DELEGATE_ID = @sUSER_ID or
                                   QI.SUB_DELEGATE_ID = @sUSER_ID )
                             and QI.FY_ST_QUESTION_INFO_TABLE_ID = QD.FY_ST_QUESTION_INFO_TABLE_ID
                       )as fq left outer join
                       ( select distinct ad.FY_ST_QUESTION_DTL_TABLE_ID, AD.ROW_NUMBER
                           from UPLOAD_TEMP_DATA ad
                          where ad.region = @cRegion
                            and ad.state_code = @cState_Code
                            and ad.fy = @nFY
                            and ad.report_id = @nREPORT_ID
                            and ad.section_id = @nSECTION_ID
                            and ad.subsection_id = @nSUBSECTION_ID
                            and ad.display_number = @nDISPLAY_NUMBER
                            and ad.QUESTION_NUMBER = @nQUESTION_NUMBER
                            and ad.QUESTION_PART_NUMBER = @nQUESTION_PART_NUMBER
                       ) as aq