We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

SQL Update Statement Using 2 Tables

Medium Priority
405 Views
Last Modified: 2012-05-06
I am attempting to update two tables.  I have a parent table and child table, and when updating the parent table I also want information in the child table to be updated as well, but I cannot seem to get the update statement to work as expected.

What I am attempting here is when updating the parent table to also update the ttblSalesAppt table column txtAppts.

I already have a select statement where the information is pulling correctly from the ttblSalesAppt table, but at this point I am having to update that table seperatly in order for the select statement to show up correctly.

I hope I have provided enough information regarding this issue.

Would it be smarter to attempt a stored procedure for this also, I have never written one, but have read where this could be used for this situation.

UPDATE    tblSalesManifestGAA
SET              txtLocation = @txtLocation, dtDate = @dtDate, dtPartyTime = @dtPartyTime, txtLNameGuest = @txtLNameGuest, txtFNameGuest = @txtFNameGuest, 
                      txtProgCode = @txtProgCode, txtMktgComment = @txtMktgComment, txtBooker = @txtBooker, txtComments = @txtComments, ckShow = @ckShow, 
                      ckNoShow = @ckNoShow
FROM         tblSalesManifestGAA INNER JOIN
                      ttblSalesAppt ON tblSalesManifestGAA.pkSalesManGAAID = ttblSalesAppt.pkSalesManGAAID
WHERE     (tblSalesManifestGAA.pkSalesManGAAID = @pkSalesManGAAID)

Open in new window

ViewUpdate.bmp
Comment
Watch Question

You cannot update two tables with one statement.  You would have to use two separate statements.  

Author

Commented:
Okay so how would this be accomplished using just a Cross Join, and updating each table as a seperate entity seperated by a " ; " or just two statements seperate within the builder such as:

Update tableA
Set       txtsomething=@txtsomething
From    tableA
Where txtsomething=@txtsomething

Update tableB
Set txtsomethingb=@txtsomethingb
From tableB
Where txtsomethingb=@txtsomethingb

Am I on the right track here, or completely off?
That's not a cross join.  A cross join is a join whereby every record from one table is joined with every record from another.  Thus producing a cartesian.

Yes, two update statements.
Commented:
Yes, two update statements preferably within a transaction like -

BEGIN TRAN ...
Update Table1...
Update Table2...
COMMIT

This way the parent/child tables should be in sync..

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
Instead of writing any code to update the column in other table, if you want to store same value in column of other table, then it would be recommended to use relationship between two table.
What you can do is make a relationship between two tables and set cascade update. The child table will be automaitically updated when the value in parent table changes.

Author

Commented:
Yeah that is a new concept for me, have not been working with SQL for very long, I am still trying to figure out the relationship of Primary key to Foreign key.

Are there any sites you can direct me to in order to accomplish this task.

Any help would be appreciated.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.