Microsoft SQL Server错误102

时间:2017-03-01 12:24:59

标签: sql sql-server tsql

我正在执行以下查询,它给了我

SELECT
  t1.s_reference "Section",
  t2.m_reference "CourseRef",
  t2.m_name "CourseName",
  t3.mi_q02m02 "Aim",
  t2.m_level "Level",
  t2.m_start "Start",
  t2.m_end "End",
  t2.m_projstudents "Target",
  COUNT(DISTINCT t4.e_id) "Enrolled"
FROM
  isr t3,
  person t5,
  department t6,
  section t1,
  enrolment t4,
  course t2
WHERE
  t3.mi_id(+)=t2.m_id
  AND t4.e_module=t2.m_id
  AND t4.e_student=t5.p_id
  AND(t2.m_status LIKE '%%'||SUBSTR((TO_CHAR(sysdate,'YYYY')+DECODE(least(to_number(TO_CHAR(sysdate,'mm')),8),8,0,-1)),-2,2) ||'%%')
  AND t2.m_reference LIKE 'LL%%'
  AND t4.e_status IN('C','S')
  AND to_date(t2.m_start,'DD-Mon-RRRR')<=to_date(sysdate,'DD-Mon-RRRR')
  AND to_date(t2.m_end,'DD-Mon-RRRR')>=to_date(sysdate,'DD-Mon-RRRR')
  AND t2.m_modulesection=t1.s_id
  AND t2.m_moduledept=t6.d_id
  AND NVL(t2.m_close,0)=0
  AND t3.mi_q02m02 IS NOT NULL
  AND((
  CASE
    WHEN((t1.s_reference='LL2a')
      AND(t2.m_reference LIKE '%W%'))
    THEN 1
    ELSE 0
  END)=0)
  AND(t2.m_reference!='LL3036E15')
GROUP BY
  t1.s_reference,
  t2.m_reference,
  t2.m_name,
  t3.mi_q02m02,
  t2.m_level,
  t2.m_start,
  t2.m_end,
  t2.m_projstudents,
  t1.s_reference,
  t2.m_reference
ORDER BY
  t1.s_reference,
  t2.m_reference

执行查询时发生错误。

Incorrect syntax near ')'. MS SQL Server Error 102

我在SQL Developer中运行了相同的查询,运行正常。

我试图删除大部分括号,但仍然没有帮助。 知道为什么会这样吗?

由于 阿鲁娜

2 个答案:

答案 0 :(得分:3)

您正在尝试在Microsoft的SQL Server中运行为Oracle编写的查询。这些产品使用稍微不同的SQL方言,这些方言不兼容。例如,DECODENVLTO_CHAR在SQL Server中不可用。您必须使用SQL Server函数重新编写查询,以替换我提到的那些特定于Oracle的CASECOALESCECONVERT

答案 1 :(得分:1)

第一步......替换所有||到单词OR

第二步

  

AND(t2.m_status LIKE   '%%' || SUBSTR((TO_CHAR(SYSDATE, 'YYYY')+ DECODE(至少(TO_NUMBER(TO_CHAR(SYSDATE, '毫米')),8),8,0,-1)), - 2,2-二)   || '%%')

其中

  

|| '%%')

不是合格的条件,因为您已经使用过它可能无关紧要

第3次

  

t3.mi_id(+)= t2.m_id

你必须重写这个

第四

  

%%

应该只是%