MySql set variable using Select - mysql

I have a stored procedure as
DELIMITER $$
CREATE PROCEDURE GetInfo(
in userid varchar (150),
out fname varchar (50),
out lname varchar (50),
out user_status varchar (30),
out reg_date DATETIME
)
BEGIN
select fname=users_table.first_name,
lname=users_table.last_name,
user_status=users_table.user_status,
reg_date=users_table.reg_date
FROM users_table
WHERE users_table.userid=userid;
END$$
I am using it like
SET #fname=#lname=#user_status=#reg_date=NULL;
CALL GetInfo(
'XYZ',
#fname,
#lname,
#user_status,
#reg_date
);
SELECT #fname,#lname,#user_status,#reg_date;
But I am getting only NULL values. I think that problem is with variable assignment using Select statement.
I know that in MSSQLSERVER, we can assign variable in SELECT statement as I have but I am not sure about MySQL.

You must change the SQL in you procedure:
DELIMITER $$
CREATE PROCEDURE GetInfo(
in userid varchar (150),
out fname varchar (50),
out lname varchar (50),
out p_user_status varchar (30),
out p_reg_date DATETIME
)
BEGIN
select users_table.first_name,
users_table.last_name,
users_table.user_status,
users_table.reg_date
into fname,lname,p_user_status,p_reg_date
FROM users_table
WHERE users_table.userid=userid
limit 1;
END$$
Variable names must be different than fields
And will work

Related

Delimiter Trouble

Fairly new to databases and am using mysql 8.0. I am trying to change my delimiter for a stored procedure but it keeps telling me that create is expected. Not sure why. Any suggestions would be greatly appreciated.
This is my code for the procedure.
DELIMITER //
CREATE PROCEDURE CustomerInfo
(
CustomerFirstName CHAR (20),
CustomerLastName CHAR (20),
CustomerAddress CHAR (100),
CustomerState CHAR (20),
CustomerZip INT(5),
CustomerPhoneNumber CHAR (15),
CustomerEmailAddress CHAR (50)
)
BEGIN
INSERT INTO Customers
(CustomerFirstName,CustomerLastName,CustomerAddress,CustomerState,CustomerZip,CustomerPhoneNumber,CustomerEmailAddress)
VALUES
(CustomerFirstName, CustomerLastName, CustomerAddress, CustomerState, CustomerZip, CustomerPhoneNumber, CustomerEmailAddress);
INSERT INTO Invoice
(CustomerFirstName,CustomerLastName,CustomerAddress,CustomerState,CustomerZip,CustomerPhoneNumber,CustomerEmailAddress)
VALUES
(CustomerFirstName, CustomerLastName, CustomerAddress, CustomerState, CustomerZip, CustomerPhoneNumber, CustomerEmailAddress);
END //
DELIMITER ;

mysql insert bigint variable into tinyint type column

