Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Order by does not work with intersect (thread started by suggestion from Sean Lange) Expand / Collapse
Author
Message
Posted Friday, June 6, 2014 9:49 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, July 14, 2014 1:33 PM
Points: 148, Visits: 292
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
Post #1578384
Posted Friday, June 6, 2014 9:58 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 7,121, Visits: 15,020
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?
Post #1578388
Posted Friday, June 6, 2014 10:05 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, July 14, 2014 1:33 PM
Points: 148, Visits: 292
So ... order by does not work with intersect?
Post #1578389
Posted Friday, June 6, 2014 10:27 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:01 AM
Points: 13,121, Visits: 11,954
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1578400
Posted Friday, June 6, 2014 10:30 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:01 AM
Points: 13,121, Visits: 11,954
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1578401
Posted Friday, June 6, 2014 10:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 9:20 AM
Points: 7,121, Visits: 15,020
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?
Post #1578403
Posted Friday, June 6, 2014 10:37 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, July 14, 2014 1:33 PM
Points: 148, Visits: 292
2008
Post #1578408
Posted Friday, June 6, 2014 10:43 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 10:01 AM
Points: 13,121, Visits: 11,954
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1578410
Posted Friday, June 6, 2014 11:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, July 14, 2014 1:33 PM
Points: 148, Visits: 292
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
Post #1578424
Posted Friday, June 6, 2014 11:42 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 10:21 AM
Points: 23,055, Visits: 31,579
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.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1578431
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse