stored procedure, how to handle errorcode: 1062 - mysql

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.

Related

Mysql procedure, If statement

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

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 */

How to get id after insert statement in MySQL

I have such procedure
DROP PROCEDURE IF EXISTS CreateNativeUser;
delimiter $$
CREATE PROCEDURE CreateNativeUser (p_email varchar(100), p_pass varchar(32))
BEGIN
insert into users(email) values (p_email);
insert into user_logins_native() values(???, p_email, p_pass);
commit;
END $$
delimiter ;
and I need some way to get user_id because user_logins_native has a foreign key on user_id column of users table
Using the LAST_INSERT_ID() function:
insert into users(email) values (p_email);
insert into user_logins_native() values(LAST_INSERT_ID(), p_email, p_pass);

MySQL Stored Procedure, Insert if row doesn't exist. Select id of that row (either inserted row or pre-existing row)

I am creating a stored procedure to register a user.
Here is the create:
CREATE TABLE User (
UID INT AUTO_INCREMENT,
Fname VARCHAR(32) NOT NULL,
Lname VARCHAR(32) NOT NULL,
Phone VARCHAR(32),
Email VARCHAR(50),
Pass VARCHAR(32),
CONSTRAINT User_PK PRIMARY KEY(EmpID),
CONSTRAINT User_UK1 UNIQUE (FName, LName, Phone),
CONSTRAINT User_UK2 UNIQUE (FName, LName, Email),
CONSTRAINT User_UK3 UNIQUE (Email, Pass)
)ENGINE=InnoDB;
Here is the stored procedure so far:
DELIMITER //
CREATE PROCEDURE regUser(fname VARCHAR(32),lname VARCHAR(32),phone VARCHAR(32), email VARCHAR(50), pass VARCHAR(32))
BEGIN
DECLARE uid INT;
DECLARE CONTINUE HANDLER FOR 1062 BEGIN END;
START TRANSACTION;
INSERT INTO User(Fname, Lname, Phone, Email, Pass) VALUES(fname, lname, phone, email, pass);
SELECT UID FROM User WHERE Phone=phone OR Email=email AND Fname=fname AND Lname=lname;
COMMIT;
END //
DELIMITER ;
In the stored procedure it attempts the insert, if a duplicate entry is found the continue handler(1062) catches the error and doesn't do anything. After the insert (or lack of) I want to select the UID of the inserted row, or of the pre-existing row (if no insert was done due to duplicate)
What is happening: The SELECT UID..WHERE... is returning a column of 4 NULL values instead of the UID of the row WHERE Phone=phone OR Email=email AND Fname=fname AND Lname=lname;
I believe the 4 NULL values are because I have 4 Users in the User table at the moment.
I am completely puzzled on this, if I do the SELECT UID..WHERE... outside of the stored procedure, it works just fine, returning the UID WHERE Phone=phone...
I get the same result NULL when inserting a User that is already in the table and when trying a new unique user.

MySQL Stored Procedure syntax error with table variable

I have this MySQL query to create a stored procedure:
Delimiter //;
Create Procedure addUser(
IN facebookId varchar(20),
IN name varchar(50),
In accessToken varchar(100),
in expires float)
Begin
Declare invitingUsers Table(Id varchar(20));
Insert Into Users
(`Facebook_Id`,`Name`,`Access_Token`,`Expired`) Values (facebookId,name,accessToken,expires);
Select Inviting_Id
From Invited_Users
Where Invited_Id = facebookId
Into invitingUsers;
Update Table Users
Set Credit = Credit + 1
Where Facebook_Id In (Select Id From invitingUsers);
End//
but I'm keep getting this error - can't understand why:
#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 'Table(Id varchar(20)); Insert Into Users (`Facebook' at line 7
Change #1
Delimiter //; to Delimiter //
Change #2
Create a Temp Table in Memory
Change #3
Changed
Select Inviting_Id
From Invited_Users
Where Invited_Id = facebookId
Into invitingUsers;
into
INSERT INTO invitingUsers
Select Inviting_Id From Invited_Users
Where Invited_Id = facebookId;
With these changes I give you this:
Delimiter //
Create Procedure addUser(
IN facebookId varchar(20),
IN name varchar(50),
In accessToken varchar(100),
in expires float)
Begin
Declare invitingUsers Table(Id varchar(20));
Create temporary table if not exists invitingUsers
(Id varchar(20), PRIMARY KEY (id)) ENGINE=MEMORY;
Insert Into Users
(`Facebook_Id`,`Name`,`Access_Token`,`Expired`)
Values (facebookId,name,accessToken,expires);
INSERT INTO invitingUsers
Select Inviting_Id From Invited_Users
Where Invited_Id = facebookId;
Update Table Users
Set Credit = Credit + 1
Where Facebook_Id In (Select Id From invitingUsers);
End//
Delimiter ;
Give it a Try !!!