473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

UNION / INTERSECT / EXCEPT in SQL Server 2000

Hi,

I'm coming back to Sql Server after 4 years away, using other RDBMS,
and there's a few things I'm struggling to remember how to do (if I
could do them in the first place...)

Main amongst those is EXCEPT syntax.

In DB2, if I have two sets of data and I want to exclude the second set
from the first, I can do:

SELECT col1, col2, col3, ... colN
FROM table1
EXCEPT
SELECT col1, col2, col3, ... colN
FROM table2
;

But SQL Server balks at this. I've had a quick look in the T-SQL help
for EXCEPT, but I didn't find that particularly enlightening. Any
pointers as to how I should be doing this?

Thanks

James

Sep 21 '06 #1
5 39305
James Foreman wrote:
In DB2, if I have two sets of data and I want to exclude the second set
from the first, I can do:

SELECT col1, col2, col3, ... colN
FROM table1
EXCEPT
SELECT col1, col2, col3, ... colN
FROM table2
;
Say table1 and table2 both have a primary key of col1, then do:

select table1.col1, table1.col2, table1.col3, ..., table1.colN
from table1
left join table2 on table2.col1 = table1.col1
where table2.col1 is null
Sep 21 '06 #2
Thanks. From looking around a bit more on this group, INTERSECT &
EXCEPT are implemented in SQL Server 2005, but not in 2000. What
you've said is fine where there's a primary key available, but we've
had situations where that isn't the case and EXCEPT has been an easier
way to deal with this (eg you get given a lot of customer address data
that nobody has bothered to form a key on, and you only want to read in
rows you haven't already got. You *can* do it by comparing columns,
but once you have lots of columns to compare the code gets rather
ugly).

Plus I find EXCEPT easier to read, but that's more personal preference
than a good reason...

James

Sep 21 '06 #3
On 21 Sep 2006 06:33:52 -0700, James Foreman wrote:

(snip)
>Plus I find EXCEPT easier to read, but that's more personal preference
than a good reason...
Hi James,

Try talking yoour boss into upgrading to SQL Server 2005 :-)

Alternatively, consider this alternative. Definitely not as clean as
just writing EXCEPT, but (esp. with long column lists) shorter than the
outer join approach:

SELECT col1, col2, col3, ... colN
FROM (SELECT col1, col2, col3, ... colN, 'table1' AS tab
FROM table1
UNION ALL
SELECT col1, col2, col3, ... colN, 'table2' AS tab
FROM table2) AS d
GROUP BY col1, col2, col3, ... colN
HAVING MIN(tab) = 'table1'
AND MAX(tab) = 'table1';
--
Hugo Kornelis, SQL Server MVP
Sep 21 '06 #4
Thanks Hugo, that's a neat way of doing it. I'm still badgering my
boss about getting 2005 - desperate to get my hands on MERGE INTO as
well...

Oct 3 '06 #5
On 3 Oct 2006 03:04:55 -0700, James Foreman wrote:
>Thanks Hugo, that's a neat way of doing it. I'm still badgering my
boss about getting 2005 - desperate to get my hands on MERGE INTO as
well...
Hi James,

Just FTR, there's no MERGE INTO in SQL Server 2005. Unfortunately. :-(

--
Hugo Kornelis, SQL Server MVP
Oct 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jaYPee | last post by:
I have converted access 97 to access xp project and connect the table from my sql server 2000. my problem is that when i run my report it says "Invalid SQL Statement. Check the server filter on the...
1
by: elpico | last post by:
Hi there, I've only recently started a project using sql server 2000 for the first time. One of the considerations we have to take is that we need to continuously age/purge data from a couple of...
0
by: Eivind Bjoraa | last post by:
Hello I have a server running .NET SDK v1.1 and MS SQL server 2000. I am using VS.NET v1.1 enterprice while some others are running VS.NET v 1.0 professional. The problems lies in adding new...
4
by: harish | last post by:
DELETING 100 million from a table weekly SQl SERVER 2000 Hi All We have a table in SQL SERVER 2000 which has about 250 million records and this will be growing by 100 million every week. At a...
3
by: John South | last post by:
I have an Access 2000 front end that I wish to work with a SQL Server 2000 database by means of Linked tables. Do I have to use an ODBC connection to SQL Server? It seems to be the only option...
1
by: mrclash | last post by:
Hello, I have a Database in a SQL Server 2000 where I have different users tables with equal fields like this: id (int) email (varchar) name (varchar) address (varchar) joinedon (datetime)
7
by: TerpZebra | last post by:
I am having difficulty connecting to SQL Server 2000 on one of our servers via a VB6 program on Vista. I can connect fine to a different server, but it gives me the following error with the server...
2
by: skumar2008 | last post by:
I've seen a number of solution for INTERSECT in MSSQL 2000 but all the examples I've seen go across two tables. What I need is to be able to INTERSECT records with multiple criteria from the same...
7
by: InnoSol | last post by:
Hi all, I import MS Excel 2003 spread sheet in MS SQL Server 2000 through MS SQL Server 2000 Enterprise Manager. In excel two sheets (sheet1 and sheet2) of data is there. I imported first sheet...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.