Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to determine of if columns exist in one table and not another and alter the table

Posted on 2010-11-24
6
Medium Priority
?
60 Views
Last Modified: 2015-02-09
I have two tables of which one is a backup table.  I need to dynamically add columns to the backup table if they existi in the primary table but not in the backup table.
0
Comment
Question by:ccunni1
[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
6 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 34208259
Hi-

You can use a SQL statement like below against the INFORMATION_SCHEMA to build dynamic ALTER TABLE statements.

The query will return for you the columns and associated information that are in the live table that do not exist with same name in the backup table.

You can then use columns like COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, DATA_TYPE, et cetera to build your ALTER TABLE {backup table name} ADD ... statements.
http://msdn.microsoft.com/en-us/library/aa275462(SQL.80).aspx

Hope that helps!

Kevin
select *
from information_schema.columns c1
where c1.TABLE_NAME = '{live table name}'
and not exists (
   select null
   from information_schema.columns c2
   where c2.TABLE_NAME = '{backup table name}'
   and c2.COLUMN_NAME = c1.COLUMN_NAME
);

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34208263
Say your tables are t_1 and t_2
declare @sql varchar(8000)
set @sql = ''
select @sql = @sql + 'alter table t_2 add ' + quotename(c.name) + ' ' + t.name +
case
when t.name in (
	'varbinary','varchar','binary','char','nchar','float',
	'time','datetime2','datetimeoffset')
	then '(' + right(c.xprec,10) + ')'
when t.name = 'nvarchar'
	then '(' + right(c.length/2,10) + ')'
when t.name in ('decimal','numeric')
	then '(' + right(c.xprec,10) + ',' + right(c.xscale,10) + ')'
else
	''
end
+ ';'
from 
(
select a.*
from
(select c.* from sysobjects o inner join syscolumns c on c.id=o.id where o.name = 't_1') a
left join
(select c.* from sysobjects o inner join syscolumns c on c.id=o.id where o.name = 't_2') b
on a.name=b.name
where b.name is null
) c
inner join systypes t on t.xtype=c.xtype and t.xusertype = c.xusertype
;
--print @sql
exec (@sql)

Open in new window

0
 

Author Comment

by:ccunni1
ID: 34232792
I am getting a Msg 1001, Level 15, State 1, Line 1
Line 1: Length or precision specification 0 is invalid.
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 2000 total points
ID: 34232955
I added a condition for varchar/varbinary max..

Regards
declare @sql varchar(8000)
set @sql = ''
select @sql = @sql + 'alter table t_2 add ' + quotename(c.name) + ' ' + t.name +
case
when t.name in ('varbinary','varchar') AND c.xprec=0 then
	'(max)'
when t.name in (
	'varbinary','varchar','binary','char','nchar','float',
	'time','datetime2','datetimeoffset')
	then '(' + right(c.xprec,10) + ')'
when t.name = 'nvarchar'
	then '(' + right(c.length/2,10) + ')'
when t.name in ('decimal','numeric')
	then '(' + right(c.xprec,10) + ',' + right(c.xscale,10) + ')'
else
	''
end
+ ';'
from 
(
select a.*
from
(select c.* from sysobjects o inner join syscolumns c on c.id=o.id where o.name = 't_1') a
left join
(select c.* from sysobjects o inner join syscolumns c on c.id=o.id where o.name = 't_2') b
on a.name=b.name
where b.name is null
) c
inner join systypes t on t.xtype=c.xtype and t.xusertype = c.xusertype
;
--print @sql
exec (@sql)

Open in new window

0
 
LVL 23

Expert Comment

by:Steve Wales
ID: 40597946
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
Viewers will learn how the fundamental information of how to create a table.

721 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