EB.
EB.

Reputation: 2757

Update 2 tables using Stored Procedure in MySql

This seems like a simple request. But my query in not working and I'm finding conflicting answers on the internet. Is it possible to have UPDATE and INSERT using a Stored Procedure joining 2 tables in MySql?

I have an Asp.net Webforms website. It has 2 tables Individual and Address. Individual table contains data on an individual, i.e. Phone Number, Fax, Email ect. The address table has all the address information for an individual. They each table has a column of Individual ID which auto increments. (Note: the individualID in Address table is not a primary key, but individualID in the individual table is a primary key.

Anyway, I have a FormView in Asp.net that with a SELECT statement that joins those 2 tables and display the data fine. But updating new information to both tables keeps failing.

My most recent error is : Duplicate entry '0' for key 'PRIMARY'

Is there a way to write an UPDATE statement that joins 2 Tables?? This has to exist right?

Upvotes: 0

Views: 2223

Answers (1)

user1191247
user1191247

Reputation: 12998

It is possible to update multiple tables with a single query -

UPDATE table1
INNER JOIN table2
  ON table1.id = table2.table1_id
SET table1.col1 = 'some value', table2.col1 = 'Another value'
WHERE <some where clause>;

Upvotes: 1

Related Questions