Order by does not work with intersect (thread started by suggestion from Sean Lange)

  • I have two tables with patient ID - one contains several records for one ID (tblLetterFlag), in the second it is a primary key. This code works OK:

    select ClinicalReviewID from tblLetterFlag

    except

    select ClinicalReviewID from tblClinicalReview

    order by ClinicalReviewID

    This one triggers an error:

    select ClinicalReviewID from tblLetterFlag

    intersect

    select ClinicalReviewID from tblClinicalReview

    order by ClinicalReviewID

    Msg 0, Level 11, State 0, Line 0

    A severe error occurred on the current command. The results, if any, should be discarded.

    Msg 0, Level 20, State 0, Line 0

    A severe error occurred on the current command. The results, if any, should be discarded.

    The second one works smoothely if I get rid of order by. Is it my problem or that of 'intersect'?

    Thanks

  • The intersect syntax looks correct. I mocked up the same syntax on a few tables here and the query does work as written.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • So ... order by does not work with intersect?

  • valeryk2000 (6/6/2014)


    So ... order by does not work with intersect?

    It works fine with intersect. Here is a full example to demonstrate it works just fine.

    create table #tblLetterFlag

    (

    ClinicalReviewID int identity,

    SomeValue varchar(10)

    )

    create table #tblClinicalReview

    (

    ClinicalReviewID int identity,

    SomeValue varchar(10)

    )

    create table #tally(N int)

    insert #tally

    select N

    from (values (1), (2), (3), (4), (5), (6), (7), (8), (9), (10)) dt(N)

    insert #tblLetterFlag

    select 'Value ' + CAST(N as varchar)

    from #Tally

    where N <= 10

    insert #tblClinicalReview

    select 'Value ' + CAST(N as varchar)

    from #Tally

    where N <= 5

    select ClinicalReviewID from #tblLetterFlag

    except

    select ClinicalReviewID from #tblClinicalReview

    order by ClinicalReviewID

    select ClinicalReviewID from #tblLetterFlag

    intersect

    select ClinicalReviewID from #tblClinicalReview

    order by ClinicalReviewID

    drop table #tblLetterFlag

    drop table #tblClinicalReview

    drop table #tally

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • The error you are seeing does not have to do with the order by. What version of sql are you running? There have been hotfixes for this issue depending on the version.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • valeryk2000 (6/6/2014)


    So ... order by does not work with intersect?

    No - just the opposite. Sorry if I was unclear, but just like Sean demonstrated - it works just fine as you had written.

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • 2008

  • valeryk2000 (6/6/2014)


    2008

    Not big on providing details today I see. What does this query return?

    select @@VERSION

    The bottom line is that there are hotfixes out there to deal with this. You were told this in this thread and the original place you posted your question. Hit up your favorite search engine and enter the error message you see and will find the Microsoft hotfixes in the top 10 or so results. Or if you don't want to search for it try this link. http://bit.ly/1i90Ukx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (6/6/2014)


    valeryk2000 (6/6/2014)


    2008

    Not big on providing details today I see. What does this query return?

    select @@VERSION

    The bottom line is that there are hotfixes out there to deal with this. You were told this in this thread and the original place you posted your question. Hit up your favorite search engine and enter the error message you see and will find the Microsoft hotfixes in the top 10 or so results. Or if you don't want to search for it try this link. http://bit.ly/1i90Ukx

    Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c) 1988-2008 Microsoft Corporation Standard Edition on Windows NT 6.0 <X86> (Build 6002: Service Pack 2) (VM)

    Interestingly this problem exists ONLY when tblClinicalReview is on the right side of intersect. All other tables that use this field ClinicalReviewID are OK with order by. Anyway it looks that my concern re order by in intersect is not a "intersect" issue, just a peculiarity of my table

    Thanks

  • valeryk2000 (6/6/2014)


    Sean Lange (6/6/2014)


    valeryk2000 (6/6/2014)


    2008

    Not big on providing details today I see. What does this query return?

    select @@VERSION

    The bottom line is that there are hotfixes out there to deal with this. You were told this in this thread and the original place you posted your question. Hit up your favorite search engine and enter the error message you see and will find the Microsoft hotfixes in the top 10 or so results. Or if you don't want to search for it try this link. http://bit.ly/1i90Ukx

    Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) Jul 9 2008 14:43:34 Copyright (c) 1988-2008 Microsoft Corporation Standard Edition on Windows NT 6.0 <X86> (Build 6002: Service Pack 2) (VM)

    Interestingly this problem exists ONLY when tblClinicalReview is on the right side of intersect. All other tables that use this field ClinicalReviewID are OK with order by. Anyway it looks that my concern re order by in intersect is not a "intersect" issue, just a peculiarity of my table

    Thanks

    If anything, I would seriously look at applying the latest Service Pack for SQL Server 2008 to your system. It may also correct the problem you are currently experiencing.

Viewing 10 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply