Thursday, February 16, 2012

Checking for existing column

Hi,

I have a table which I want to alter to add the data from another
table.
Now both these tables have a few columns which are same. Now,
I want the system to be such that only the unique columns can be
added to the first table.

How can I accomplish this in SQL?Do something like

select table_name, column_name, ordinal_position, data_type
from information_schema.columns WHERE table_name = 'myTable1'
AND column_name NOT IN (
select table_name, column_name, ordinal_position, data_type
from information_schema.columns WHERE table_name = 'myTable2')

This will give you the columns of 'myTable1' not in 'myTable2'

and you do vica versa to see what columns are in 'myTable2' but not in
'myTable1'

From this combined list , you can create your ALTER TABLE statements.

--
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm

"Shwetabh" <shwetabhgoel@.gmail.com> wrote in message
news:1141205791.768559.167490@.p10g2000cwp.googlegr oups.com...
> Hi,
> I have a table which I want to alter to add the data from another
> table.
> Now both these tables have a few columns which are same. Now,
> I want the system to be such that only the unique columns can be
> added to the first table.
> How can I accomplish this in SQL?

No comments:

Post a Comment