Solved

MSSQL 2K8 - Selecting columns from table a not in table b

Posted on 2015-02-19
5
56 Views
Last Modified: 2015-02-19
Good Morning,

I'm trying to find column names in Table A that don't exist in Table B.  Just the names of the columns.

Thanks!
0
Comment
Question by:ttist25
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40619129
<knee-jerk reaction>
SELECT a.name
FROM (
   SELECT name
   FROM sys.columns
   WHERE OBJECT_NAME(object_id) = 'Table A') a
LEFT JOIN (
   SELECT name
   FROM sys.columns
   WHERE OBJECT_NAME(object_id) = 'Table B') b ON a.name = b.name
WHERE b.name IS NULL

Open in new window

0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40619131
You can use either the SYS tables or INFORMATION_SCHEMA views to retrieve the column names.
If you run a query for both tables, you can use the EXCEPT keyword to exclude columns from your first query that are in your second query.
0
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40619193
   SELECT name
   FROM sys.columns
   WHERE OBJECT_NAME(object_id) = 'Table A'
   EXCEPT
   SELECT name
   FROM sys.columns
   WHERE OBJECT_NAME(object_id) = 'Table B'

Open in new window

0
 
LVL 1

Author Closing Comment

by:ttist25
ID: 40619222
Thanks Jim!
0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 40619226
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question