Solved

Multiple insert statements in the same stored procedure

Posted on 2006-11-09
2
491 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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This guide whil teach how to setup live replication (database mirroring) on 2 servers for backup or other purposes. In our example situation we have this network schema (see atachment). We need to replicate EVERY executed SQL query on server 1 to…
I use MySQL for many of my development projects in a Windows environment. To manage my databases (and perform queries) for years I used a tool called MySQL administrator.  This tool has since been replaced by MySQL Workbench. So I decided to m…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now