1

What's the right way to create in MySQL a PROCEDURE for multiple queries like this one made for SQL?

CREATE OR REPLACE PROCEDURE foo
IS
BEGIN
  -- The create sentence goes here. For example:
  -- EXECUTE IMMEDIATE
  -- 'CREATE TABLE bar (...)';

  -- The update sentence goes here
  -- EXECUTE IMMEDIATE
  -- 'UPDATE bar SET ...';

  -- The drop/delete sentence goes here.
  -- EXECUTE IMMEDIATE
  -- 'DROP TABLE bar;'
END;
7
  • 1
    You don't need no execute statements. Just the queries seperated by ;. Don't forget to set a different separator outside the procedure like delimiter $ Commented Jun 24, 2015 at 12:35
  • dev.mysql.com/doc/refman/5.0/en/create-procedure.html Commented Jun 24, 2015 at 12:37
  • @juergend I saw this reply bad voted about it (that's the reason why I'm here) stackoverflow.com/a/6212730/4458531 What do you think? Commented Jun 24, 2015 at 12:43
  • 1
    @Simone: Don't know why. But I am not a PLSQL expert. In MySQL using just the queries is the way to go. Commented Jun 24, 2015 at 12:47
  • @juergend thanks, I don't understand the delimiter...should $ or // ...what's the difference? Commented Jun 24, 2015 at 12:49

2 Answers 2

5

In MySQL just insert the 3 queries into your procedure. You need a different delimiter defined before, because otherwise the engine will terminate the procedure definition at the first ; which would make it incomplete. So change to anything else like $ or //

DELIMITER //
CREATE PROCEDURE your_proc()
BEGIN
  create ... ;
  update ... ;
  drop ... ;
END //
DELIMITER ;
Sign up to request clarification or add additional context in comments.

Comments

0
CREATE PROCEDURE foo 
    @LastName nvarchar(50), 
    @FirstName nvarchar(50) 
AS 
    QUERY1; -- SELECT or whatever
    QUERY2;
GO

2 Comments

and maybe you should explain the final GO
SQL-Server uses go, MySQL not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.