SQL Server 2012 ORDER BY的工作方式不同?

时间:2013-09-13 14:56:18

标签: sql sql-server-2012 bcp

我在SQL Server 2005中使用了以下代码多年,最近升级到SQL Server 2012,它似乎打破了ORDER BY子句。代码应该显示如下:

A
A1
B
B1
B2
B3
C

但是将所有相同类型的行分组在一起。有什么想法吗?

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 

1 个答案:

答案 0 :(得分:8)

您的最终查询...

SELECT Extract FROM datadump

...没有ORDER BY。你期待什么?

在那里添加ORDER BY。您在插入时所订购的内容与将来的查询无关。