Stored procedures that dont work? - mysql

I am trying to create some stored procedures in my database for my assignment and I can't work out why they don't work correctly as they run, they just don't have the desired effect.
The first is attempting to add members to my table. It runs fine but nothing is added. It just add's 0's for everything.
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddMember`(IN `iFirstname` VARCHAR(15) CHARSET utf8, IN `iLastname` VARCHAR(15) CHARSET utf8, IN `iCPR` INT(10) ZEROFILL, IN `iPhone` INT(8) ZEROFILL, IN `iAddress` VARCHAR(50) CHARSET utf8, IN `iPostcode` INT(4) UNSIGNED, IN `iDateJoined` DATE, IN `iNewsletter` BOOLEAN)
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO members (FirstName,
LastName,
CPR,
PhoneNumber,
Address,
Postcode,
DateJoined,
Newsletter)
VALUES (iFirstname,
iLastname,
iCPR,
iPhone,
iAddress,
iPostcode,
iDateJoined,
iNewsletter)
The second is deleting a member. If I run the DELETE FROM WHERE line by it's self it works fine but when its put into the stored procedure it doesn't work.
CREATE DEFINER=`root`#`localhost` PROCEDURE `DeleteMember`(IN mID INT)
BEGIN
DELETE FROM members WHERE MemberID = mID;
END
I am a bit confused as to what is wrong with them as I am new to this and finding specific answers is difficult especially as most examples are even more complex.

Hello Create an id Field in database table with auto incremented values then you will be able to store values via Stored Procedure returned above

Related

I am trying to create email data using an MySQL procedure. How can I go about not inserting into my email table if the data already exists?

Most of the posts I found involve using PHP but I am not using PHP in my case. Here is my MySQL procedure
CREATE DEFINER=`root`#`localhost` PROCEDURE `create_email`(in UID_ int(20),
in user_id_ int(20),
in from_email_ VARCHAR(1024),
in subject_email_ TINYTEXT,
in body_email_ LONGTEXT,
out id_ BIGINT(20) unsigned )
BEGIN
set id_=0;
INSERT INTO emails (UID, user_id, from_email, subject_email, body_email)
VALUES
(UID_ ,user_id_, from_email_, subject_email_, body_email_);
set id_ = LAST_INSERT_ID();
END
Currently my procedure does not check if the data in the table exists it only inserts as the data is being received. What is an effective way of making sure incoming email data does not dublicate onto my emails table?
Thank you in advance.

How to Create Stored Procedure in MySQL

My tables are
create table employee(
id int(10) auto_increment primary key,
name varchar(100),
addressId int(10)
);
go
create table address(
id varchar(10) auto_increment primary key,
name varchar(100)
);
Here is my procedure
create procedure insert_employee(IN emp_name varchar(100),IN emp_address varchar(100))
begin
DECLARE #addressId varchar(10);
SELECT #addressId:=id from address where name LIKE '%'+emp_address+'%';
IF #addressId = ''
THEN
set #addressId= 'DBS-2136';-- It will come form function
INSERT INTO address values(#addressId,emp_address);
END IF
INSERT INTO employee values(emp_name,#addressId);
END
I don't understand what is the problem. If i write this type of if condition in ms sql server there is not error. every time execute the procedure ti say error in end if. I have search in google but there is not idea about this. there is a problem in declare variable. If i copy form mysql documentation that also not work. why is that?
please help me
1. What is the proper way to declare variable under mysql stored procedure,
2. how to write if condition in mysql stored procedure.
thank you
Lots of differences between mysql and mssql. Declared variables should not include '#', all statements must be terminated, + is an arithmetic operator, if you procedure has multiple statements you must set delimiters before and after.
Further reading
How to declare a variable in MySQL?
https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
From MySQL reference Manual
An IF ... END IF block, like all other flow-control blocks used within stored programs, must be terminated with a semicolon
IF #addressId = ''
THEN
set #addressId= 'DBS-2136';-- It will come form function
INSERT INTO address values(#addressId,emp_address);
END IF;

Why won't my SQL passwordhashing-procedure run?

I am trying to create a SQL procedure that hashes password inputs. This code won't run and I am not getting any useful response errors.
The first part creates the table, the second creates the procedure. When I call on my procedure in the third part it send the values into the procedure. There the password is supposed to be hashed using SHA2_512 and inserted into the table we made eralier.
I used online research to make this code, the parts I don't get is:
The N before my values
The SetNoCount
The #responsemessage
-- makes Admin table
CREATE TABLE IF NOT EXISTS `AdminUser` (
`AdminID` smallint(6) NOT NULL AUTO_INCREMENT,
`Username` char(15) NOT NULL,
`PasswordHash` BINARY(64) NOT NULL,
`Fornavn` char(30) NOT NULL,
`Etternavn` char(40) NOT NULL,
`Email` char(40) NOT NULL,
PRIMARY KEY (`AdminID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- Makes hashing procedure
CREATE OR REPLACE PROCEDURE vm_ski.addAdmin
#pUsername NVARCHAR(50),
#pPassword NVARCHAR(50),
#pFornavn NVARCHAR(30),
#pEtternavn NVARCHAR(40),
#pEmail NVARCHAR(40),
#responseMessage NVARCHAR(250)='' OUTPUT
AS
BEGIN
SET NOCOUNT ON
BEGIN TRY
INSERT INTO vm_ski.AdminUser (Username, PasswordHash, Fornavn, Etternavn, Email)
VALUES(#pUsername, HASHBYTES('SHA2_512', #pPassword), #pFornavn, #pEtternavn, #pEmail)
SET #responseMessage='Success'
END TRY
BEGIN CATCH
SET #responseMessage=ERROR_MESSAGE()
END CATCH
END;
-- Admin example
DECLARE #responseMessage NVARCHAR(250)
EXECUTE vm_ski.addAdmin
#pUsername = N'sondre',
#pPassword = N'example'
#pFornavn = N'Sondre'
#pEtternavn = N'Morgendal'
#pEmail = N'sondre.example#gmail.com'
;
This is not a direct answer to the question; this is a security note on the methodology of the question
Do NOT hash passwords in MySQL. The data given to MySQL is plaintext, and easily intercepted by MySQL processing logs as well as possibly numerous other places before being dumped in the database (such as if message packets sent to the database are non-localhost and are non-TLS). ( Why? )
When hashing passwords you want to be doing so as early in the process as possible. This typically means using PHP password_hash and simply dumping only the hashed data in the MySQL.
If you do not use PHP to interact with your SQL then you can use other server methods such as Argon2 or Libsodium.
Also as a side point you should be using the mb4 UTF-8 charset and collations - principly utf8mb4_general_ci ( Why? )

MySQL stored procedure character encoding

I've been experiencing a rather annoying issue, when inserting data into a MySQL database table, using a stored procedure. The database, made in MySQL Workbench 6.3, has characters in tables defined as utf8, and it works perfectly well, but insertion into the tables by stored procedures, changes the character encoding, leaving wrong characters instead of the correct ones. I've been looking all over for an answer. Hope someone can help. Thanks !
delimiter $$
use lfms_browser$$
drop procedure if exists spAddMember$$
create procedure spAddMember
(
in member varchar(60) character set utf8,
in city varchar(60) character set utf8
)
begin
insert into Members
(
member,
city
)
values
(
member,
city
)
;
end$$
delimiter ;

Stored procedure to insert new column into table

I have a table:
CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`score` int(11) NOT NULL,
PRIMARY KEY (`id`)
Now i want to create a stored procedure that will insert new column into this table with the next id (which is automaticly created), name (value which I will write), and score (which i will write).
I have tryed
DELIMITER ;;
CREATE PROCEDURE insertStudent (IN ime varchar(100),IN score int(11))
BEGIN
insert into student (name,score) values (in_score,in_score);
END
;;
But it doesnt seem to work.
I know this is basic but i need help.
Are you inserting a column or a row? It looks like you're trying to insert a row, which is much more conventional.
As for "doesn't seem to work", I imagine you're getting an error from the database. What is that error? I would guess it has something to do with unknown identifiers in the query or syntax errors. Take a look at your procedure declaration:
insertStudent (IN ime varchar(100),IN score int(11))
And at your query:
insert into student (name,score) values (in_score,in_score);
What is in_score in the query? Where do you get that? You have a couple of problems here:
in_score is never defined, instead you have values called ime and score
You're trying to insert the same value into columns of two different types, that's not going to work.
There may be additional problems with your procedure declaration, I'm not familiar enough with SQL to say with certainty. But IN and int(11) look suspicious to me.
Maybe your query should be this?:
insert into student (name,score) values (ime,score);
At the very least, even if it doesn't work that may change the error message you're seeing. Which would be a step in the right direction. At that point continue to examine the error message to help debug what's wrong.
CREATE PROCEDURE insertStudent #ime varchar(100),score int
as
BEGIN
insert into student
values (#a1, #a2);
END
You forgot to put # before the parameter and you don't need to use in
and forgot to put as