Add values dynamically in stored procedure - mysql

I am currently learning MySQL and trying to find out how to code dynamically for input values.
DELIMITER //
CREATE PROCEDURE add_user(
IN username VARCHAR (16),
IN pass VARCHAR (255),
IN email VARCHAR(255),
IN fname VARCHAR (32),
IN lname VARCHAR (32),
IN age INT )
BEGIN
INSERT INTO users (username, pass, email, fname, lname, age)
VALUES (#username, #pass, #email, #fname, #lname, #age);
INSERT INTO usershobbies (u_id, h_id)
VALUES (LAST_INSERT_ID(), '1');
END; //
DELIMITER ;
When I call the procedure and insert values, I get an error message:
call add_user('bugsbunny', 'Carrot1000', 'bugs#bunny.com', 'Bugs','Bunny', '80');
#1054 - Unknown column 'age' in 'field list'
I appreciate any feedback. Thank you!

This error is telling you that there is no column age in table users. You would need to check the definition of your table (which you did not provide in your question).
Also, when using in/out parameters in MySQL, you should not prefix them with #. I like to give them a string prefix (like p_) do disambiguate from other litteral strings such as column names :
CREATE PROCEDURE add_user(
IN p_username VARCHAR (16),
IN p_pass VARCHAR (255),
IN p_email VARCHAR(255),
IN p_fname VARCHAR (32),
IN p_lname VARCHAR (32),
IN p_age INT
)
BEGIN
INSERT INTO users (username, pass, email, fname, lname)
VALUES (p_username, p_pass, p_email, p_fname, p_lname);
INSERT INTO usershobbies (u_id, h_id) VALUES (LAST_INSERT_ID(), '1');
END;

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 ;

stored procedure, how to handle errorcode: 1062

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.

sql error Column name or number of supplied values does not match table definition

Create table Login_User
(
IDnum int foreign key references Contacts(ID),
Username varchar (50),
Password varchar (50),
SecretQ varchar(50),
SecretA varchar (50),
)
Create procedure sp_create
#Username varchar (50),
#Password varchar (50),
#SecretQ varchar(50),
#SecretA varchar (50)
as
insert into Login_User
Values (#Username, #Password, #SecretQ, #SecretA)
The error is when I execute the stored procedure
Msg 213, Level 16, State 1, Procedure sp_create, Line 10
Insert Error: Column name or number of supplied values does not match table definition.
Since IDnum is the foreign key column, it can't be NULL or empty, until you have the NULL values in the Contacts table ID column.
Your query can work if the IDnum is the auto increment value, other wise you have to pass the IDnum value and store into the table.
So your actual query should be:
insert into Login_User (IDnum, Username, Password, SecretQ , SecretA )
Values (#IDNum, #Username, #Password, #SecretQ, #SecretA)
Create insert command by providing all the column list
insert into Login_User (Username, password, secretq, secreta)
Values (#Username, #Password, #SecretQ, #SecretA)
You have to also include IDnum column value in insert query:
insert into Login_User (IDnum, Username, Password, SecretQ , SecretA ) Values (#IDNum,#Username, #Password, #SecretQ, #SecretA)
OR
Remove IDnum from insert query:
insert into Login_User (Username, Password, SecretQ , SecretA ) Values (#Username, #Password, #SecretQ, #SecretA)

how to create insert stored procedure mysql

I am trying to create a stored procedure and i am getting an error code.
Error Code: 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 '' at line 12
I'm not the greatest with MYSQL and I'm unsure where to even look to figure the problem out.
Create Procedure Sp_insertCustomer(
IN Customer_id VARCHAR(20) ,
IN UserName VARCHAR(20),
IN Fname VARCHAR(20),
IN Lname VARCHAR(20),
IN Dob Date,
IN Address VARCHAR(250),
IN Phone INT,
IN Email VARCHAR(250),
IN Ss VARCHAR(9) )
BEGIN
INSERT INTO Customer (Cusomter_id,UserName,Fname,Lname,Dob,Address,Phone,Email,Ss)
VALUES (in_Customer_id ,in_UserName , in_Fname , in_Lname , in_Dob , in_Address , in_Phone , in_Email , in_Ss);
END
1) Your parameters and values in insert statement are different:
Customer_id is parameter and in_Customer_id in insert statement
2) add delimeters
DELIMITER $$
< your procedure >
END$$ --- instead your END
DELIMITER ;
It seems like you missed the parameters names. Also you are not need to specify IN for params.
DELIMITER $$
Create Procedure Sp_insertCustomer(
Customer_id VARCHAR(20) ,
UserName VARCHAR(20),
Fname VARCHAR(20),
Lname VARCHAR(20),
Dob Date,
Address VARCHAR(250),
Phone INT,
Email VARCHAR(250),
Ss VARCHAR(9) )
BEGIN
INSERT INTO Customer (Cusomter_id, UserName, Fname, Lname, Dob, Address, Phone, Email, Ss)
VALUES (Customer_id, UserName, Fname, Lname, Dob, Address, Phone, Email, Ss);
END$$

MySql set variable using Select

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