Compare string with select statement mysql - mysql

i have to make a function in mysql that returns 0 or 1.
DELIMITER //
drop function compruebacuenta//
CREATE FUNCTION compruebacuenta(ncliente INT) RETURNS INT
BEGIN
DECLARE fin int default 0;
DECLARE cuenta varchar(30);
DECLARE cliente varchar(30);
DECLARE cuentacliente varchar(30);
select codigo_cliente FROM clientes where codigo_cliente=ncliente into cuentacliente;
RETURN fin;
END;
//
user gives a number when calling the function i.e select function(1);
what i do then is get the cod for a client with that given code (I know a bit redundant , but whatever); and store it in a variable called cuentacliente;
What i need to do now is get a query like this one
select cod_cliente from cuentas;
store that in another and I want to know how can I compare lets say a 10 , to see if there's a 10 in the resulting query.
I'm thinking that perhaps i need a cursor that fetches each row and compare that with what's in cuentacliente.

Related

Stored Procedure - create a cursor - accept an OUT parameter

I was hoping to get some help the the below question, unfortunately the script I have created isn't working. Any assistance would be greatly appreciated!
Question:
Write a script that creates a stored procedure named test. This stored procedure should create a cursor for a result set that consists of the product_name and list_price columns for each product with a list price that’s greater than $700. The rows in this result set should be sorted in descending sequence by list price. The stored procedure should accept an OUT parameter where a message is passed out of the procedure. Then, the procedure should set the out parameter to a string variable that includes the product_name and list price for each product so it looks something like this:
Gibson SG,2517.00|Gibson Les Paul,1199.00|
Here, each value is enclosed in asterisk(*), each column is separated by a comma (,) and each row is separated by a pipe character (|).
My script:
CREATE PROCEDURE test( OUT message VARCHAR(200) )
BEGIN
DECLARE product_name_var VARCHAR(50);
DECLARE list_price_var DECIMAL(9,2);
DECLARE row_not_found TINYINT DEFAULT FALSE;
DECLARE s_var VARCHAR(400) DEFAULT '';
DECLARE invoice_cursor CURSOR for
SELECT
product_name,
list_price
FROM
products
WHERE
list_price > 700
ORDER BY list_price DESC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET row_not_found = TRUE;
OPEN invoice_cursor;
FETCH invoice_cursor INTO product_name_var, list_price_var;
WHILE row_not_found = FALSE DO
SET s_var = CONCAT(s_var,'*', product_name_var,'*,*',list_price_var,'*|');
FETCH invoice_cursor INTO product_name_var, list_price_var;
END WHILE;
SELECT s_var AS message;
END
SELECT s_var AS message; - message is seen as an alias
use
SET MESSAGE = s_var ;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c43887fd68a17e5b1dc2093d10cd03ec
and beware nulls...

MySQL: Function not returning the correct integer

We have a question regarding a function returning the wrong integer-value in MySQL. We have checked that "booked_passengers" contains the right value, 0, and it works just fine when removing that variable, meaning just returning the integer 40. But as soon as we try to subtract "booked_passengers" from it, which still should end up returning 40, it does not work.
Including the code below.
Thanks in advance! :-)
CREATE FUNCTION calculateFreeSeats(flightnumber INT)
RETURNS INT
NOT DETERMINISTIC
BEGIN
DECLARE booked_passengers INT;
SELECT BOOKED_PASSENGERS INTO booked_passengers FROM FLIGHT WHERE (flightnumber = NR);
RETURN (40-booked_passengers);
END $$
When column name and local variable name interfere and there is no table alias then the variable is preferred. So your SELECT BOOKED_PASSENGERS ... selects variable value, not column value. Use
CREATE FUNCTION calculateFreeSeats(flightnumber INT)
RETURNS INT
READS SQL DATA
BEGIN
DECLARE booked_passengers INT;
SELECT FLIGHT.BOOKED_PASSENGERS INTO booked_passengers FROM FLIGHT WHERE (flightnumber = NR);
RETURN (40-booked_passengers);
END $$
From the other side the variable usage is obviously excess:
CREATE FUNCTION calculateFreeSeats(flightnumber INT)
RETURNS INT
READS SQL DATA
RETURN (SELECT 40 - BOOKED_PASSENGERS FROM FLIGHT WHERE flightnumber = NR LIMIT 1);

Call a stored function in mysql,But get an error:Subquery returns more than 1 row

In mysql database,I have a table called 'usertest' which has some user information stored in it. And I create a UDF called 'getId' successfully. This is how it created:
create function getId(name varchar(255))
returns int
begin
declare id_found int;
set id_found=(select Id from usertest where Name=name);
return id_found;
end
It should be OK.However,when I call
select getId('mike');
mysql engine reports an error:
Subquery returns more than 1 row.
But in table usertest I have only one row where Name='mike'.That's wired.Someone can tell me why? Thank you in advance :)
Column names and variables are case-insensitive, so Name and name are the same thing. It's not comparing the Name column with the name variable, it's comparing the name variable with itself, so it's always true.
Use a different name for the function parameter.
create function getId(name_param varchar(255))
returns int
begin
declare id_found int;
set id_found=(select Id from usertest where Name=name_param);
return id_found;
end
or use a table name prefix.
create function getId(name varchar(255))
returns int
begin
declare id_found int;
set id_found=(select Id from usertest where usertest.Name=name);
return id_found;
end
See MySQL : When stored procedure parameter name is the same as table column name

Table Parameter in MySQL Function

Basically I am trying to refer to my table in mysql function, so that in my query I can say "from x" as in x is a parameter of the function, so that someone can put in the table they want the function to run on.
CREATE DEFINER=`root`#`localhost` FUNCTION `somefunction`(t varchar(8), num integer) RETURNS int(8)
BEGIN
DECLARE result integer(12);
DECLARE test varchar(12);
SET result = 0;
SET test = t;
select integer * 5 INTO result from x;
return result;
END
Basically when I do somefunction(thisisthetableiwant, 5) I get an error saying that it cannot find 'test in field list' so it isn't setting the table to what I put in the parameter, currently I have the part "from x" hardcoded with the table I want and it works but I need to make it so I can have a parameter incase I need to use the function on another table

MySQL stored function SELECT INTO unexpected results

I am trying to write a stored function in mysql 5.1 that returns the value 'AccountIDref' for a given room. If I only query the inner SELECT statement this works (returns the value for room). But invoking the function I get the response:
'#1172 - Result consisted of more than one row'
CREATE FUNCTION getAccountId (room INT) RETURNS INT
BEGIN
DECLARE refID INT DEFAULT NULL;
SELECT AccountIDref INTO refID FROM Allocation
WHERE Room = room;
RETURN refID;
END
What am I doing wrong here?
Field name and parameter name must be different -
CREATE FUNCTION getAccountId (room_param INT) RETURNS INT
BEGIN
DECLARE refID INT DEFAULT NULL;
SELECT AccountIDref INTO refID FROM Allocation
WHERE Room = room_param;
RETURN refID;
END
In your function you were getting all tables records.
What I am going to suggest isn't going to be much different from what you have, but I am skeptical about the where clause being in the next line and also let's use limit 1 to explicitly set the limit.
Try this :
CREATE FUNCTION getAccountId (room INT) RETURNS INT
BEGIN
DECLARE refID INT DEFAULT NULL;
SELECT AccountIDref INTO refID FROM Allocation WHERE Room = room LIMIT 1;
RETURN refID;
END