insert into in stored procedure with parameters MYSQL doesnt work - mysql

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

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 ;

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')

Related

#1064 - You have an error in your SQL syntax

I already have a table employee with columns name(String),id(int),age(int).
I can't figure out where the syntax is wrong?
CREATE PROCEDURE recins (
name1 IN employee.name%type ,
id1 IN employee.id%type ,
age1 IN employee.age%type
) AS
BEGIN
INSERT INTO employee VALUES(name1,id1,age1);
END;
create table employee2
(
name varchar(100) not null,
id int not null,
age int not null
);
DELIMITER $$
CREATE PROCEDURE recins (
IN name1 varchar(100),
IN id1 int,
IN age1 int
)
BEGIN
INSERT INTO employee2 (name,id,age) VALUES(name1,id1,age1);
END $$
DELIMITER ;
-- test:
call recins('a',1,2);
delimiter is a special wrapper for stored procs, events, functions. Delimiter ; at the end of the stored proc sets it back to the normal/default delimiter of a semi-colon.
The above was tested.

mysql Insert Statements in Stored Procedure not Inserting

I created a stored procedure which should perform the following:
Insert into Agencies Table, creating an id in the PK index row
Save the id in a variable
Insert the id and other data into Users table
The syntax below does not trigger any errors:
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END
However, the Stored Proc doesn't insert my parameters into the tables when executed. So, after searching I try to create the SP like this (including $$ after "END" and resetting the delimiter to a semi-colon ):
DELIMITER $$
CREATE PROCEDURE insertNewAgencyAndAdmin (IN aName varchar (100), IN numTrav int, IN polType int, IN uEmail varchar(255), IN uFName varchar(40), IN uLName varchar(40), IN uTitle varchar(100))
BEGIN
INSERT INTO btsAgency.Agencies (agencyName, numTrav, polType) VALUES (#aName, #numTrav, #polType);
SET #agencyID = (SELECT agencyID from btsAgency.Agencies where agencyID = LAST_INSERT_ID());
INSERT INTO btsUsers.Users (userAgencyID, userEmail, userFirstName, userLastName, userTitle ) VALUES
(#agencyID, #uEmail, #uFName, #uLName, #uTitle);
END $$
DELIMITER ;
MySql creates the Stored Proc but gives me the following error:
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 'DELIMITER' at line 1
When I execute the created Stored Proc it still doesn't insert the data in my parameters.
Any help would be appreciated.
Some problems in your stored procedure:
It is important to indicate the difference between 9.4. User-Defined Variables and routine parameters 13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax, are different variables (eg.: aName != #aName).
Avoid naming parameters or variables as columns of your tables.
DELIMITER $$
CREATE PROCEDURE `insertNewAgencyAndAdmin` (
/*
IN `aName` varchar (100),
IN `numTrav` int,
IN `polType` int,
IN `uEmail` varchar(255),
IN `uFName` varchar(40),
IN `uLName` varchar(40),
IN `uTitle` varchar(100)
*/
IN `_aName` varchar (100),
IN `_numTrav` int,
IN `_polType` int,
IN `_uEmail` varchar(255),
IN `_uFName` varchar(40),
IN `_uLName` varchar(40),
IN `_uTitle` varchar(100)
)
BEGIN
/*
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
#`aName`,
#`numTrav`,
#`polType`
);
*/
INSERT INTO `btsAgency`.`Agencies` (
`agencyName`,
`numTrav`,
`polType`
) VALUES (
`_aName`,
`_numTrav`,
`_polType`
);
/*
SET #`agencyID` = (SELECT `agencyID` from `btsAgency`.`Agencies` where `agencyID` = LAST_INSERT_ID());
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
#`agencyID`,
#`uEmail`,
#`uFName`,
#`uLName`,
#`uTitle`
);
*/
INSERT INTO `btsUsers`.`Users` (
`userAgencyID`,
`userEmail`,
`userFirstName`,
`userLastName`,
`userTitle`
) VALUES (
LAST_INSERT_ID(),
`_uEmail`,
`_uFName`,
`_uLName`,
`_uTitle`);
END$$
DELIMITER ;
SELECT * FROM discounts $$
delimiter $$;
CREATE PROCEDURE insertData(IN id int,IN title varchar(255),IN amount decimal)
BEGIN
insert into discounts values (id,title,amount);
END; $$
call insertData(3,"sadasd",56);
/*DROP PROCEDURE insertData */

Stored Proc not performing any actions

I have this stored proc
DELIMITER $$
DROP PROCEDURE IF EXISTS RegisterUser$$
CREATE PROCEDURE `RegisterUser`(IN `username` VARCHAR(30), IN `password` VARCHAR(30), IN `firstname` VARCHAR(25), IN `lastname` VARCHAR(30), IN `email` VARCHAR(35))
BEGIN
INSERT INTO users
(
username,
password,
firstname,
lastname,
email
)
VALUES
(
#username,
#password,
#firstname,
#lastname,
#email
);
END $$
DELIMITER ;
Whenever i try to execute this in PHPmyadmin or from the PHP code, no records are being inserted and no errors come up. Thanks for the help!
Don't use # since your parameters don't have them too.
DELIMITER $$
DROP PROCEDURE IF EXISTS RegisterUser$$
CREATE PROCEDURE `RegisterUser`(IN `uname` VARCHAR(30), IN `pass` VARCHAR(30), IN `first` VARCHAR(25), IN `last` VARCHAR(30), IN `mail` VARCHAR(35))
BEGIN
INSERT INTO users (username, password, firstname, lastname, email)
VALUES (uname, pass, first, last, mail);
END $$
DELIMITER ;
And use different parameter names than your column names to avoid mistakes.

Error 1064 on MySQL 5 database

I keep getting an error while trying to create a stored procedure. I am using the following SQL:
CREATE PROCEDURE sp_add_mailing_list_item
(
IN paramA VARCHAR(50) ,
IN paramB VARCHAR(50) ,
IN paramC VARCHAR(255) ,
IN paramD DATE ,
IN paramE VARCHAR(255) ,
)
BEGIN
INSERT INTO MailingList
(
firstName ,
lastName ,
email ,
dob ,
notes
)
VALUES
(
paramA ,
paramB ,
paramC ,
paramD ,
paramE
)
END
I am pretty well versed in SQL (for MSSQL Server), but this is just eluding me. Any help is appreciated. I have tried using a delimiter, but that brings up all kinds of other errors.
Thanks.
you have an extra comma here
IN paramE VARCHAR(255) ,
EDIT: try this .
DELIMITER $$
DROP PROCEDURE IF EXISTS sp_add_mailing_list_item $$
CREATE PROCEDURE sp_add_mailing_list_item
(
IN paramA VARCHAR(50) ,
IN paramB VARCHAR(50) ,
IN paramC VARCHAR(255) ,
IN paramD DATE ,
IN paramE VARCHAR(255)
)
BEGIN
INSERT INTO MailingList
(
firstName ,
lastName ,
email ,
dob ,
notes
)
VALUES
(
paramA ,
paramB ,
paramC ,
paramD ,
paramE
)
END $$
DELIMITER

Inserting old record into other table before updating MySQL Triggers

I'd like make a copy of old record in my database. I tried use trigger but there's something wrong.
I'm using MySQL Workbench so I post this code:
USE `secus_21`;
DELIMITER $$
CREATE TRIGGER `tsm_tasks_BUPD` BEFORE UPDATE ON tsm_tasks FOR EACH ROW
INSERT INTO `tsm_tasks_history`
( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` );
to update event, added new record, and tried change something. I didnt receive any error so i was sure that adding finished successfuly but in tsm_tasks_history table still have nothing.
Edit:
Here are all trigger statement
-- Full Trigger DDL Statements
-- Note: Only CREATE TRIGGER statements are allowed
DELIMITER $$
USE `secus_21`$$
CREATE TRIGGER `tsm_tasks_BUPD` BEFORE UPDATE ON tsm_tasks FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
INSERT INTO `tsm_tasks_history`
( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` )
$$
CREATE TRIGGER `tsm_tasks_BDEL` BEFORE DELETE ON tsm_tasks FOR EACH ROW
-- Edit trigger body code below this line. Do not edit lines above this one
INSERT INTO `tsm_tasks_history`
( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` )
$$
You changed the delimiter to $$ but didn't finish creation of trigger with $$. So the statement didn't complete.
USE `secus_21`;
DELIMITER $$
DROP TRIGGER IF EXISTS tsm_tasks_BUPD;
CREATE TRIGGER `tsm_tasks_BUPD` BEFORE UPDATE ON tsm_tasks FOR EACH ROW
BEGIN
INSERT INTO `tsm_tasks_history`
( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` );
END $$
DROP TRIGGER IF EXISTS tsm_tasks_BDEL;
CREATE TRIGGER `tsm_tasks_BDEL` BEFORE DELETE ON tsm_tasks FOR EACH ROW
BEGIN
INSERT INTO `tsm_tasks_history`
( `ID_task` , `ID_user` , `ID_project` , `name` , `add_time` , `comment` )
VALUES
( OLD.`ID` , OLD.`ID_user` , OLD.`ID_project` , OLD.`name` , NOW() , OLD.`comment` );
END $$
DELIMITER ;