Thanks everyone.
I used the following code and it works like a charm.
declare
@DbName sysname,
@IdCol sysname,
@CodeColumn sysname
CREATE TABLE #TableWithBothColumns (
TabName varchar(500),
ColumnID varchar(20),
CodeColumn Varchar(500) NOT NULL)
select @Dbname = 'DB', @idcol= 'col1', @codecolumn= 'col2'
exec (
'USE '+ @DbName +' insert into #TableWithBothColumns SELECT distinct
a.name, ''' + @IdCol + ''' AS IDColumn , ''' + @CodeColumn + ''' AS
codeColumn FROM sysobjects a JOIN syscolumns b ON a.id = b.id WHERE
a.type = ''U''
and a.id in (
select distinct c.id from syscolumns c
inner join syscolumns s
on c.id = s.id
where c.name = '''+ @IdCol +''' and s.name = '''+@CodeColumn +''')'
)
declare @table_name sysname
LoopTable:
set @table_name = NULL
select top 1 @table_name = Tabname FROM #TableWithBothColumns
print 'tabename is '+ @table_name
if NOT (@table_name IS NULL)
begin
exec ( ' Update [' + @table_name + '] Set col1=col2')
delete from #TableWithBothColumns where tabname = @table_name
goto LoopTable
end
drop table #TableWithBothColumns
"Greg D. Moore \(Strider\)" <mo****************@greenms.com> wrote in message news:<_s*******************@twister.nyroc.rr.com>. ..
"Muzamil" <mu*****@hotmail.com> wrote in message
news:5a**************************@posting.google.c om... Hi
My database contains , lets say, 100 tables. Out of these 100 tables,
60 tables contain both columns; Column1 and Column2.
I want to:
Update (All tables with BOTH of these columns)
Set Column1 = Column2
Is there a better way of doing it? OR do I have to do it manually for
each table.
There is an UNDOCUMENTED SP in master sp_msforeachtable.
Note: Microsoft does NOT support the use of this. It may change in any
servicepack or update.
If you're careful for what you want though, it may be useful.
Thanks for your help.