Author |
Topic |
nitin1353
Constraint Violating Yak Guru
381 Posts |
Posted - 2006-10-05 : 09:11:59
|
GurusI have a scenario where i have two databases (db1 and db2) and i have two tables with same structure and with same name with DB2 having one more column with name sync..Now when any update is made in table1 of DB2 the sync column changes to 0.Now i want to update same table of the db1 with same changes where sync =0.How will i do this.?Please help.What will be the update command which i will fire.RegardsNitin |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-05 : 09:19:07
|
[code]update t1set col1 = t2.col1, col2 = t2.col2, . . . from db1.dbo.table t1 inner join db2.dbo.table t2 on t1.pk = t2.pkwhere t2.sync = 0[/code] KH |
|
|
nitin1353
Constraint Violating Yak Guru
381 Posts |
Posted - 2006-10-05 : 09:25:56
|
Thanks KHinstead of hardcoding it cant i use some system table to check my columns so that i dnt have add column manually everytime i add a column or smething?RegardsNitin |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-05 : 09:34:13
|
in that case you will have to use Dynamic SQL. You can obtain the column names from syscolumns or INFORMATION_SCHEMA.COLUMNS KH |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
nitin1353
Constraint Violating Yak Guru
381 Posts |
Posted - 2006-10-05 : 09:36:46
|
Hi yes i understand this,but how will i pick up the column of a particular table?RegardsNitin |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
nitin1353
Constraint Violating Yak Guru
381 Posts |
Posted - 2006-10-05 : 09:39:24
|
Thanks last question,how will i use this with my above situation?Plz helpRegardsNitin |
|
|
nitin1353
Constraint Violating Yak Guru
381 Posts |
Posted - 2006-10-05 : 09:40:35
|
Yes i saw your reply.How will i use this with my above said update script.ThanksNitin |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-05 : 09:55:00
|
something like thisuse Northwinddeclare @sql varchar(8000)declare @table varchar(50)declare @keycol varchar(50)select @table = 'Customers', @keycol = 'CustomerID'select @sql = 'UPDATE t1' + char(13) + 'SET' + char(13) select @sql = @sql + char(9) + quotename(COLUMN_NAME) + ' = t2.' + quotename(COLUMN_NAME) + ',' + char(13)from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = @tableand COLUMN_NAME not in ('sync', 'CustomerID')order by ORDINAL_POSITION-- remove the last comma + CRselect @sql = left(@sql, len(@sql) - 2) + char(13)select @table = quotename(@table), @keycol = quotename(@keycol)select @sql = @sql + 'FROM db1.dbo.' + @table + ' t1 INNER JOIN db2.dbo.' + @table + ' t2' + char(13)select @sql = @sql + 'ON t1.' + @keycol + ' = t2.' + @keycol + char(13)select @sql = @sql + 'WHERE t2.sync = 0'-- print out to verify scriptprint @sql-- remove to execute-- exec(@sql)/* OUTPUTUPDATE t1SET [CompanyName] = t2.[CompanyName], [ContactName] = t2.[ContactName], [ContactTitle] = t2.[ContactTitle], [Address] = t2.[Address], [City] = t2.[City], [Region] = t2.[Region], [PostalCode] = t2.[PostalCode], [Country] = t2.[Country], [Phone] = t2.[Phone], [Fax] = t2.[Fax]FROM db1.dbo.[Customers] t1 INNER JOIN db2.dbo.[Customers] t2ON t1.[CustomerID] = t2.[CustomerID]WHERE t2.sync = 0*/ KH |
|
|
nitin1353
Constraint Violating Yak Guru
381 Posts |
Posted - 2006-10-05 : 10:09:19
|
Thanks a ton.Will this script work when we have two databases like db1 and db2.?RegardsNitin |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-10-05 : 10:26:39
|
Yes. as long as the user has access rights to both db.quote: FROM db1.dbo.[Customers] t1 INNER JOIN db2.dbo.[Customers] t2
KH |
|
|
nitin1353
Constraint Violating Yak Guru
381 Posts |
Posted - 2006-10-06 : 00:06:35
|
Thanks again .One more thing can i implement this in Sql task in DTS.?regardsNitin |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-10-06 : 00:12:27
|
Please do not post questions in the Script Library.The Script Library s for posting working scripts that you are sharing.CODO ERGO SUM |
|
|
|