Solved

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

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now