Stored Proceedure that returns a boolean? - mysql

I'm new to SQL but I'm trying to minimize code duplication. I am working on a game whose sever will use MySQL C++ connector. I will also host a website that will access the same database. They both need to authenticate users. Ideally, I want to write a stored procedure that takes in a username and a hash, then returns a boolean if the name/password match.
I'm not too bad at writing queries, but stored proceedures are new territory.
Could someone give me a simple MySQL stored procedure example using a dummy table like this:
Table Player
Column PlayerID (int, primary key, not null, auto increment)
Column PlayerUsername (varchar 25, not null)
Column PlayerPassword (varchar 25, not null)
How I might create a stored procedure that takes in a name and password, and returns true if there is at least 1 record where the username and password match the parameter one.
Once I see the syntax for stored procedures I should be able to create my own. My needs are pretty basic.

Something like this
DELIMITER //
DROP FUNCTION IF EXISTS MY_FUNCTION//
create function MY_FUNCTION(name varchar(255) CHARACTER SET utf8,
pwd varchar(255) CHARACTER SET utf8, )
RETURNS tinyint(1)
READS SQL DATA SQL SECURITY INVOKER
BEGIN
declare result tinyint(1);
SELECT if(count(*)>0,1,0) INTO result
FROM users
WHERE users.name=name and users.pwd=pwd;
RETURN result;
END//
DELIMITER ;

Related

Azure Data Factory - Get SCOPE_IDENTITY value from MYSQL Stored Procedure

I'm using Lookup activity to invoke a MYSQL Stored Procedure. The requirement is to insert a record into a table, get the latest primary key and use that in another activity.
Below is the Stored Procedure
CREATE Procedure usp_LogAudit (
p_pipeline_id int,
p_status varchar(20)
)
begin
Insert into PipelineExecutionLog
(
pipeline_id,
execution_start,
execution_end,
status
)
values
(
p_pipeline_id ,
utc_timestamp(),
null,
p_status
);
SELECT LAST_INSERT_ID() AS SCOPE_IDENTITY;
end;
As, I'm using MYSQL, I don't have the facility to directly import the Stored Procedure along with it' parameters. (Note: For MS-SQL, it's straight forward)
As shown below, I'm using the Query option and invoking the MYSQL Stored Procedure using the 'call'.
Below is the Dynamic Expression
#concat('call usp_LogAudit(',variables('PipelineId'),',','''',variables('PipelineStatus'),'''',')')
The Lookup activity is calling the Stored Procedure successfully at database side. However, in ADF, it's throwing an error.
Not sure how to fix this. Any advice is appreciated.

MySQL Stored Procedure works as plain SQL

