This Stored Procedure should be returning a false if the value passed in the parameter does not exist in the table.
CREATE DEFINER=`listsjag_user`#`%` PROCEDURE `Select_ReferenceAvailability`(IN ref VARCHAR(45))
BEGIN
IF EXISTS (
SELECT * FROM tbl_dataLibrary
WHERE itemreference = #ref
) THEN
SELECT 'true';
ELSE
SELECT 'false';
END IF;
END
It is returning true for all values but there is only one value in the column itemreference.
Does anyone have any ideas?
A MySQL stored procedure cannot really return a value. You can either pass in an OUT parameter and modify it, or convert your procedure to a function.
CREATE FUNCTION ReferenceAvailability (IN ref VARCHAR(45))
RETURNS boolean
BEGIN
IF(EXISTS(SELECT * FROM tbl_dataLibrary WHERE itemreference = #ref)) THEN
RETURN 1;
END IF;
RETURN 0;
END;
I changed your true/false to 1/0, feel free to change back, but you will need to return varchar(5).
Use the function in a select like this:
SELECT ReferenceAvailability(...)
Hope that helps.
Related
I'm trying to call a stored procedure from another stored procedure and store the value in a variable. The inner stored procedure basically checks if something exists and uses a select statement to return a zero or one. I keep getting an error. In this situation, MySQL is saying "=" is not valid at this position, expecting ";"
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
DECLARE exist TINYINT;
EXECUTE exist = CardNames_CheckExist searchedCard
IF (exist = 0)
INSERT INTO card_names (name)
VALUE(searchedCard)
END
You have to rewrite you other stored procedure, that you don't need btw, to give back a result
CREATE PROCEDURE CardNames_CheckExist (IN searchedCard VARCHAR(50), OUT result TINYINT )
BEGIN
--do some stuzff
result = 1
END
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
CALL CardNames_CheckExist(searchedCard,#result);
IF (#result = 0) THEN
INSERT INTO card_names (name)
VALUES (searchedCard);
END IF;
END
I have a table named 'reserves' and the exercise asks me to make a function that counts how many reservations are with an id lower than the id I give as the only parameter of the function. They also ask me that when I introduce 'NULL' as a parameter, the function must make a sum of all of the imports from the column 'import' and show the total. I have half of the function working. The 'NULL' part is the one not working.
Now I have this:
CREATE OR REPLACE FUNCTION compta_reserves(int) RETURNS int AS $$
DECLARE
num_reserves INT:=(SELECT COUNT(*) FROM mobilitat.reserves);
id ALIAS FOR $1;
comptador INT:=0;
i INT:=1;
import_total double precision:=((SELECT SUM(import) FROM mobilitat.reserves WHERE import<60)::INT);
BEGIN
IF (id=NULL) THEN
RETURN import_total AS int;
ELSE
WHILE i<num_reserves LOOP
IF (SELECT id_reserva FROM mobilitat.reserves WHERE id_reserva=i)<id THEN
IF (SELECT import FROM mobilitat.reserves WHERE id_reserva=i)<60 THEN
comptador=comptador+1;
END IF;
END IF;
i=i+1;
END LOOP;
RETURN comptador;
END IF;
END;
$$ LANGUAGE PLPGSQL;
When I run the function with the 'NULL' parameter, I just get an empty table with no sum.
I was told to create a mysql function that, given the artwork title, return the name of the creator. I know it sounds silly, i could use just a select.
I followed some tutorials, but none of them helped me. When I try to create the function, mysql workbench says "Error Code: 1415. Not allowed to return a result set from a function"
Here is what i'm trying to do
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
SELECT result= ar.artistName FROM artist as ar, art_object as oa
where ar.artistCode= oa.artistCode
and oa.title= workName
group by ar.artistName ;
return result;
end$$
delimiter ;
That's not how you assign the result of a variable in a SELECT statement. You have to use SELECT ... INTO variable.
You also need to declare the variable first.
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
DECLARE result VARCHAR(250);
SELECT DISTINCT ar.artistName FROM artist as ar, art_object as oa
INTO result
where ar.artistCode= oa.artistCode
and oa.title= workName;
return result;
end$$
delimiter ;
SELECT result = ar.artistName is comparing the value of result with ar.artistName, and returning the result of that comparison from the SELECT statement.
I need to make a stored function:
This is my code
SELECT count(Dominio) FROM Thogar WHERE DOMINIO='%'
I need to make a stored function where I will write a letter between (U,C,R) and the function will replace the % in the previous code with the selected letter.
How can I do it? Thanks!
Got it working
CREATE FUNCTION `Buscar`(`param` CHAR(1))
RETURNS INT
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE res INT;
SELECT count(Dominio) INTO res FROM Thogar WHERE DOMINIO=param;
RETURN res;
END
Call buscar('C')
This should work:
DROP FUNCTION IF EXISTS myFunc;
DELIMITER $$
CREATE FUNCTION myFunc(
param CHAR(1)
) RETURNS INT;
BEGIN
DECLARE res INT;
SELECT count(Dominio) INTO res FROM Thogar WHERE DOMINIO=param;
RETURN res;
END;
$$
DELIMITER ;
If You want to make stored function with only one sql query,
I don't see any normal reason for it.
It will not give You performance gain.
How about simplification?
You can create view:
CREATE VIEW v_dominio_counters AS
SELECT Dominio, count(Dominio) AS counter FROM Thogar GROUP BY Dominio
And then use it:
SELECT counter FROM v_dominio_counters WHERE Dominio = 'U' LIMIT 1;
It will always keep for You ready to use counters that is handy when You have huge table.
I am curious how to reference an existing stored procedure SELECT statement from a secondary query or SET call within the same stored procedure.
For example:
CREATE PROCEDURE 'mysp' (OUT sumvalue INT)
BEGIN
-- This is the core recordset the SP returns
SELECT * FROM Table
-- I also want to return a value based on the above recordset
SET sumvale = SUM(previousselect.values)
END
Essentially, I have a SP that returns a detailed recordset, and I need to return SUM and custom values based on the data within that recordset. The issue is I cannot figure out how to reference the data after the SELECT statement (e.g. does it create an internal reference I can use such as #recordset1.X).
Any help would be appreciated.
Try using cursor from this link:
As MySql does not allow you to return a recordset from either store procedures or functions, you could try this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `some_procedure`(out some_id int)
BEGIN
declare done boolean default false;
declare id int;
declare tot decimal(10,2);
declare some_cursor cursor for
select id, total from some_table where id = some_id;
declare continue handler for not found set done = true;
open some_cursor;
loop1: loop
fetch some_cursor into id, tot;
if done=true then
leave loop1;
end if;
//do your calculation here or whatever necessary you want to do with the code
end loop loop1;
END;