insert into in stored procedure with parameters MYSQL doesnt work

44,380

Solution 1

You need to change the delimiter, like this:

# change the delimiter to $$, so you can use semicolon in create procedure
DELIMITER $$

USE deb42181_ramos$$

DROP PROCEDURE IF EXISTS sp_insertuser$$

CREATE PROCEDURE sp_insertuser(IN gebruikersnaamparam varchar(10)
, IN wachtwoordparam VARCHAR(50)
, IN voornaamparam VARCHAR(15)
, IN achternaamparam VARCHAR(15)
, IN tussenvoegselparam VARCHAR(10)
, IN gebruikerlevelparam INT)
BEGIN

INSERT INTO gebruikers (
gebruikersnaam
, wachtwoord
, voornaam
, achternaam
, tussenvoegsel
, gebruikerlevel)

    VALUES (gebruikersnaamparam
    , wachtwoordparam
    , voornaamparam
    , achternaamparam
    , tussenvoegselparam
    , gebruikerlevelparam);

END$$
# change the delimiter back to semicolon
DELIMITER ;

Solution 2

DELIMITER $$
DROP PROCEDURE IF EXISTS `database_name`.`ins`$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ins`(in nam varchar(50),in username varchar(50), in branch varchar(50))
BEGIN
insert into table_name(nam,user_name,branch) values(nam,username,branch);
    END$$
DELIMITER ;

call ins('sas','sdsd','sdsd')
Share:
44,380
Jeroen
Author by

Jeroen

Updated on September 19, 2020

Comments

  • Jeroen
    Jeroen over 3 years

    i am trying to make a stored procedure with parameters using mysql workbench to insert data into a table.

    what am i doing wrong??

        USE deb42181_ramos;
    CREATE PROCEDURE sp_insertuser(IN gebruikersnaamparam varchar(10)
    , IN wachtwoordparam VARCHAR(50)
    , IN voornaamparam VARCHAR(15)
    , IN achternaamparam VARCHAR(15)
    , IN tussenvoegselparam VARCHAR(10)
    , IN gebruikerlevelparam INT)
    BEGIN
    
    INSERT INTO gebruikers (
    gebruikersnaam
    , wachtwoord
    , voornaam
    , achternaam
    , tussenvoegsel
    , gebruikerlevel)
    
        VALUES (gebruikersnaamparam
        , wachtwoordparam
        , voornaamparam
        , achternaamparam
        , tussenvoegselparam
        , gebruikerlevelparam);
    
    END
    

    the error is in the last row of the values after ) he doesnt expect a ;
    regards Jeroen

  • Jeroen
    Jeroen over 10 years
    ow thanks now it works, but why do i need that? wat is the reason?
  • Zagor23
    Zagor23 over 10 years
    By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server. Otherwise, MySQL breaks CREATE PROCEDURE, before it reaches the END statement. You can see the documentation for more details: dev.mysql.com/doc/refman/5.5/en/stored-programs-defining.htm‌​l