SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


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


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

Author
Message
valeryk2000
valeryk2000
SSC Veteran
SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)

Group: General Forum Members
Points: 291 Visits: 412
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
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12371 Visits: 18574
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?
valeryk2000
valeryk2000
SSC Veteran
SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)

Group: General Forum Members
Points: 291 Visits: 412
So ... order by does not work with intersect?
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26265 Visits: 17553
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.

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)
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26265 Visits: 17553
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.

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)
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12371 Visits: 18574
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?
valeryk2000
valeryk2000
SSC Veteran
SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)

Group: General Forum Members
Points: 291 Visits: 412
2008
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)One Orange Chip (26K reputation)

Group: General Forum Members
Points: 26265 Visits: 17553
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.

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)
valeryk2000
valeryk2000
SSC Veteran
SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)SSC Veteran (291 reputation)

Group: General Forum Members
Points: 291 Visits: 412
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
Lynn Pettis
Lynn Pettis
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39690 Visits: 38562
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.

Cool
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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search