仅在运行时查询错误

时间:2012-07-27 08:06:45

标签: sql common-table-expression

我有一个奇怪的行为,我执行一个运行查询的方法。它适用于localhost,但在质量保证方面失败。查询和错误如下:

WITH t AS 
(
    SELECT
        *,
        ROW_NUMBER() OVER(PARTITION BY matricola  ORDER BY esercizio DESC, cod_valutazione, valutaz_sintetica, matricola, FigureOrder) AS RowN 
    FROM 
        (SELECT DISTINCT 
            '' AS pdf,
            Vm.NomeManager + ' ' + Vm.CognomeManager AS valutatore,
            vpd.ESERCIZIO AS esercizio,
            vpd.cod_valutazione,
            Vm.NomeValutato +' ' + Vm.CognomeValutato AS valutato,
            vpd.matricola,
            valutaz_sintetica,
            vpd.COD_STATO AS stato_scheda,
            vpd.VALUTAZIONE_RANKING_SINTETICA,
            vvv.COD_FIGURA,
            CASE
                WHEN COD_FIGURA = 'OWNG' THEN 0
                WHEN COD_FIGURA = 'VAL1' THEN 1
                WHEN COD_FIGURA = 'OWN' THEN 2
            END AS FigureOrder      
        FROM
            View_SchedePerDistribuzione vpd
            INNER JOIN RL_VALUTAZIONE_VALUTATI_VALUTATORI vvv ON vpd.COD_VALUTAZIONE = vvv.COD_VALUTAZIONE AND vpd.MATRICOLA = vvv.MATRICOLA_VALUTATO 
            INNER JOIN ValutatiManager  Vm ON vpd.MATRICOLA = Vm.MatricolaVAlutato AND vpd.COD_VALUTAZIONE = Vm.COD_VALUTAZIONE
        WHERE 
            vpd.cod_valutazione='PA_SCI_11-12_test' AND 
            vpd.cod_stato='PROP' AND 
            chiusa = 0 AND 
            valutaz_sintetica <> '' AND 
            VVV.COD_FIGURA IN ('VAL1', 'OWN', 'OWNG') AND 
            VVV.Matricola = '06000013' 
        ) AS tab) 

        SELECT * 
          FROM t
         WHERE RowN = 1
         ORDER BY esercizio DESC, cod_valutazione, valutaz_sintetica, matricola, FigureOrder

这是错误:

        nnString=Provider=SQLOLEDB;Data Sourcexxx;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password='xxx';Connect Timeout=xx System.Data.OleDb.OleDbException (0x80040E14): 
        Incorrect syntax near the keyword 'with'.     
        at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)     
        at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)     
        at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)     
        at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)     
        at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)     
        at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)     
        at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)     
        at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)     
        at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)     
        at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)     
        at valDipLibrary.utility.getQuery(String qry, OleDbTransaction SqlTrn, String ConnString, Boolean fromPool, Boolean logReq, Int32 startRecord, Int32 maxRecord)     
        at valDipLibrary.utility.getQuery(String qry, OleDbTransaction SqlTrn, String ConnString, Boolean fromPool, Boolean logReq, Int32 startRecord, Int32 maxRecord)

更多信息:

如果我在我的SSMS上运行查询,它就可以了。 如果我在质量服务器的SSMS上运行查询,它就可以工作。

仅当我运行方法执行查询时才会起作用。

你能帮助我吗?

2 个答案:

答案 0 :(得分:1)

我认为质量服务器的版本低于2005.因为从2005版开始支持CTE

答案 1 :(得分:1)

在使用

的单词前尝试分号

即:

;WITH t AS  
( 
    SELECT 
    ...