Get OUT paramater in mysql - mysql

DELIMITER //
CREATE PROCEDURE InsertUser
(
IN RoleID INT,
IN UserEmail VARCHAR(100),
IN UserPassword VARCHAR(250),
OUT ID INT
)
BEGIN
INSERT INTO useraccount
(
RoleID,
UserEmail,
UserPassword,
CreateDate
)
VALUES
(
RoleID,
UserEmail,
UserPassword,
CURRENT_TIMESTAMP
);
SET ID = (SELECT UserID FROM useraccount WHERE UserEmail = UserEmail);
END //
DELIMITER ;
I want to get OUT Parameter ID but when i execute this procedure i got the error message "Subquery returns more than 1 row" can you tell me my mistake?
Thank You

Your subquery is wrong here:
SELECT UserID FROM useraccount WHERE UserEmail = UserEmail
Here you are compare UserEmail with itself which is going to be true for every row in the table ith not null values.
I would suggest the better approach will be to change the name of your UserEmail IN parameter(lets say it to email) :
DELIMITER //
CREATE PROCEDURE InsertUser
(
IN RoleID INT,
IN email VARCHAR(100),
IN UserPassword VARCHAR(250),
OUT ID INT
)
BEGIN
INSERT INTO useraccount
(
RoleID,
UserEmail,
UserPassword,
CreateDate
)
VALUES
(
RoleID,
email ,
UserPassword,
CURRENT_TIMESTAMP
);
SET ID = (SELECT UserID FROM useraccount WHERE UserEmail = email);
END //
DELIMITER ;
Please try this. Hope you are making sure that 2 useraccounts dont have same email

Related

MySQL: insert procedure, with variable from another table

I have two tables:
CREATE TABLE userTypes (
id INTEGER NOT NULL PRIMARY KEY,
type VARCHAR(50) NOT NULL
);
CREATE TABLE users (
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(50) NOT NULL,
userTypeId INTEGER NOT NULL,
FOREIGN KEY (userTypeId) REFERENCES userTypes(id)
);
INSERT INTO userTypes (id, type) VALUES (0, 'free');
INSERT INTO userTypes (id, type) VALUES (1, 'paid');
I want to create a procedure where that it inserts a user in the users table, with :
id is auto incremented.
email is equal to the email parameter.
userTypeId is the id of the userTypes row whose type attribute is equal to the type parameter
The INSERT function doesn't work with WHERE, so I tried to add a UPDATE but it's not working. Here's what I have for the moment:
DELIMITER //
CREATE PROCEDURE insertUser(
IN type VARCHAR(50),
IN email VARCHAR(50)
)
BEGIN
INSERT INTO users(id, email, userTypeID) VALUES (LAST_INSERT_ID(), email, userTypeID);
UPDATE users SET users.userTypeID = usertypes.id
WHERE usertypes.type = type;
END//
DELIMITER ;
The expected result should be something like this:
CALL insertUser('free', 'name_1#mail.com');
CALL insertUser('paid', 'name_2#mail.com');
SELECT * FROM users;
id email userTypeId
------------------------------------------
1 name_1#mail.com 0
2 name_2#mail.com 1
Leave out the auto_increment-column. As the name suggests, the db will fill this automatically.
Then, use different names for the parameters than the column names. You can use a prefix with the parameters.
Additionally, you could consider using the userTypeId integer value as parameter instead of the textual value. Otherwise you need to handle the situation where the passed type is not among the types in the userTypes (create a new one/reject insert).
DELIMITER //
CREATE PROCEDURE insertUser(
in_type VARCHAR(50),
in_email VARCHAR(50)
)
BEGIN
INSERT INTO users(email, userTypeID)
SELECT in_email, id
FROM userTypes
WHERE type=in_type;
IF (ROW_COUNT()=0) THEN
SELECT 'Error';
ELSE
SELECT 'OK';
END IF;
END//
DELIMITER ;

Mysql stored procedure insert into multiple tables

