| Author |
Topic |
|
Conjurer
Starting Member
30 Posts |
Posted - 2006-01-10 : 16:01:57
|
| I have two databases, database A and database BBoth have an identical table called "Codes" containing billing numbers and prices.Database B is one I use for testing, and is out of date. Is there an easy way to load the values from Table_A.codes into Table_B.codes?I know how to do an update from one table to another within the same database, but am clueless how to do it when the data reside in separate databases.Thanks in advance. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-01-10 : 16:11:37
|
| DELETE FROM DB1.dbo.Table1INSERT INTO DB1.dbo.Table1SELECT * FROM DB2.dbo.Table1Tara Kizeraka tduggan |
 |
|
|
Conjurer
Starting Member
30 Posts |
Posted - 2006-01-10 : 17:28:35
|
| Hmmm, so can I update the values this way instead? Update DB1.dbo.Table1 Set a.Fee = b.Fee From DB1.dbo.Table1 a, DB2.dbo.Table1 b Where a.code=b.codeWould that work? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-01-10 : 17:35:50
|
| For a test table, I prefer to just wipe out and start over. But here you go:UPDATE t1SET Column1 = t2.Column1FROM DB1.dbo.Table1 t1INNER DB2.dbo.Table1 t2ON t1.JoinColumn = t2.JoinColumnTara Kizeraka tduggan |
 |
|
|
Conjurer
Starting Member
30 Posts |
Posted - 2006-01-10 : 18:00:13
|
| Still not getting it....I tried the following and I am getting errror messagesHere is the code I tried:TranUpdate t1Set Fee = t2.FeeFrom Training.dbo.procedures t1Inner CentricityPM.dbo.procedures t2on t1.CPTCode = t2.CPTCode And t2.fee is not Nulland when I execute I get the following errors:Server: Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'Tran'.Server: Msg 170, Level 15, State 1, Line 5Line 5: Incorrect syntax near '.'.Any ideas where I am messing up. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-01-10 : 18:03:15
|
| You'll need INNER JOIN instead of INNER, I had a typo in my last post.Tara Kizeraka tduggan |
 |
|
|
Conjurer
Starting Member
30 Posts |
Posted - 2006-01-10 : 18:07:31
|
Hmmm, and I thought that was so cool that you could just type Inner for an Inner Join. Off to give it a shot; |
 |
|
|
Conjurer
Starting Member
30 Posts |
Posted - 2006-01-10 : 18:10:43
|
Getting closer....I used: TranUpdate t1Set Fee = t2.FeeFrom Training.dbo.procedures t1Inner Join CentricityPM.dbo.procedures t2on t1.CPTCode = t2.CPTCode And t2.fee is not Null---CommitRollback No I get just the error: Server: Msg 156, Level 15, State 1, Line 1Incorrect syntax near the keyword 'Tran'.Any ideas what I have messed up? |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2006-01-10 : 18:12:01
|
| Do you mean BEGIN TRAN? Not to mention COMMIT TRAN AND ROLLBACK TRAN...Tara Kizeraka tduggan |
 |
|
|
Conjurer
Starting Member
30 Posts |
Posted - 2006-01-10 : 18:17:41
|
Yep that was it!Needed Begin in front of the TRAN.Got it to work, and they are all good to go.... seeing the light at the end of tunnel now!Thanks so much for your help. That is something I am going to be able to use a lot so it is going to be saving me a lot of work. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2006-01-10 : 20:17:12
|
Conjurer....PLEASE get a good Transact SQL book if you are going to be doing a lot of coding. Any book by Ken Henderson is good. Books Online is also a great reference, as I mentioned in my other post.MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
|