Question

I have a problem with the query below. I write this query for sql server 2005 using row number, but I realized the sql server is 2000, so i can't use row number. I remove row number with identity and temp table but the query doesn't work. Is there any solution ? And if is a solution it is a good idea to use cte and temp table ?

@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' 
Was it helpful?

Solution

The syntax of your query is incorrect. You cannot create and drop the #TEMP table within the CTE query.

BTW, CTE is not required on this case, given that all the info you need is on the #TEMP table. You can rewrite the query as follows:

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top