Am trying to insert data into 2 tables but only 1 table gets the data but the other one doesn't. I don't get any error messages but only the login table gets populated but the user doesn't.
Here is my code
CREATE DEFINER=`root`#`localhost` PROCEDURE `new_user`(
_email varchar(50),
_password varchar(255),
_name varchar(35),
_surname varchar(35)
)
BEGIN
start transaction;
insert into logins_copy (
email,
password,
created_at,
updated_at
)
values
(
_email,
_password,
current_timestamp(),
current_timestamp()
);
SELECT #user_id := id from logins_copy where email = _email;
insert into users (
user_id,
name,
surname,
updated_at
)
values
(
#user_id,
_name,
_surname,
current_timestamp()
);
commit;
END
and to test this I called the procedure:
call `new_user`("test#hotmail.com", "test123", "Adam", "James");

Couldn't get the call addusers procedure to work for testing because of auto increment

I have problem with Addusers procedure because I add UserID as out for auto increment since I basically want to pass:
CALL addusers
(
'Rory',
'Covertry',
'5566',
'rory#gmail.com'
'1234',
'U'
)
I'm expected to insert the data but I ended getting an error like this.
"Error
SQL query:
CALL addusers
(
'Rory',
'Covertry',
'5566',
'rory#gmail.com'
'1234',
'U'
)
MySQL said: Documentation
1318 - Incorrect number of arguments for PROCEDURE construction.addusers; expected 7, got 5"
Here's my stored procedure for
addusers
DELIMITER go
Create procedure Addusers(
Out UserID int(11),
IN FirstName varchar(30),
IN LastName varchar(30),
IN Password varchar(30),
IN EmailAddress varchar(30),
IN Salt varchar(40),
IN RoleID varchar(1))
BEGIN
insert into users(
FirstName,
LastName ,
Password ,
EmailAddress ,
Salt ,
RoleID
)
Values
(
FirstName,
LastName ,
Password ,
EmailAddress ,
Salt ,
RoleID
);
set UserID = AUTO_INCREMENT;
End
go
DELIMITER ;
You must use last_insert_id() instead of AUTO_INCREMENT reserved word, and put all 7 parameters when you do a call:
DELIMITER go
Create procedure Addusers(
Out UserID int(11),
IN FirstName varchar(30),
IN LastName varchar(30),
IN Password varchar(30),
IN EmailAddress varchar(30),
IN Salt varchar(40),
IN RoleID varchar(1))
BEGIN
insert into users(
FirstName,
LastName ,
Password ,
EmailAddress ,
Salt ,
RoleID
)
Values
(
FirstName,
LastName ,
Password ,
EmailAddress ,
Salt ,
RoleID
);
set UserID = last_insert_id();
End
go
DELIMITER ;
To get the OUT parameter you must use a session variable like this:
set #new_id = null;
call addusers(#new_id,'Rory','Covertry','5566','rory#gmail.com','1234','U' );
select #new_id; -- contains the inserted id

MySQL Stored procedure IN parameter issue

In my Mysql , I have written below stored procedure to insert data into user table,
DELIMITER $$
DROP PROCEDURE IF EXISTS `CreateUser1`$$
CREATE PROCEDURE `CreateUser1`(
IN Email VARCHAR(50),
IN Password1 VARCHAR(50),
IN FirstName VARCHAR(50),
IN LastName VARCHAR(50),
IN AlternateEmail VARCHAR(50),
IN PhoneNumber VARCHAR(50),
IN Token VARCHAR(500)
)
BEGIN
IF NOT EXISTS( SELECT user_id FROM `um.user` WHERE `email`=Email)THEN
INSERT INTO `um.user`(site_id,email,PASSWORD,alternate_email,first_name,last_name,contact_number,
created_on,updated_on,is_active,token,is_verified_email)
VALUES
(1, Email1 , Password1 ,AlternateEmail, FirstName , LastName ,PhoneNumber,UTC_TIMESTAMP(),UTC_TIMESTAMP(),1,Token,0);
END IF;
END$$
DELIMITER ;
When i test this procedure as below,
CALL `CreateUser1`('ab1#ansys.com' , 'abcdefgh' ,'abc#gmail.com', 'sa' , '' ,'123456789','hasghsdfhgfhgfhdgfhdsgsh');
SELECT * FROM `um.user` WHERE email='ab1#ansys.com';
It does nothing.
It doesn't insert data into table, I figured out the issue .
The isssue is in parameter "Email".
But when I change the parameter "Email" to "Email12" , it worked as expected.
But I don't want to change in parameter as it will be a change in my API as well,
Now i want to solve this issue in sp level as well, I have tried below changes as well in SP which also doesn't works,
Set #userEmail=Email;
IF NOT EXISTS( SELECT user_id FROM `um.user` WHERE `email`=#userEmail)THEN
Any suggestions
Regards
Sangeetha
You may also qualify their identifiers. See 9.2.1 Identifier Qualifiers.
...
/*
-- You can also use Alias
SELECT `user_id`
FROM `um.user` `uu`
WHERE `uu`.`email` = `Email`
*/
IF NOT EXISTS (SELECT `user_id`
FROM `um.user`
WHERE `um.user`.`email` = `Email`) THEN
...
SQL Fiddle demo unqualified
SQL Fiddle demo qualified
Try this if you want to handle your issue within the stored procedure itself, without changing the name of parameter,
DELIMITER $$
DROP PROCEDURE IF EXISTS `CreateUser1`$$
CREATE PROCEDURE `CreateUser1`(
IN Email VARCHAR(50),
IN Password1 VARCHAR(50),
IN FirstName VARCHAR(50),
IN LastName VARCHAR(50),
IN AlternateEmail VARCHAR(50),
IN PhoneNumber VARCHAR(50),
IN Token VARCHAR(500)
)
BEGIN
DECLARE Email1 VARCHAR(50);
SET Email1 = Email;
IF NOT EXISTS( SELECT user_id FROM `um.user` WHERE `email`=Email1)THEN
INSERT INTO `um.user`(site_id,email,PASSWORD,alternate_email,first_name,last_name,contact_number,
created_on,updated_on,is_active,token,is_verified_email)
VALUES
(1, Email1 , Password1 ,AlternateEmail, FirstName , LastName ,PhoneNumber,UTC_TIMESTAMP(),UTC_TIMESTAMP(),1,Token,0);
END IF;
END$$
DELIMITER ;
This is because in MySQL, the parameter name passed to a Stored Procedure should not be the same as Column Name. Hope this solves your problem :)

MySQL - Select INTO variable not working as desired

I am facing a problem with the select INTO clause-
CREATE PROCEDURE `sp_user_signup`(
IN gender int,
IN dob varchar(50),
IN location int,
IN email varchar(100),
IN password varchar(50),
IN verify_uuid varchar(256),
IN fb_user bool
)
BEGIN
DECLARE User_ID INT DEFAULT 0;
DECLARE ENABLED BOOL DEFAULT TRUE;
SELECT user_id INTO User_ID FROM tbl_user_login WHERE email = email;
CALL sp_debug(ENABLED,1,User_ID,(select concat_ws('',"UserID:", User_ID)));
IF User_ID = 0 THEN
<do something>
ELSE
<do something>
END IF
END
My tbl_user_login has values for email - v.psk#gmail.com.
Even though I pass the email v.psk#gmail.com to SP it always goes into IF BLOCK.
The values of User_ID is zero from the debugs.
Thanks in advance.
This is not doing what you expect:
SELECT user_id INTO User_ID FROM tbl_user_login WHERE email = email;
The problem is that you think one email is a variable and the other a column. SQL can't read your mind though, so both are the column.
Prefix your variables with something to distinguish them from columns. For example:
CREATE PROCEDURE `sp_user_signup`(
IN v_gender int,
IN v_dob varchar(50),
IN v_location int,
IN v_email varchar(100),
IN v_password varchar(50),
IN v_verify_uuid varchar(256),
IN v_fb_user bool
)
BEGIN
DECLARE v_User_ID INT DEFAULT 0;
DECLARE ENABLED BOOL DEFAULT TRUE;
SELECT user_id INTO v_User_ID FROM tbl_user_login WHERE email = v_email;
CALL sp_debug(ENABLED,1,v_User_ID,(select concat_ws('',"UserID:", v_User_ID)));
IF v_User_ID = 0 THEN
<do something>
ELSE
<do something>
END IF
END