Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sponguru_dba
Yak Posting Veteran
93 Posts |
Posted - 2006-09-14 : 06:17:22
|
| Hi allIn my present Data some changes are happend,so that i have to Update EMPID across the Databasealmost 30% of table having EMPID as ReferalHow to Update Data in a Multiple tablesThanks In AdvanceSree |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-09-14 : 06:21:52
|
| You update one table at a time.Peter LarssonHelsingborg, Sweden |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-09-14 : 06:22:25
|
"How to Update Data in a Multiple tables"a single update command can only update one table. You will required one update query for each table."In my present Data some changes are happend,so that i have to Update EMPID across the Databasealmost 30% of table having EMPID as Referal"What are the update you need to perform ? KH |
 |
|
|
chiragkhabaria
Master Smack Fu Yak Hacker
1907 Posts |
Posted - 2006-09-14 : 06:31:08
|
If is this procedure helps you.. i Generally use this procedure to update some field across the database. For this you need to create the Temp Table, where EmpID and their new empid should be there.. Somthing like this Create Table Mig( EmpID int, --your old empid NewEmpID int --New EmpID )-- First parmeter is Column and second is the temp table name which you used for migration.. Sp_UpdateFields "EmpID", "Mig"This procedure will create the update statements and will display in front of you in in QA grid and then you can review it and update. if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Sp_UpdateFields]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[Sp_UpdateFields]GO--Procedure to update a praticular field based on joins through all the Databases Based on EmpID CREATE Proc Sp_UpdateFields (@pFieldName VarChar(1000),-- Column name which is require to be updated @pTableName Varchar(1000) = '', -- This is optional since the values can be hardcodded @pFieldValue Varchar(1000) = '' -- Value for column which is required to be updated )As Begin Declare @QryString VarChar(8000) Declare @TableName VarChar(800)Declare @ColumnName Varchar(100)Declare @TblQry Table (QryString Varchar(8000))Insert @TblQry Select 'Begin Tran 'select @TableName = '',@ColumnName = ''While @ColumnName < (Select Max(InRows) From Dbo.CrackInRows(',',@pFieldName))Begin Select @ColumnName = Min(InRows) From Dbo.CrackInRows(',',@pFieldName) Where InRows > @ColumnName Insert @TblQry Select '--Updating the Column ' + @ColumnName select @TableName = '' while @TableName < (select max(T.Table_Name) from Information_Schema.Columns As T Inner Join (Select Table_Name From Information_Schema.Columns Where Column_Name = 'EmpID') As F On F.Table_Name = T.Table_Name Inner Join (Select Table_Name From Information_Schema.Tables Where Table_Type = 'BASE TABLE') As K on T.Table_Name = K.Table_Name Where T.Column_Name = @ColumnName) begin select @TableName = Min(T.Table_Name) from Information_Schema.Columns As T Inner Join (Select Table_Name From Information_Schema.Columns Where Column_Name = 'EmpID') As F On F.Table_Name = T.Table_Name Inner Join (Select Table_Name From Information_Schema.Tables Where Table_Type = 'BASE TABLE') As K on T.Table_Name = K.Table_Name Where T.Column_Name = @ColumnName And T.[Table_Name] > @TableName if (@pTableName <> '') select @QryString = 'Update T Set ' + @ColumnName + '= ' + @pTableName + '.' + @ColumnName + ' From ' + @TableName + ' T Inner Join ' + @pTableName + ' On T.EmpID = ' + @pTableName + '.EmpID' Else select @QryString = 'Update' + @TableName + ' Set ' + @ColumnName + '= ' + @pFieldValue Insert @TblQry Select @QryString End Insert @TblQry Select '--Updating Completed For Column ' + @ColumnName End insert @TblQry Select 'Rollback Tran 'Select * From @TblQry End GOHope this helpss.Chirag |
 |
|
|
sponguru_dba
Yak Posting Veteran
93 Posts |
Posted - 2006-09-14 : 06:40:22
|
| Thanks for all,Thanks chiragkhabariaIam Working on chiragkhabaria Procedure,I let you know result,hopefull it works i thinkonce agian Thanks chirag |
 |
|
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2006-09-14 : 07:27:21
|
| EMPID looks like it stands for "Employer ID".You are saying your primary key data has changed? "Toys-R-Us" used to be EMPID 345, but now it is EMPID 934.Is this the basic idea?[Signature]For fast help, follow this link:http://weblogs.sqlteam.com/brettk/archive/2005/05/25.aspxLearn SQLhttp://www.sql-tutorial.net/ http://www.firstsql.com/tutor.htm http://www.w3schools.com/sql/default.asp |
 |
|
|
|
|
|
|
|