SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On


123»»»

Need to copy data from table to table with update statement Expand / Collapse
Author
Message
Posted Tuesday, December 09, 2008 3:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 11, 2009 9:57 PM
Points: 31, Visits: 74
Here are the tables involved:

MachineName table has two fields: machineName and machineID

MachineAudit table has two fields: machineID and leaseNumber

LeaseInfoImport table has two fields: computerName and leaseNumber

The LeaseInfoImport table was created with the DTS import wizard and the data came from an Excel sheet. Currently, the leaseNumber is not populated in the MachineAudit table. So, I am looking to populate it based on the data from the LeaseInfoImport table.

I need to do something like this, but don't know the correct syntax:

populate/update the leaseNumber row in the MachineAudit table where the LeaseInfoImport.[computerName] = MachineName.[machineName]

Thanks in advance,

Pat B.
Post #616548
Posted Tuesday, December 09, 2008 3:15 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: Administrators
Last Login: Today @ 11:41 AM
Points: 19,451, Visits: 5,034
update a
set leasenumber = c.
from machineaudit a
inner join machinename b
on a.computername = b.machinename
inner join leaseinfoimport c
on b.leasenumber= c.leasenumber

Not sure what you want to import.

You need to check to be sure that the data is matching up correctly. I'd run this as a select first to see if things match up. Return all values and be sure of what you ware updating.
Post #616556
Posted Tuesday, December 09, 2008 3:16 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Thursday, June 11, 2009 11:23 AM
Points: 766, Visits: 1,909
populate/update the leaseNumber row in the MachineAudit table where the LeaseInfoImport.[computerName] = MachineName.[machineName]


First...

SELECT MA.MachineID, MN.MachineName, L.LeaseNumber
FROM MachineAudit MA
INNER JOIN MachineName MN ON MA.MachineID = MN.MachineID
INNER JOIN LeaseInfoImport L ON L.[computerName] = MN.[machineName]

And if that gives you what you're looking for...

UPDATE MachineAudit
SET LeaseNumber = L.LeaseNumber
FROM MachineAudit MA
INNER JOIN MachineName MN ON MA.MachineID = MN.MachineID
INNER JOIN LeaseInfoImport L ON L.[computerName] = MN.[machineName]



Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots
Post #616557
Posted Tuesday, December 09, 2008 3:21 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 9:27 AM
Points: 1,227, Visits: 3,459
Sorry original posted to incorrect forum, my apologies

If everything seems to be going well, you have obviously overlooked something.

Before posting a question please read

Before posting a performance problem please read
Post #616559
Posted Tuesday, December 09, 2008 3:45 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 2:17 AM
Points: 271, Visits: 1,667
Try this and let know this is what you require. Note that you should provide sample data and script for better response.

create table MachineName (machineName varchar(50), machineID int)
insert into MachineName values ('PC1', 1)
insert into MachineName values ('PC2', 2)
insert into MachineName values ('PC3', 3)

create table MachineAudit (machineID int, leaseNumber int)
insert into MachineAudit values (1, null)
insert into MachineAudit values (2, null)
insert into MachineAudit values (3, null)

create table LeaseInfoImport (computerName varchar(50), leaseNumber int)
insert into LeaseInfoImport values ('PC1', 11)
insert into LeaseInfoImport values ('PC2', 12)
insert into LeaseInfoImport values ('PC3', 13)



update MachineAudit set leaseNumber = b.leaseNumber
from MachineAudit a,
(select a.machineID, b.leaseNumber from MachineName a, LeaseInfoImport b
where a.machineName = b.computerName) b
where a.machineID = b.machineID



-----------------------------------------------------------
Time Is Money
Calculating the Number of Business Hours Passed since a Point of Time
Calculating the Number of Business Hours Passed Between Two Points of Time
Post #616567
Posted Tuesday, December 09, 2008 6:07 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 11, 2009 9:57 PM
Points: 31, Visits: 74
bitbucket -

It sounds like I might have posted to the wrong forum. Is that the case? I tried to access the article you referenced (http://www.sqlservercentral.com/articles/Best+Practices/61537/ ), but I get this error:

Sorry, an error has occurred
Please check the URL, some characters in it triggered this error.
If problems persist, please contact us to let us know.

I also just looked under the Articles section for Best Practices, but did not see it there.

What Forum should I have posted under?

Sorry for any inconvenience,

Pat


Post #616599
Posted Tuesday, December 09, 2008 6:42 PM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: Thursday, June 11, 2009 11:23 AM
Points: 766, Visits: 1,909
You were fine Pat, I think he meant HE posted to the wrong forum.

The best practices post is the same one many of us (including myself) have in our signatures for how to post questions on the forum, but that's not what he was originally linking (if I remember the post from earlier correctly).


Seth Phelabaum
Consistency is only a virtue if you're not a screwup. ;)

Links: How to Post Sample Data :: Running Totals :: Tally Table :: Cross Tabs/Pivots
Post #616603
Posted Tuesday, December 09, 2008 7:04 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 9:27 AM
Points: 1,227, Visits: 3,459
Pbreitenbeck
I posted to the incorrect forum YOU DID NOT.

And thanks about the bug in the signature block, it used to work and I have cut and pasted the correct url into the signature block and tested and it still does not work ? ? ?

Here is the url

http://www.sqlservercentral.com/articles/Best+Practices/61537/

After this posts I will test again

More importantly the link in Garadin's post does work


If everything seems to be going well, you have obviously overlooked something.

Before posting a question please read

Before posting a performance problem please read
Post #616611
Posted Tuesday, December 09, 2008 7:33 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 1:30 PM
Points: 16,213, Visits: 8,849
Worked for me.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #616613
Posted Wednesday, December 10, 2008 8:12 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, May 11, 2009 9:57 PM
Points: 31, Visits: 74
Thanks for all the super fast responses. Unfortunately, there are a few things that I need clarification on. This might seem basic, but it appears that you are using correlation names/table aliases in the examples (without the AS keyword), right? I thought that when using them, it is necessary to first declare them (with the AS keyword), but it appears that you are using them without declaring them. For example, in the code below MA is referenced in the SELECT statement before it is declared in the FROM statement such as MachineAudit AS MA. However, based on what I am seeing, I am gathering that it is okay to do this?

SELECT MA.MachineID, MN.MachineName, L.LeaseNumber
FROM MachineAudit MA
INNER JOIN MachineName MN ON MA.MachineID = MN.MachineID
INNER JOIN LeaseInfoImport L ON L.[computerName] = MN.[machineName]

Post #617070
« Prev Topic | Next Topic »

123»»»

Permissions Expand / Collapse