Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple insert statements in the same stored procedure

Posted on 2006-11-09
2
Medium Priority
?
504 Views
Last Modified: 2008-02-01
Hello Experts,
I am trying to build a stored procedure in MySQL that runs 1 insert statement, get the identity, then runs another insert statement.
Here is my procedure:
CREATE PROCEDURE usp_sch_insert_subevent_attendees_dev(
      IN transaction_key int,
      IN attendee_key int,
      IN sub_event_key int,
      IN event_key int,
      IN subevent_comments varchar(500),
      IN fee int,
      IN comp int,
      IN comp_amt int,
      IN comp_code varchar(20),
      IN pmt_type char(10)
 )

BEGIN


INSERT INTO sch_invoice
(
transaction_key,
attendee_key,
sub_event_key,
fee,
comp,
comp_amt,
pmt_type
)
VALUES
(
transaction_key,
attendee_key,
sub_event_key,
fee,
comp,
comp_amt,
pmt_type
)


DECLARE i_key int
SET i_key = last_insert_id()



INSERT INTO sch_subevent_attendees
(
      attendee_key,
      sub_event_key,
      event_key,
      subevent_comments,
      fee,
      comp,
      comp_amt,
      comp_code,
      transaction_key,
      invoice_key,
      pmt_type
)
VALUES
(
      attendee_key,
      sub_event_key,
      event_key,
      subevent_comments,
      fee,
      comp,
      comp_amt,
      comp_code,
      transaction_key,
      i_key,
      pmt_type
)

END;

And here is the very vague error I'm getting:
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO sch_invoice
(
transaction_key,
attendee_key,
sub_event_key,
fee,
com' at line 14
(16 ms taken)

If I take one of the insert statements out, it runs OK. Is it not possible to do this in MySQL 5.0.22-standard?
Thanks
Chad
0
Comment
Question by:ChadMarsh
2 Comments
 
LVL 33

Accepted Solution

by:
snoyes_jw earned 2000 total points
ID: 17909475
You need ; after each insert and declaration.  You'll need to change the delimiter to something other than ; using the DELIMITER statement, so that MySQL will process the whole thing as one stored procedure.  See http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html for examples.
0
 
LVL 2

Author Comment

by:ChadMarsh
ID: 17913829
Thanks a lot. That worked perfectly.
Chad
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses
Course of the Month11 days, 2 hours left to enroll

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question