Question

I have used the following code in SQL Server 2005 for years and recently upgraded to SQL Server 2012 and it has seemingly broken the ORDER BY clause. The code is supposed to display like so:

A
A1
B
B1
B2
B3
C

But is grouping all the lines of the same type together. Any ideas why?

DECLARE @PeriodStart DATETIME 
DECLARE @PeriodEnd DATETIME 

SELECT @PeriodEnd = Getdate(), 
       @PeriodStart = Dateadd(hour, -96, Getdate()); 

WITH outpq 
     AS (SELECT 1                                          AS grpOrd, 
                Cast(NULL AS VARCHAR(255))                 AS posInGrp, 
                'A'                                        AS Ord, 
                casenumberkey, 
                'A|' + clientskey + '|' 
                + Cast(brtnumber AS VARCHAR(11)) + '|' 
                + Isnull(Replace(CONVERT(CHAR(10), coverdate, 101), '/', ''), '' 
                ) 
                + '|' 
                + Isnull(Replace(CONVERT(CHAR(10), coverdate, 101), '/', ''), '' 
                ) 
                + '|' + Isnull(parcelnumber, '') + '|' 
                + Isnull(assessedbeg, '') + '|' 
                + Isnull(assesseddim, '') + '|' 
                + Isnull(abbrlegal, '') + '|' 
                + Isnull(waterfrom, '') + '|' 
                + Isnull(waterto, '') + '|' 
                + Isnull(Cast(wateropen AS VARCHAR(50)), '') 
                + '|' + Isnull(taxfrom, '') + '|' + Isnull(taxto, '') 
                + '|' 
                + Isnull(Cast(taxopen AS VARCHAR(50)), '') AS Extract 
         FROM   newcitycollection.dbo.propertyinformation 
         WHERE  datefinished BETWEEN @PeriodStart AND @PeriodEnd 
                AND clientkey = 2 
         UNION ALL 
         SELECT 1                                AS grpOrd, 
                NULL                             AS posInGrp, 
                'A1', 
                A.casenumberkey, 
                'A1|' + '|' + '|' + B.liennumber + '|' 
                + Isnull(Cast(B.lienamt AS VARCHAR(50)), '') 
                + '|' + Isnull(Replace(liendate, '/', ''), '') 
                + '|' + Isnull(B.lienreason, '') AS Extract 
         FROM   newcitycollection.dbo.propertyinformation A 
                JOIN newcitycollection.dbo.muniliens B 
                  ON B.casenumberkey = A.casenumberkey 
         WHERE  A.datefinished BETWEEN @PeriodStart AND @PeriodEnd 
                AND clientkey = 2 
         UNION ALL 
         SELECT 2                                     AS grpOrd, 
                Cast(C.interestskey AS VARCHAR(11))   AS posInGrp, 
                'B', 
                A.casenumberkey, 
                'B|' + '|' + Isnull(C.first, '') + '|' 
                + Isnull(C.middle, '') + '|' + Isnull(C.last, '') 
                + '|' + Isnull(C.alias, '') + '|' 
                + Isnull(C.comname, '') + '|' 
                + Isnull(C.docrel, '') + '|' 
                + Cast(C.interestskey AS VARCHAR(11)) AS Extract 
         FROM   newcitycollection.dbo.propertyinformation A 
                JOIN newcitycollection.dbo.interests C 
                  ON C.casenumberkey = A.casenumberkey 
         WHERE  A.datefinished BETWEEN @PeriodStart AND @PeriodEnd 
                AND clientkey = 2 
         UNION ALL 
         SELECT 2                                   AS grpOrd, 
                Cast(C.interestskey AS VARCHAR(11)) AS posInGrp, 
                'B1', 
                A.casenumberkey, 
                'B1|' + Isnull(fulladd, '') + '|' 
                + Isnull(D.city, '') + '|' + Isnull(D.state, '') 
                + '|' + Isnull(D.zip, '')           AS Extract 
         FROM   newcitycollection.dbo.propertyinformation A 
                JOIN newcitycollection.dbo.interests C 
                  ON C.casenumberkey = A.casenumberkey 
                JOIN newcitycollection.dbo.interestadd D 
                  ON D.casenumberkey = A.casenumberkey 
                     AND D.interestskey = C.interestskey 
         WHERE  A.datefinished BETWEEN @PeriodStart AND @PeriodEnd 
                AND clientkey = 2 
         UNION ALL 
         SELECT 2                                                AS grpOrd, 
                Cast(C.interestskey AS VARCHAR(11))              AS posInGrp, 
                'B2', 
                A.casenumberkey, 
                'B2|' + '|' + Isnull(E.suitnumber, '') + '|' 
                + Cast(E.bdate AS VARCHAR(11)) + '|' 
                + Isnull(E.chapter, '') + '|' + Isnull(E.vs, '') AS Extract 
         FROM   newcitycollection.dbo.propertyinformation A 
                JOIN newcitycollection.dbo.interests C 
                  ON C.casenumberkey = A.casenumberkey 
                JOIN newcitycollection.dbo.banks E 
                  ON E.casenumberkey = A.casenumberkey 
                     AND E.interestskey = C.interestskey 
         WHERE  A.datefinished BETWEEN @PeriodStart AND @PeriodEnd 
                AND clientkey = 2 
         UNION ALL 
         SELECT 3                                 AS grpOrd3, 
                NULL                              AS posInGrp, 
                'B3', 
                A.casenumberkey, 
                'B3|' + '|' + F.doctype + '|' 
                + Isnull(Cast(F.docamt AS VARCHAR(50)), '') 
                + '|' 
                + Isnull(Replace(CONVERT(CHAR(10), docdate, 101), '/', ''), '') 
                + '|' 
                + Isnull(Replace(CONVERT(CHAR(10), recdate, 101), '/', ''), '') 
                + '|' + Isnull(F.docid, '') + '|' 
                + Isnull(F.grantee, '') + '|' 
                + Isnull(F.grantor, '') 
                + Cast(F.docidkey AS VARCHAR(11)) AS Extract 
         FROM   newcitycollection.dbo.propertyinformation A 
                JOIN newcitycollection.dbo.documents F 
                  ON F.casenumberkey = A.casenumberkey 
         WHERE  A.datefinished BETWEEN @PeriodStart AND @PeriodEnd 
                AND clientkey = 2 
         UNION ALL 
         SELECT 4 
                AS grpOrd 
                , 
                NULL 
                AS posInGrp, 
                'C', 
                A.casenumberkey, 
                'C|' + Isnull(J.ctype, '') + '|' 
                + Isnull(J.plaintiffname, '') + '|' 
                + Isnull(J.plaintiffadd1, '') + '|' 
                + Isnull(J.plaintiffcity, '') + '|' 
                + Isnull(J.plaintiffstate, '') + '|' 
                + Isnull(J.plaintiffzip, '') + '|' + '|' 
                + Isnull(J.defendantname, '') + '|' 
                + Isnull(J.defendantadd1, '') + '|' 
                + Isnull(J.defcity, '') + '|' 
                + Isnull(J.defstate, '') + '|' 
                + Isnull(J.defzip, '') + '|' + '|' 
                + Isnull(J.court, '') + '|' + Isnull(J.caseid, '') 
                + '|' + Isnull(J.jamt, '') + '|' 
                + Isnull(Replace(CONVERT(VARCHAR(10), jdate, 101), '/', ''), '') 
                + '|' 
                + Isnull(Replace(CONVERT(VARCHAR(10), reviveddate, 101), '/', '' 
                ), '' 
                ) 
                AS 
                Extract 
         FROM   newcitycollection.dbo.propertyinformation A 
                JOIN acme.new_judgment_system.dbo.selected_compiled_clean J 
                  ON J.casenumber = A.casenumberkey 
         WHERE  A.datefinished BETWEEN @PeriodStart AND @PeriodEnd 
                AND clientkey = 2 
                AND J.plaintiffname NOT IN (SELECT plaintiff 
                                            FROM 
                    newcitycollection.dbo.excluded_plaintiffs)) 
--Extract data set into a table -- dump table in .txt with current date as part of name then delete that table 
SELECT extract 
INTO   datadump 
FROM   outpq 
ORDER  BY casenumberkey, 
          grpord, 
          posingrp, 
          ord 

DECLARE @FileName   VARCHAR(50), 
        @bcpCommand VARCHAR(2000) 

SET @FileName = Replace('D:\LDExport\Argosy_import_' 
                        + CONVERT(CHAR(8), Getdate(), 1) + '_0001.txt', '/', '') 
SET @bcpCommand = 'bcp "SELECT Extract FROM datadump" QUERYOUT "' 
SET @bcpCommand = @bcpCommand + @FileName 
                  + '" -U sa -P $%^&*() -T -c' 

EXEC master..Xp_cmdshell 
  @bcpCommand 

DROP TABLE datadump 
Was it helpful?

Solution

Your final query...

SELECT Extract FROM datadump

...doesn't have an ORDER BY. What do you expect?

Add an ORDER BY there. What you ordered by when you inserted has no bearing on future queries.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top