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 |
jrobin747
Starting Member
48 Posts |
Posted - 2013-06-07 : 11:52:46
|
I am clueless to writing queries. I am learning what doesn't work. lolI believe my question is simple. However I cannot make the query work.I have two databases. dbo.DatalinkMerchantAccount_TEMPdbo.DatalinkMerchantAccount_TEMP_oldThey both have the same fieldsMerchantID, DBA, BankAccountNum, BankTransitNumI want to update/replace the current values in dbo.DatalinkMerchantAccount_TEMP_old with dbo.DatalinkMerchantAccount_TEMPAll the rows in MerchantID were filled with one value when I tried this:UPDATE dbo.DatalinkMerchantAccount_TEMP_oldSET dbo.DatalinkMerchantAccount_TEMP_old.MerchantID = dbo.DatalinkMerchantAccount_TEMP.MerchantIDFROM dbo.DatalinkMerchantAccount_TEMPGot and error when I tried the followingSelect *into dbo.DatalinkMerchant_TEMP_oldFrom dbo.DatalinkMerchantAccount_TEMPMsg 2714, Level 16, State 6, Line 1There is already an object named 'DatalinkMerchant' in the database.Update dbo.DatalinkMerchant_TEMP_oldSelect *From dbo.DatalinkMerchantAccount_TEMPMsg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'Select'.Update dbo.DatalinkMerchant_TEMP_oldSELECT MerchantID, DBA, BankAccountNum, BankTransitNumFrom dbo.DatalinkMerchantAccount_TEMPMsg 156, Level 15, State 1, Line 2Incorrect syntax near the keyword 'Select'.I'm trying different things and I'm not success. Thanks |
|
pascal_jimi
Posting Yak Master
167 Posts |
Posted - 2013-06-07 : 12:04:18
|
UPDATE dbo.DatalinkMerchantAccount_TEMP_oldSET dbo.DatalinkMerchantAccount_TEMP_old.MerchantID =(select MerchantID from dbo.DatalinkMerchantAccount_TEMP ) ;kmkmmm |
|
|
jrobin747
Starting Member
48 Posts |
Posted - 2013-06-07 : 12:09:50
|
quote: Originally posted by pascal_jimi UPDATE dbo.DatalinkMerchantAccount_TEMP_oldSET dbo.DatalinkMerchantAccount_TEMP_old.MerchantID =(select MerchantID from dbo.DatalinkMerchantAccount_TEMP ) ;kmkmmm
Thanks, however I got an errorMsg 208, Level 16, State 1, Line 1Invalid object name 'dbo.DatalinkMerchantAccount_TEMP_old'. |
|
|
pascal_jimi
Posting Yak Master
167 Posts |
Posted - 2013-06-07 : 12:37:55
|
create table t1(name nvarchar(20), age int);create table t2(name nvarchar(20), age int);insert into t1(name, age) values('ivanov',35);insert into t1(name, age) values('petrov',45);insert into t1(name, age) values('sidorov',55);insert into t2(name) values('ivanov');insert into t2(name) values('petrov');insert into t2(name) values('sidorov');goselect*from t1select*from t2goUPDATE t2SET t2.age = ( SELECT t1.age FROM t1 WHERE t1.name = t2.name)goselect*from t2kmkmmm |
|
|
jrobin747
Starting Member
48 Posts |
Posted - 2013-06-07 : 12:49:46
|
quote: Originally posted by pascal_jimi create table t1(name nvarchar(20), age int);create table t2(name nvarchar(20), age int);insert into t1(name, age) values('ivanov',35);insert into t1(name, age) values('petrov',45);insert into t1(name, age) values('sidorov',55);insert into t2(name) values('ivanov');insert into t2(name) values('petrov');insert into t2(name) values('sidorov');goselect*from t1select*from t2goUPDATE t2SET t2.age = ( SELECT t1.age FROM t1 WHERE t1.name = t2.name)goselect*from t2kmkmmm
I think you replied to the wrong post |
|
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-06-07 : 13:43:08
|
I believe PascalJimi is trying to provide an example for you.You can update your tables in couple of ways:1. If you want to keep part of the data in the dbo.DatalinkMerchantAccount_TEMP_old table then you can do something like this:[CODE]UPDATE T2 SET DBA = T1.DBA, BankAccountNum = T1.BankAccountNum, BankTransitNum = T1.BankTransitNum FROM dbo.DatalinkMerchantAccount_TEMP_old AS T2 JOIN dbo.DatalinkMerchantAccount_TEMP AS T1 ON T1.MerchantID = T2.MerchantID[/CODE]2. If you don't care to keep any of the data in the table dbo.DatalinkMerchantAccount_TEMP_old table then you can do something like this:[CODE]TRUNCATE TABLE dbo.DatalinkMerchantAccount_TEMP_old; -- WARNING THIS COMMAND DELETES ALL THE DATA FROM THIS TABLE!!!INSERT INTO dbo.DatalinkMerchantAccount_TEMP_old(MerchantID, DBA, BankAccountNum, BankTransitNum) SELECT MerchantID, DBA, BankAccountNum, BankTransitNum FROM dbo.DatalinkMerchantAccount_TEMP;[/CODE] |
|
|
jrobin747
Starting Member
48 Posts |
Posted - 2013-06-07 : 16:23:48
|
quote: Originally posted by MuMu88 I believe PascalJimi is trying to provide an example for you.You can update your tables in couple of ways:1. If you want to keep part of the data in the dbo.DatalinkMerchantAccount_TEMP_old table then you can do something like this:[CODE]UPDATE T2 SET DBA = T1.DBA, BankAccountNum = T1.BankAccountNum, BankTransitNum = T1.BankTransitNum FROM dbo.DatalinkMerchantAccount_TEMP_old AS T2 JOIN dbo.DatalinkMerchantAccount_TEMP AS T1 ON T1.MerchantID = T2.MerchantID[/CODE]2. If you don't care to keep any of the data in the table dbo.DatalinkMerchantAccount_TEMP_old table then you can do something like this:[CODE]TRUNCATE TABLE dbo.DatalinkMerchantAccount_TEMP_old; -- WARNING THIS COMMAND DELETES ALL THE DATA FROM THIS TABLE!!!INSERT INTO dbo.DatalinkMerchantAccount_TEMP_old(MerchantID, DBA, BankAccountNum, BankTransitNum) SELECT MerchantID, DBA, BankAccountNum, BankTransitNum FROM dbo.DatalinkMerchantAccount_TEMP;[/CODE]
Using your first example I wroteUPDATE dbo.DatalinkMerchantAccount_TEMP_old SET DBA = dbo.DatalinkMerchantAccount_TEMP.DBA, BankAccountNum = dbo.DatalinkMerchantAccount_TEMP.BankAccountNum, BankTransitNum = dbo.DatalinkMerchantAccount_TEMP.BankTransitNum FROM dbo.DatalinkMerchantAccount_TEMP_old AS dbo.DatalinkMerchantAccount_TEMP_old JOIN dbo.DatalinkMerchantAccount_TEMP AS dbo.DatalinkMerchantAccount_TEMP ON dbo.DatalinkMerchantAccount_TEMP.MerchantID = dbo.DatalinkMerchantAccount_TEMP_old.MerchantIDand I got this errorMsg 102, Level 15, State 1, Line 4Incorrect syntax near '.'.Why did I use FROM for the old database when I want to put info inside from the 1st database |
|
|
pascal_jimi
Posting Yak Master
167 Posts |
Posted - 2013-06-08 : 02:22:47
|
If you have not solved this problemhow to write clearly and has ties TABLESfor exampleOn t1phone = t2phoneandWhat do you want modifykmkmmm |
|
|
|
|
|
|
|