I am trying to make a stored procedure handler for duplicate username entries.
the procedure manages to create it self, but when i enter inn the information into the tables i only get the standard error from mysql. How do i sort this out?
DELIMITER $$
DROP PROCEDURE IF EXISTS Testproc2$$
create procedure testproc2(filename VARCHAR (40), descriptionn VARCHAR(40),
username VARCHAR(40), firstname VARCHAR(40), lastnameVARCHAR(40), classcode INT(10))
begin
declare exit handler for 1062
select concat('Feil: username',username,'already exist');
start transaction;
insert into bilde
values (NULL, filnavn, beskrivelse);
insert into student
values (username, firstname, lastname, classcode, LAST_INSERT_ID());
commit;
SELECT CONCAT('Inserted student ',username,' ',firstname' ',lastname' ',classcode' ', LAST_INSERT_ID());
END$$
I found my error, i called for testproc1 instead of testproc2.
I need to create 1 procedure and 1 trigger which both logs insert of a car in Car-table: car(reg_nr, color, brand). I have created the trigger like this:
Create table CarLog(
ID integer,
User varchar (32),
Primary key (ID)
)engine=innodb;
DELIMITER //
CREATE TRIGGER CarsLog AFTER INSERT ON Car
FOR EACH ROW BEGIN
INSERT INTO CarLog(User) values ( user() ) ;
END;
//
DELIMITER ;
But how do I create a procedure which does the same thing as the trigger above?
How do i make an if statment that ROLLBACK: if the studentbrukernavn (students username) already exist, and if the klassekode (classcode) does not exist
DELIMITER $$
DROP PROCEDURE IF EXISTS OPPRETT_STUDENT$$
CREATE PROCEDURE OPPRETT_STUDENT
(
IN bilde_bildenr INT,
IN bilde_filnavn VARCHAR(30),
IN bilde_beskrivelse VARCHAR(30),
IN student_brukernavn VARCHAR(30),
IN student_fornavn VARCHAR(30),
IN student_etternavn VARCHAR(30),
IN student_klassekode VARCHAR(30)
)
BEGIN
START TRANSACTION;
INSERT INTO bilde
VALUES (bilde_bildenr,
CONCAT('bilder/',student_brukernavn,'.jpg'),
CONCAT('bilde av ',student_fornavn,' ',student_etternavn)
);
INSERT INTO student VALUES (student_brukernavn, student_fornavn, student_etternavn, student_klassekode, bilde_bildenr);
COMMIT;
END$$
DELIMITER ;
IF NOT EXISTS(SELECT query) THEN
ELSE
END IF;
Hope this helps
What is the best method to atomically update a row if exists, else insert a new row into an table?
e.g. say we have this example below.
CREATE TABLE Test(
id integer,
val varchar(255)
)
DELIMITER $$
DROP PROCEDURE IF EXISTS `TestUpsert` $$
CREATE PROCEDURE `TestUpsert` (in value1 varchar(255), in id1 int)
BEGIN
IF EXISTS(Select 1 from Test where id=id1) THEN
UPDATE Test set val = value1 where id = id1;
ELSE
insert into Test(id, val) VALUES (id1, value1);
END IF;
END $$
DELIMITER ;
What changes can we make to TestUpsert to make it atomic?
Use on duplicate key update:
CREATE PROCEDURE `TestUpsert` (
in in_value varchar(255),
in in_id int
)
BEGIN
insert into test(id, val)
values (in_id, in_val)
on duplicate key update val = in_val;
END $$
I should add that for this to work, test.id has to be declared as the primary key or unique.
I recommend you read about INSERT ... ON DUPLICATE KEY UPDATE.
See my lengthy answer about this statement here: https://stackoverflow.com/a/548570/20860
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 */