Solved

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

Posted on 2015-02-19
5
51 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
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 33

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

809 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