mysql Stored function not working - mysql

I need to select the first flight of today. For that I'm using
delimiter//
CREATE function get_flightID(flightident())
returns int
begin
return (SELECT * FROM flightdep where depday = dayofweek(CURDATE()) ORDER BY depTime asc LIMIT 1;)
end//
delimiter;
But this isn't working. When I execute this I got an error about MySQL syntax.
17:50:50 ) end// delimiter Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') end// delimiter' at line 1 0.000 sec
I'm really guessing what to use in my first line, I tried everything I could think of.
CREATE function get_flightID(flightident())

First, you are returning select *, which would presumably have more than one column. Second, you have an unnecessary semicolon. Try something like this:
delimiter//
CREATE function get_flightID(flightident())
returns int
begin
return (SELECT deptime
FROM flightdep
where depday = dayofweek(CURDATE())
ORDER BY depTime asc
LIMIT 1
)
end//
delimiter;

You're attempting to return a resultset (that is, SELECT *...) from your function, but you declared it to return an int.
You want something like this:
DELIMITER $$
DROP FUNCTION IF EXISTS get_flightID $$
CREATE function get_flightID()
returns int
reads sql data
begin
declare result int;
SELECT flightID INTO result
FROM flightdep
where depday = dayofweek(CURDATE())
ORDER BY depTime asc LIMIT 1;
return result;
end$$
DELIMITER ;

Related

How to get value from select in trigger mysql and use it for condition of IF

DELIMITER $$
CREATE TRIGGER cek
AFTER INSERT ON lapor_karya
FOR EACH ROW
BEGIN
SET #var = (SELECT COUNT(lapor_karya.ID_Lapor) FROM lapor_karya GROUP BY NEW.ID_Karya);
IF(#var > 10)
THEN
DELETE * FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END $$
DELIMITER ;
error
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '* FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END' at line 9
I want to get value from lapor_karya table, and use this value for condition IF. If my value more than 10, then will execute query delete. But it's doesn't working
The error which you got about the DELETE statement. You have to leave * when you writing DELETE statement.
Also, just try without parentheses.
IF #var > 10 THEN
DELETE FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
For more info, check this out MySQL documentation.
A slight simplification:
BEGIN
IF ( SELECT COUNT(*) FROM lapor_karya
WHERE ??? = NEW.ID_Karya
) > 10
THEN
DELETE * FROM karya_pelajar WHERE ID_Karya=NEW.ID_Karya;
END IF;
END $$
That is, a parenthesized SELECT that returns a single value can be used virtually any place where an expression can be used.

error trying to make function for counting friends

Error: There was an error while applying the SQL script to the database.
code:
USE `my_first_db`;
DROP function IF EXISTS `show_users_firends_count`;
DELIMITER $$
USE `my_first_db`$$
CREATE FUNCTION `show_users_firends_count` (ff int)
RETURNS int(11)
BEGIN
Select
count(user_friend.user_id + user_friend.friend_id)
From
user_friend
Where
user_friend.user_id = ff;
END$$
DELIMITER ;
You had it almost right.
A functions ahs to return something so you send the count back.
You should check the query anyway. your count doesn't look right. because you add useid and freind_id and count that, that is wrong. so check the query and change it. it now count all friends_id that are connected to a user_id
DROP function IF EXISTS `show_users_friends_count`;
DELIMITER $$
CREATE FUNCTION `show_users_friends_count` (ff int)
RETURNS int
DETERMINISTIC
BEGIN
Select
count(user_friend.friend_id) INTO #count
From
user_friend
Where
user_friend.user_id = ff;
RETURN #count;
END$$
DELIMITER ;

Syntax error in sql create function

I'm getting the following error whenever i'm trying to create the following function in mysql.
error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 4
And my function is
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
RETURN result;
END
You need to change the delimiter before creating a function. If you don't your CREATE statement terminates at the first semi-colon.
Try this:
DELIMITER $$
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
END IF
RETURN result;
END$$
DELIMITER ;
Thanks to #Ravinder for catching the missing END IF.

Why won't simple If ELSE Statement work in mySql

I'm trying to create a simple stored procedure with an if else statement in SQLYog against a mySql db. I'm not overly familiar with mySql syntax so i'm hoping it's something simple but I just can't see why this isn't working
CREATE PROCEDURE p(IN Number INT)
IF NUMBER = 1 THEN
SELECT * FROM tblProduct WHERE ProductID = Number
ELSE SELECT * FROM tblProduct WHERE ProductId = 2
END IF
I'd appreciate if anyone can help me with this and tell me where i'm going wrong.
Thanks for reading.
I get the following when I try to execute:
Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ELSE SELECT * FROM tblProduct where intProductId = 2
END IF' at line 5
Statements in MySQL are delimited by semicolons. To create procedures with them, you do a little trick like so:
DELIMITER //
CREATE PROCEDURE p(IN Number INT)
BEGIN
IF NUMBER = 1 THEN
SELECT * FROM tblProduct WHERE ProductID = Number;
ELSE
SELECT * FROM tblProduct WHERE ProductId = 2;
END IF;
END //
DELIMITER ;
Check out the documentation for if/else for more info.
Remember the IF ELSE statement is always used in stored procedure, triggers not in simple select query. And ELSE OR IF keyword always write in new line not in front of query.Like below
Correct syntax:
DELIMITER //
CREATE PROCEDURE NAME(IN Number INT)
BEGIN
IF roll= 1 THEN
SELECT * FROM table1 WHERE id = roll;
ELSE
SELECT * FROM table2 WHERE id = 2;
END IF;
END //
DELIMITER ;
Wrong syntax:
DELIMITER //
CREATE PROCEDURE NAME(IN Number INT)
BEGIN
IF roll= 1 THEN SELECT * FROM table1 WHERE id = roll;
ELSE SELECT * FROM table2 WHERE id = 2;
END IF;
END //
DELIMITER ;
You need a ; after your select statements. You also need BEGIN and END around your procedure body. See the manual for lots of examples about the exact syntax for procedures.

mysql 5.1: how can i use benchmark() command to test a call to a stored procedure?

I'm trying to benchmark a stored procedure.
select benchmark(100000000,(select 1));
this benchmark works
but the following benchmark doesn't:
do benchmark(1000,(call test_login_user('a')));
it produces the following error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'call xpofb_login_user('a')))' at line 1
any ideas how to resolve the issue ?
You can't do this with benchmark(), but you could create a stored procedure to do it.
Here's an example:
delimiter $$
create procedure benchmark_test_login_user (p_username varchar(100),
p_count int unsigned)
begin
declare v_iter int unsigned;
set v_iter = 0;
while v_iter < p_count
do
call test_login_user(p_username);
set v_iter = v_iter + 1;
end while;
end $$
delimiter ;
call benchmark_test_login_user('a',1000);
You can't
http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_benchmark
Only scalar expressions can be used. Although the expression can be a subquery, it must return a single column and at most a single row. For example, BENCHMARK(10, (SELECT * FROM t)) will fail if the table t has more than one column or more than one row.