可以在CTE中使用临时表吗?

时间:2014-01-22 16:27:56

标签: sql-server sql-server-2000 common-table-expression

我的查询存在问题。我使用行号为sql server 2005编写此查询,但我意识到sql server是2000,所以我不能使用行号。我删除带有标识和临时表的行号,但查询不起作用。有什么解决方案吗?如果是一个解决方案,使用cte和temp表是个好主意吗?

@Emp nvarchar(50),
@Start_Date nvarchar(50),
@End_Date nvarchar(50)
as
WITH Ordered
     AS (SELECT CONVERT(VARCHAR(15), cast(Substring(unitts, 1, 8) AS DATE), 105) AS Data,
                Substring(UnitTS, 9, 2) + ':' + Substring(UnitTS, 11, 2)         AS EventTime,
                CASE
                  WHEN RdrHead = 'A' THEN 'OUT'
                  ELSE 'IN '
                END                                                              AS Reader,
                [RdrName],
                [CrdName],
                IDENTITY (int, 1, 1)                                             AS rn,
                UnitTS
         INTO   #TEMP --rn = row_number() over (order by Crdname,UnitTs)
         FROM   TandA.dbo.History
         WHERE  ( UnitNr = '3'
                  AND RdrNr IN ( '0', '2', '3' )
                   OR UnitNr = '4'
                      AND RdrNr IN( '1', '6' ) )
                AND Type = 'A'
                AND Sign = '+'
                AND Substring(unitts, 1, 8) >= @Start_Date
                AND Substring(unitts, 1, 8) <= @End_Date
                AND ( CrdName IN ( @mp )
                       OR @emp = 'all' )

                       SELECT * 
                       FROM #TEMP 
                       ORDER BY rn 

                       DROP TABLE #TEMP)
SELECT o_out.CrdName,
       o_out.RdrName,
       o_out.Data,
       CASE
         WHEN o_in.EventTime IS NULL THEN 'Necunoscut'
         ELSE o_in.EventTime
       END In_Time,
       [Out_Time] = o_out.EventTime,
       CASE
         WHEN cast(datediff (s, o_in.EventTime, o_out.EventTime) AS INT) IS NULL THEN '0'
         ELSE cast(datediff (S, o_in.EventTime, o_out.EventTime) AS INT)
       END Duration
FROM   Ordered o_out
       LEFT JOIN Ordered o_in
         ON o_in.rn = o_out.rn - 1
            AND o_in.Reader = 'in'
WHERE  o_out.Reader = 'out' 

1 个答案:

答案 0 :(得分:2)

您的查询语法不正确。您无法在CTE查询中创建和删除#TEMP表。

BTW,在这种情况下不需要CTE,因为您需要的所有信息都在#TEMP表上。您可以按如下方式重写查询:

SELECT CONVERT(VARCHAR(15), cast(Substring(unitts, 1, 8) AS DATE), 105) AS Data,
                Substring(UnitTS, 9, 2) + ':' + Substring(UnitTS, 11, 2)         AS EventTime,
                CASE
                  WHEN RdrHead = 'A' THEN 'OUT'
                  ELSE 'IN '
                END                                                              AS Reader,
                [RdrName],
                [CrdName],
                IDENTITY (int, 1, 1)                                             AS rn,
                UnitTS
         INTO   #TEMP --rn = row_number() over (order by Crdname,UnitTs)
         FROM   TandA.dbo.History
         WHERE  ( UnitNr = '3'
                  AND RdrNr IN ( '0', '2', '3' )
                   OR UnitNr = '4'
                      AND RdrNr IN( '1', '6' ) )
                AND Type = 'A'
                AND Sign = '+'
                AND Substring(unitts, 1, 8) >= @Start_Date
                AND Substring(unitts, 1, 8) <= @End_Date
                AND ( CrdName IN ( @mp )
                       OR @emp = 'all' )

                       ORDER BY rn;

SELECT o_out.CrdName,
       o_out.RdrName,
       o_out.Data,
       CASE
         WHEN o_in.EventTime IS NULL THEN 'Necunoscut'
         ELSE o_in.EventTime
       END In_Time,
       [Out_Time] = o_out.EventTime,
       CASE
         WHEN cast(datediff (s, o_in.EventTime, o_out.EventTime) AS INT) IS NULL THEN '0'
         ELSE cast(datediff (S, o_in.EventTime, o_out.EventTime) AS INT)
       END Duration
FROM   Ordered o_out
       LEFT JOIN #TEMP o_in
         ON o_in.rn = o_out.rn - 1
            AND o_in.Reader = 'in'
WHERE  o_out.Reader = 'out';

DROP TABLE #TEMP;