MySQL stored procedure returns wrong values - mysql

Im new to MySQL stored procedures and I was following some tutorial on how to use them, but I ran into an interesting thing with the following:
DELIMITER $$
CREATE DEFINER=`user`#`%` PROCEDURE `CalculateScores`(IN ID INT, OUT test INT)
BEGIN
SELECT COUNT(*)
INTO test
FROM myTable
WHERE id = ID;
END$$
DELIMITER ;
I run it with this:
CALL CalculateScores(252, #test);
and then just:
SELECT #test;
The strange thing is that #test returns the total row count of the entire table not just for the id I sent as a parameter.
What am I missing here? The tutorial never mention this, and I can't find an answer to why this is happening, I might suck at searching..

It looks like MySQL cannot differentiate between id and ID:
SELECT COUNT(*)
INTO test
FROM myTable
WHERE id = ID;
And it treats it like 1 = 1 which is always true (if column is not nullable).
You could add alias to indicate that id is column and not parameter.
CREATE PROCEDURE `CalculateScores`(IN ID INT, OUT test INT)
BEGIN
SELECT COUNT(*)
INTO test
FROM myTable t
WHERE t.id = ID;
END
db<>fiddle demo

Related

MySQL Get row data from Update query with LIMIT via stored procedure

I have a query which updates multiple rows in a table with a variable LIMIT. I need to get data from the updated rows so I know which exact rows got affected. I wrote this simple procedure:
DELIMITER $$
CREATE PROCEDURE select_update(IN myId INT, IN myAttr VARCHAR(10), IN myAmount MEDIUMINT)
begin
SELECT data FROM mytable WHERE id IS NULL AND attr = myAttr LIMIT myAmount;
UPDATE mytable SET id = myId WHERE id IS NULL AND attr = myAttr LIMIT myAmount;
end$$
DELIMITER ;
Will this SELECT statement always return the exact same rows that the UPDATE statement affects? Is it possible for another user to execute a query while this procedure is running and thus to possibly change the affected rows between the SELECT and UPDATE?
Create a temporary table to hold the primary keys of the rows to be updated.
CREATE PROCEDURE select_update(IN myId INT, IN myAttr VARCHAR(10), IN myAmount MEDIUMINT)
begin
CREATE TEMPORARY TABLE temp_mytable AS
SELECT pk FROM mytable WHERE id IS NULL AND attr = myAttr LIMIT myamnt;
UPDATE mytable JOIN temp_mytable USING (pk)
SET mytable.id = myId;
SELECT mytable.data
FROM mytable JOIN temp_mytable USING (pk);
end$$

Selecting all values from a table in mysql using a stored procedure

I have a table called Contacts with a field called person_id that I have connected to a java application.
If no value is specified for person_id in the application, I want to select everything from the contacts table using a stored procedure.
The operation I want to perform is this:
Select * from Contacts where (person_id like "%")
For this I have written a stored procedure shown below:
Delimiter $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectTest2`(In p_id int(11))
BEGIN
if p_id = null then
set p_id = "%";
end if;
select * from Contacts where (person_id like p_id);
END $$
Delimiter ;
However when I run this procedure in my sql using the following
call selectTest2(null)
The table that is returned is blank. How do I make it show all the values in the table?
The parameter p_id gets its value from a text box in the application. If the user has entered an id, I want the procedure to show only that particular record else I want it to show all records.
What have I done wrong and how do I correct it? I am aware that p_id is an int however I tried the same thing with other fields of type varchar and the table failed to return any value.
Try using case statement in where clause like below
WHERE CASE WHEN p_id IS NOT NULL THEN person_id = p_id ELSE TRUE END
Hope this should solve your problem

Using where clause on a view from a Mysql Stored procedure yields no results, but a table does

It appears that performing a where clause on a table with Mysql works as expected (first code example).
However, when a where clause is performed on a view, the where clause appears to be ignored.
To keep things simple, I've tested a simple where clause on a standard table- this performs as expected, additionally a 'proof test' was ran to see if a mysql procedure can do a simple select * on a view (it did). Thus, it's only when the where clause is added to quiers on views.
How can I perform a WHERE query within a stored procedure on a View?
Example code 1 (working with normal table):
DELIMITER //
CREATE PROCEDURE p1 (id INT, fname VARCHAR(250))
BEGIN
DECLARE staffId INT;
DECLARE name VARCHAR(250);
SET name = fname;
SET staffId = id;
SELECT * FROM Staff WHERE ID_Staff = staffId AND Fname = name;
END; //
DELIMITER
Result set returned from the above performs as expected, acting upon the where clause.
However, with when using a where clause on a view the where clause is ignored:
Example code (not working as expected)
DELIMITER //
CREATE PROCEDURE p2 (year INT)
BEGIN
DECLARE a CHAR(4);
SET a = year;
SELECT * FROM TotalHoursView WHERE Year = "2014";
END; //
DELIMITER ;
Note: 'TotalHoursView' is a view, not a table.
The result of the second procedure ignores the where clause, always returning the full contents of the view (SELECT * FROM TotalHoursView).
Does Mysql not support where clauses on views when called in a stored procedure?
http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.pdf
Try to use another names for your variables, it's not CASE sensitive

MySQL stored procedure: OUT parameter not being set

I've got a stored procedure in MySQL that gets the next unique ID from a table, to use as an ID for 2 other tables (not the best way to do it, I'm sure, but I'm modifying someone else's code here). The procedure is as follows:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `GetNextID`( OUT id bigint )
BEGIN
DECLARE uid VARCHAR(255);
SET uid = uuid();
INSERT INTO `ident_column_generator` (u) VALUES (uid);
SELECT ID INTO id FROM `ident_column_generator` WHERE u = uid;
DELETE FROM `ident_column_generator` WHERE u = uid;
END$$
When I call the procedure from MySQL Workbench:
CALL GetNextID( #id );
SELECT #id;
#id is NULL. I can't work out what's going wrong? Even if I run SET #id = 0; before calling the procedure, it ends up as NULL afterwards. If I call the functions within the procedure manually from MySQL Workbench, #id outputs fine, e.g.:
SET #uid = uuid();
INSERT INTO `ident_column_generator` (u) VALUES (#uid);
SELECT ID INTO #id FROM `ident_column_generator` WHERE u = #uid;
DELETE FROM `ident_column_generator` WHERE u = #uid;
SELECT #id;
This outputs #id as being a valid number.
Any ideas why id isn't being set properly?
Typically, spent 3 hours on this, then JUST after I posted the question I find the problem. So, for future reference: It appears MySQL is case insensitive where variables are concerned. The ID column name and id variable apparently completely confused it.
I changed the procedure's input parameter name to retId and then it worked perfectly.
Thanks Nick, I also had same issue. The column name and variable name were same due to which we were getting the issue.

how to create procedure in mysql for selecting record?

how to write procedure in mysql to select record from particular table with IN and OUT parameter?
delimiter //
create procedure sample (in id int, out MyCount int)
begin
select count(*) into MyCount
from YourTable
where YourKey = id;
end//