I've got tables users and devices with a user_id columns which are tinyint(3).
I have to create a procedure which will add new user and update these tables with user_id. So I set the delimiter to // and make this:
create procedure add_user (in pr_name varchar(15), in pr_surname varchar(15), in pr_address varchar(15), in pr_pesel float(11,0), in pr_tel int (9), in pr_package_id tinyint(3) unsigned, in pr_mac varchar(17), in pr_model varchar(15), in pr_ip int(10) unsigned)
begin
set #date = curdate();
insert into users values (null, pr_name, pr_surname, pr_address, pr_pesel, pr_tel, pr_package_id);
set #id = last_insert_id()
insert into devices (pr_mac, pr_ip, #id, pr_model, #date);
end;//
I want to take last added user_id from users (which is first column in users and is also auto-increment) and put it into user_id in devices table. Mysql returns an syntax error near 'null, #id, pr_model, #date); end'.
I think it's because last_insert_id() function returns bigint unsigned.
Please help me with this.

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

ERROR 1064 (42000): You have an error in your SQL syntax;

I am getting the following error at command line when creating this new sproc and I can't seem to figure out what's causing it. All column names have been verified in the table along with their data types to be correct.
Error:
ERROR 1064 (42000): 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 'CREATE PROCEDURE sproc_CreateFullRewardCardPass (
IN MerchantNo INT (11),
IN Me' at line 2
here is the script:
DELIMITER //
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass
CREATE PROCEDURE sproc_CreateFullRewardCardPass (
IN MerchantNo INT (11),
IN MerchantStoreName VARCHAR (200),
IN RewardCardType VARCHAR (20),
IN RewardCardId BIGINT (20),
IN RewardCardTitle VARCHAR (50),
IN RewardCardDescription VARCHAR (100),
IN MerchantCharity VARCHAR (200),
IN MerchantDonation DOUBLE,
IN RewardCardExpDate DATE,
IN RewardCardreateDate DATE,
IN RewardCardStatus INT (1),
IN RewardCardPassHeader VARCHAR (50)
);
BEGIN
DECLARE RCpasstypeidenitifier TEXT;
DECLARE RCpassformatversion INT (2);
DECLARE RCpassteamidentifier VARCHAR (30);
DECLARE RCpassserialnumber VARCHAR (100);
DECLARE RCpassbackgroundcolor VARCHAR (16);
DECLARE RCpassvisual_1xicon TEXT;
DECLARE RCpassvisual_2xicon TEXT;
DECLARE RCpassvisual_logo TEXT;
DECLARE RCpasssecondary_label1 VARCHAR (50);
DECLARE RCpasssecondary_value1 VARCHAR (50);
DECLARE RCpassaux_label1 VARCHAR (50);
DECLARE RCpassbarcode_type VARCHAR (20);
DECLARE RCpassbarcode_encoding VARCHAR (200);
DECLARE RCpassback_value1 TEXT;
DECLARE RewardCardTitle TEXT;
SET RCpasssecondary_value1 = 'expires';
SET RCpassbarcode_type = 'PKBarcodeFormatQR';
SET RCpassbarcode_encoding = 'iso-8859-1';
SET RCpassback_value1 = 'RCpassback_value1';
/* CREATE FULL REWARD CARD PASS */
INSERT INTO `mcp_passes_merchants` (
passtype,
`status`,
merchantid,
rewardid,
merchantname,
meta_description,
passtypeidentifier,
formatversion,
teamidentifier,
serialnumber,
backgroundcolor,
visual_1xicon,
visual_2xicon,
visual_logo,
visual_text,
meta_expdate,
primary_label,
primary_value,
secondary_label1,
secondary_value1,
aux_label1,
aux_value1,
barcode_value,
barcode_text,
barcode_type,
barcode_encoding,
datecreated,
nonprofit,
donation,
back_label1,
back_value1);
VALUES (
RewardCardType,
RewardCardStatus,
MerchantNo,
RewardCardId,
MerchantStoreName,
RewardCardTitle,
RCpasstypeidenitifier,
RCpassformatversion,
RCpassteamidentifier,
RCpassserialnumber,
RCpassbackgroundcolor,
RCpassvisual_1xicon,
RCpassvisual_2xicon,
RCpassvisual_logo,
MerchantStoreName,
RewardCardExpDate,
RewardCardPassHeader,
RewardCardTitle,
RCpasssecondary_label1,
RCpasssecondary_value1,
RCpassaux_label1,
RewardCardId,
RewardCardId,
RewardCardId,
RCpassbarcode_type,
RCpassbarcode_encoding,
RewardCardCreateDate,
MerchantCharity,
MerchantDonation,
RewardCardDescription,
RCpassback_value1);
END//
DELIMITER ;
You need to end every statement with a delimiter. In your case it is //. Add this after the drop statement
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass //
here -------------------------^
here is how I fixed it. It turns out I had extra ';' after CREATE PROCEDURE statement.
DELIMITER $$
DROP PROCEDURE IF EXISTS sproc_CreateFullRewardCardPass $$
CREATE PROCEDURE sproc_CreateFullRewardCardPass (IN MerchantNo INT (11),IN MerchantStoreName VARCHAR (200), IN RewardCardId BIGINT (20), IN RewardCardTitle VARCHAR (50), IN RewardCardDescription VARCHAR (100), IN MerchantCharity VARCHAR (200), IN MerchantDonation DOUBLE, IN RewardCardExpDate DATE, IN RewardCardreateDate DATE, IN RewardCardStatus INT (1),IN RewardCardPassHeader VARCHAR (50) )
BEGIN
DECLARE RCpasstypeidenitifier TEXT;
DECLARE RCpassformatversion INT (2);
DECLARE RCpassteamidentifier VARCHAR (30);
DECLARE RCpassserialnumber VARCHAR (100);
DECLARE RCpassbackgroundcolor VARCHAR (16);
DECLARE RCpassvisual_1xicon TEXT;
DECLARE RCpassvisual_2xicon TEXT;
DECLARE RCpassvisual_logo TEXT;
DECLARE RCpasssecondary_label1 VARCHAR (50);
DECLARE RCpasssecondary_value1 VARCHAR (50) default 'expires' ;
DECLARE RCpassaux_label1 VARCHAR (50);
DECLARE RCpassbarcode_type VARCHAR (20) default 'PKBarcodeFormatQR' ;
DECLARE RCpassbarcode_encoding VARCHAR (200) default 'iso-8859-1' ;
DECLARE RCpassback_value1 TEXT default 'RCpassback_value1' ;
DECLARE RewardCardType VARCHAR (20) default 'full' ;
/* CREATE FULL REWARD CARD PASS */
INSERT INTO `tablename` (passtype, `status`, merchantid, rewardid, merchantname, meta_description, passtypeidentifier, formatversion,
teamidentifier, serialnumber, backgroundcolor, visual_1xicon, visual_2xicon, visual_logo, visual_text, meta_expdate, primary_label, primary_value,
secondary_label1, secondary_value1, aux_label1, aux_value1, barcode_value, barcode_text, barcode_type, barcode_encoding, datecreated, nonprofit,
donation, back_label1, back_value1) VALUES (RewardCardType, RewardCardStatus, MerchantNo, RewardCardId, MerchantStoreName, RewardCardTitle, RCpasstypeidenitifier, RCpassformatversion, RCpassteamidentifier, RCpassserialnumber, RCpassbackgroundcolor, RCpassvisual_1xicon, RCpassvisual_2xicon, RCpassvisual_logo, MerchantStoreName, RewardCardExpDate, RewardCardPassHeader, RewardCardTitle,
RCpasssecondary_label1, RCpasssecondary_value1, RCpassaux_label1, RewardCardId, RewardCardId, RewardCardId, RCpassbarcode_type,
RCpassbarcode_encoding, RewardCardCreateDate, MerchantCharity, MerchantDonation, RewardCardDescription, RCpassback_value1);
END $$
DELIMITER ;;