I'm trying to write a stored procedure that inserts into one table (A), then queries another table (B), then finally inserts into table (C) the last insert id, along with the result from table B. I have written a stored procedure named VetIdFromCode to do the selecting from table B, which works fine in isolation. When I run the query in isolation, subbing in value for the IN parameters then it runs fine, but when I try and save it as a stored procedure it tells me invalid SQL near 'SET #LIID...'
Many thanks for any help.
CREATE PROCEDURE `NewClientUser`(
IN `uemail` VARCHAR(60),
IN `uphash` CHAR(40),
IN `uvcode` VARCHAR(11))
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO users (user_id,user_email,user_hash,user_role)
VALUES (NULL,uemail,uphash,'1');
SET #LIID = LAST_INSERT_ID();
CALL `VetIdFromCode`(uvcode, #VID);
INSERT INTO user_vet_lookup(user_id,vet_id)
VALUES (#LIID,#VID);
You need to start the "code" of the procedure with the key word "BEGIN" and put an "END" at the end. Like:
CREATE PROCEDURE `NewClientUser`(
IN `uemail` VARCHAR(60),
IN `uphash` CHAR(40),
IN `uvcode` VARCHAR(11))
BEGIN
DETERMINISTIC
MODIFIES SQL DATA
SQL SECURITY INVOKER
INSERT INTO users (user_id,user_email,user_hash,user_role)
VALUES (NULL,uemail,uphash,'1');
SET #LIID = LAST_INSERT_ID();
CALL `VetIdFromCode`(uvcode, #VID);
INSERT INTO user_vet_lookup(user_id,vet_id)
VALUES (#LIID,#VID);
END
Check out the documentation: http://dev.mysql.com/doc/refman/5.7/en/create-procedure.html

MySQL Stored Procedure and Identifiers

I am learning to write MySQL stored procedures and I have encountered some difficulties. Here I have two stored procedures:
First stored procedure:
CREATE PROCEDURE sp1 (IN `username` TEXT, OUT `user_id` INT)
BEGIN
DECLARE rowcount INT;
SELECT count(`User ID`) INTO rowcount FROM user WHERE `Username`=username;
SET user_id = rowcount;
END|
Second stored procedure:
CREATE PROCEDURE sp2 (IN `doc_id` INT, IN `content` LONGTEXT)
BEGIN
UPDATE doc SET `Content`=content WHERE `Doc ID`=doc_id;
END|
(Delimiter is |.)
Question:
I observe that the result of the first stored procedure is the same as calling SELECT count(`User ID`) FROM user;. However, the second stored procedure does its job and gets the content updated with the new content.
So why does the first stored procedure treat `Username` and username as equal identifiers but the second stored procedure treats `Content` and content as different identifiers? The two identifiers in both cases are the same except the capitalization of the first letter.
I figure it out after reading the official MySQL documentation about the scope of local variables.
It states that:
A local variable should not have the same name as a table column. If an SQL statement, such as a SELECT ... INTO statement, contains a reference to a column and a declared local variable with the same name, MySQL currently interprets the reference as the name of a variable.

mysql where clause doesn't work

I am writing a stored procedure in mysql which simply returns the row with ID provided or return all table when no ID is provided.
CREATE DEFINER=`root`#`localhost` PROCEDURE `SLICE_GET`(`slice_id` int)
BEGIN
SELECT *
FROM `thesis_db`.`SLICE_INFO`
WHERE (SLICE_ID = `slice_id` OR `slice_id` IS NULL);
END
I have used the same idea in ms-sql for years yet it doesn't seem to work for mysql since no matter which ID is passed, the procedure returns entire table.
What am I missing here ?
This is a way to write procedures in mysql
DELIMITER $$
CREATE PROCEDURE `name of procedure` (x CHAR(1), D1 DATE, D2 DATE)
BEGIN
SELECT name of columns you want to display
FROM table name
WHERE SLICE_ID= x
OR SLICE_ID IS NULL;
END
$$
Note: Moreover mysql is not case sensitive means all caps or all small will not effect it.
delimiter is used to:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises.
By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

MySQL UUID_SHORT() gives error Out of range value for column

I'm using MySQL 5.6.17 on Amazon Web Services RDS and when calling SELECT UUID_SHORT() I'm getting a number bigger than 9223372036854775807. For example the number I get is
12057145185130250250
The problem is in my table I have a column as BIGINT(20) unsigned, but when storing a number 12057145185130250250 I get the error MySQL 22003
'MySQL 22003 Out of range value for column '' at row 1'
If I run SELECT UUID_SHORT() on our test server which is MySQL 5.6.11 (running on Windows 2008 64x) the result is as follows;
23526798209843216
I changed the column I'm trying to save my number to, as BIGINT(20) unsigned, but still get this error.
Any ideas why ?
UPDATE
Further to my initial post, I found that if I manually insert the value '12057145185130250250' using Workbench editor into the BIGINT(20) column, it saves fine.
However, if I save the value using a stored procedure like below this is when I get the problem;
DELIMITER $$
CREATE DEFINER=`testaccount`#`%` PROCEDURE `CreateCustomer`(iUserId
INTEGER, sPassword VARCHAR(75))
BEGIN
DECLARE iSalt BIGINT;
SELECT UUID_SHORT() INTO iSalt;
INSERT INTO UserCustomer
(
UserId,
Password,
Salt
)
VALUES
(
iUserId,
SHA2(CONCAT(iSalt,sPassword), 256),
iSalt
);
END
BIGINT and BIGINT UNSIGNED are not the same. All the integer data types are signed unless explicitly unsigned.
But also, UUID_SHORT() is designed to produce unique but not random, not unpredictable, and always-incrementing values, which seems like a particularly bad choice for a salt, no?
...Especially since the RANDOM_BYTES() function was introduced in 5.6.17.
http://dev.mysql.com/doc/refman/5.6/en/encryption-functions.html#function_random-bytes