Solved

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

Posted on 2015-02-19
5
53 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 65

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 65

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to keep a record with the highest value 3 39
SQL Select Query help 1 37
add stored proc on publlication 4 20
Sql server Error message 3 16
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

756 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