473,472 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Update all tables in database

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.

Thanks for your help.
Jul 20 '05 #1
5 7905

"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.

Thanks for your help.


If this is a one-time task, then you can use a query like this, then copy
and paste the result set (assuming all your tables are owned by dbo):

select 'update dbo.' + t.TABLE_NAME + ' set col1 = col2'
from INFORMATION_SCHEMA.TABLES t
join INFORMATION_SCHEMA.COLUMNS c1
on T.TABLE_NAME = C1.TABLE_NAME
join INFORMATION_SCHEMA.COLUMNS c2
on t.TABLE_NAME = c2.TABLE_NAME
where t.TABLE_TYPE = 'BASE TABLE'
and c1.COLUMN_NAME = 'col1'
and c2.COLUMN_NAME = 'col2'

If you need to do this more often, then you can declare a cursor for the
same query, and loop through using EXEC() to execute the commands
dynamically.

Simon
Jul 20 '05 #2
On 26 May 2004 08:10:22 -0700, Muzamil wrote:
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.

Thanks for your help.


If you can compile a table with the table names

CREATE TABLE TNames (TName varchar(32) primary key);

, then

SELECT 'UPDATE ' + TNames.TName + ' SET Column1=Column2'

will return a list of the update statements. Copy them to the clipbaord and
paste into a Query Analyzer window, and you're done.

You could get more elaborate and use a cursor to actually execute
everything, but why bother? It sounds like a one-off operation.

If you're using SQL Server 2000, you can compile that table of names with

INSERT INTO TNames (Tname)
SELECT TABLE_NAME
FROM information_schema.tables
where TABLE_TYPE='BASE TABLE'
and TABLE_NAME in (
SELECT TABLE_NAME
FROM information_schema.columns
where COLUMN_NAME='Column2')
and TABLE_NAME in (
SELECT TABLE_NAME
from information_schema.columns
where COLUMN_NAME='Column2')
Jul 20 '05 #3

"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.

Jul 20 '05 #4
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.

Jul 20 '05 #5
>> I want to:

UPDATE {{All tables with BOTH of these columns}}
SET column1 = column2; <<

The short answer is to use dynamic SQL. Of course this is a sure sign
of poor coding and design, it is proprietary and dangerous.

The real question is: "what are you trying to do?" The goal of
normalization is to remove redundancy and you seem to be adding it
into 100 tables.
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Niyazi | last post by:
Hi, I have not understand the problem. Before all the coding with few application everything worked perfectly. Now I am developing Cheque Writing application and when the cheque is clear the...
1
by: Pesko S | last post by:
Hi, Could anybody just point me in a direction where I can find information on how the heck I can update a database with relational data from an XML file. I use stored procedures to insert...
7
by: PC Datasheet | last post by:
Looking for suggestions ---- A database was designed for a national automobile inspection program. In it's simplest form, the database has two tables: TblOwner OwnerID <Year/Make/Model owned...
0
by: M. David Johnson | last post by:
I cannot get my OleDbDataAdapter to update my database table from my local dataset table. The Knowledge Base doesn't seem to help - see item 10 below. I have a Microsoft Access 2000 database...
9
by: jaYPee | last post by:
I have search a lot of thread in google newsgroup and read a lot of articles but still i don't know how to update the dataset that has 3 tables. my 3 tables looks like the 3 tables from...
15
by: graham | last post by:
Hi all, <bitching and moaning section> I am asking for any help I can get here... I am at the end of my tether... I don;t consider myself a genius in any way whatsoever, but I do believe I have...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
2
by: Scotty | last post by:
I get stuck to write an update, insert and delete command, i am looking for some help to start Whats the best way to update 2 tables toe the database (Access) below my code used to load my...
5
by: explode | last post by:
I made a procedure Public Sub Novo(ByVal nova1 As String, ByVal nova2 As String) that creates a new oledbDataAdapter with insert update select and delete commads. I also added that commands can...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.