Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Update

Status
Not open for further replies.

abenitez77

IS-IT--Management
Oct 18, 2007
147
US
I have 2 tables (tableA and tableB). TableA has about 50 records. TableB has about 3 million records. I want to update 1 column1 in TableA if TableA.keyfield = TableB.keyfield. If there is no match on keyfield, will the update still try to run and not update anything? I know updates can be slow and I don't want to use up a lot of resources on the server if there is not keyfield match. What is the best way to approach this?
 
Yes, the script still has to run to find out if the condition exists or not.

You can always try something like this, but then I'm not sure what you would gain. You would have to test it to see if it provides any improvement.

Code:
SELECT TableA.Keyfield
INTO #keyfield
FROM TableA
WHERE Keyfield EXISTS (SELECT Keyfield FROM TableB)
GO
Then have your update check #keyfield to see if there are any values, if not, don't run the update. If values exist, then update the rows in TableB that match keyfield from #keyfield.

Again, I'm not sure you will see any 'savings' from doing that.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top