Writing script creates and calls a stored procedure - mysql

Write a script that creates and calls a stored procedure named test. This stored procedure should
declare a variable and set it to the count of all products in the Products table. If the count is greater
than or equal to 7, the stored procedure should display a message that says, “The number of products
is greater than or equal to 7”. Otherwise, it should say, “The number of products is less than 7”.
DROP PROCEDURE IF EXISTS test;
CREATE procedure test()
BEGIN
DECLARE count_of_7 DECIMAL(10,2);
SELECT count(product_id)
into count_of_7
FROM products;
IF count_of_7 >= 7 THEN
SELECT 'The number of products is greater than or equal to 7' AS message;
ELSE
SELECT 'The number of products is less than 7' AS message;
end if;
call test();
21:38:55 CREATE procedure test() BEGIN DECLARE count_of_7 DECIMAL(10,2) 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 '' at line 3 0.016 sec

You need to change the delimiter before declaring your procedure. Also, you are missing an END statement. This should work:
DROP PROCEDURE IF EXISTS test;
DELIMITER //
CREATE procedure test()
BEGIN
DECLARE count_of_7 DECIMAL(10,2);
SELECT count(product_id) into count_of_7 FROM products;
IF count_of_7 >= 7 THEN
SELECT 'The number of products is greater than or equal to 7' AS message;
ELSE
SELECT 'The number of products is less than 7' AS message;
END IF;
END //
DELIMITER ;
call test();

Related

MySQL CREATE TRIGGER IF variable is greater than set value error 1064

I have been searching this site and Google for over an hour trying to figure out why I get this message:
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 '#msg = 'HIGH VALUE CUSTOMER - SALES GREATER THAN
$10K' IF (SALES > 10000) THEN' at line 5
...when running the SQL to create this trigger:
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100)
#msg = 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K'
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = #msg
ENDIF
This is for an assignment that is due by midnight tomorrow night and I'm stumped because everything I have tried is in the same syntax as the examples and I'm not trying to create a complicated trigger. Please help!?!?!
This query has so many errors, syntax, variable define, delimiter etc... Even the inner logic is not clear as well, you should read about CREATE PROCEDURE and CREATE FUNCTION Syntax first, here I only fix some syntax errors:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100) DEFAULT 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = msg;
END IF;
END;
||
DELIMITER ;
Edit:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
BEFORE INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100) DEFAULT 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
IF (NEW.SALES > 10000) THEN
SET NEW.CUSTOMERNOTES = msg;
END IF;
END;
||
DELIMITER ;
I ended up figuring it out...here's what worked for me:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE CUSTOMERNOTES VARCHAR(100);
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
END IF;
END;
||
DELIMITER ;
Thanks everyone that responded for pointing me in the right direction.

MySQL syntax error issue in creating stored procedure

I'm trying to create stored procedure but phpmyadmin saying I've syntax error in it but I can't see any error.
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 'CURSOR CUR_ID_FORUM
IS
SELECT ForumID FROM Forum' at line 3
delimiter //
CREATE PROCEDURE updateForumAdmin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
CURSOR CUR_ID_FORUM
IS
SELECT ForumID FROM Forum //
IF(
previous_role=1,
DELETE ForumManager WHERE ModuleID=3 AND ModuleEntityID=user_id AND IsDirect=0,
SELECT ForumID FROM Forum
FOR REC_ID_FORUM IN CUR_ID_FORUM
LOOP
INSERT ForumManager (ForumID,ModuleID,ModuleEntityID,ModuleRoleID,AddedBy,IsDirect) VALUES (REC_ID_FORUM,3,user_id,11,0,0)
END LOOP //
)
END //
delimiter ;
My updated code :
delimiter //
CREATE PROCEDURE updateForumAdmin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
DECLARE REC_ID_FORUM INT(11) //
DECLARE CUR_ID_FORUM CURSOR FOR SELECT ForumID FROM Forum //
IF(
previous_role=1,
DELETE ForumManager WHERE ModuleID=3 AND ModuleEntityID=user_id AND IsDirect=0,
SELECT ForumID FROM Forum
FOR REC_ID_FORUM IN CUR_ID_FORUM
LOOP
FETCH CUR_ID_FORUM INTO REC_ID_FORUM //
INSERT ForumManager (ForumID,ModuleID,ModuleEntityID,ModuleRoleID,AddedBy,IsDirect) VALUES (REC_ID_FORUM,3,user_id,11,0,0)
END LOOP //
)
END //
delimiter ;
and phpmyadmin's updated error :
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 '' at line 3
UPDATED:
Your syntax is incorrect in many places. You need to declare the REC_ID_FORUM variable to use for your cursor fetch:
DECLARE REC_ID_FORUM INTEGER //
You need to use the correct cursor syntax of:
DECLARE CUR_ID_FORUM CURSOR FOR SELECT ForumID FROM Forum //
After you declare your cursor you need to open it:
OPEN CUR_ID_FORUM //
And in your if statement you need to FETCH from the cursor in your LOOP:
FETCH CUR_ID_FORUM INTO REC_ID_FORUM //
Your IF statement syntax is also wrong. You're using the IF function that is used in MySQL select statements, not the IF conditional used in stored procedures.
Here is what I think your entire code should look like:
delimiter //
CREATE PROCEDURE updateForumAdmin (IN user_id INT, IN previous_role INT,IN new_role INT)
BEGIN
DECLARE REC_ID_FORUM INTEGER //
DECLARE CUR_ID_FORUM CURSOR FOR SELECT ForumID FROM Forum //
IF previous_role = 1 THEN
DELETE ForumManager WHERE ModuleID=3 AND ModuleEntityID=user_id AND IsDirect=0 //
ELSE
OPEN CUR_ID_FORUM //
LOOP
FETCH CUR_ID_FORUM INTO REC_ID_FORUM //
INSERT ForumManager (ForumID,ModuleID,ModuleEntityID,ModuleRoleID,AddedBy,IsDirect) VALUES (REC_ID_FORUM,3,user_id,11,0,0)
END LOOP //
END IF //
END //
delimiter ;
This is very much untested but should get you closer.

Cannot create Function MySQL syntax errors

I cannot seem to get this working properly, its always a different error I am getting. Any suggestions are appreciated at this point...
CREATE DEFINER=`db`#`localhost` FUNCTION `output_date`(in_date DATE) RETURNS DATE
READS SQL DATA
BEGIN
DECLARE date_format_index INT;
DECLARE date_format_string VARCHAR;
SELECT s.output_date_format INTO date_format_index FROM config s
SET date_format_string = ( CASE date_format_index WHEN 2 THEN '%d-%m-%Y' WHEN 3 THEN '%m-%d-%Y' ELSE '%Y-%m-%d' END );
RETURN in_date
END
I am using DELIMITER $$ when attempting to process this function.
The current error is ...
Error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; SELECT s.output_date_format INTO date_format_index FROM system_config s SET d'
My environment is MyEclipse, newest version.
The MySQL version I have is 5.2.
You are missing some semi-colons and a problem with varchar, try this:
DELIMITER $$
CREATE DEFINER=`db`#`localhost` FUNCTION `output_date`(in_date DATE) RETURNS DATE
READS SQL DATA
BEGIN
DECLARE date_format_index INT;
DECLARE date_format_string VARCHAR(100); -- obviously change the size
SELECT s.output_date_format INTO date_format_index FROM config s;
SET date_format_string = ( CASE date_format_index WHEN 2 THEN '%d-%m-%Y' WHEN 3 THEN '%m-%d-%Y' ELSE '%Y-%m-%d' END );
RETURN in_date;
END
$$
DELIMITER ;
It runs on my box.

Can't get this MySQL UDF to Create

I'm receiving a variety of error codes when I try to create this function, most recently this one: "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 '' at line 2 "
CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
RETURNS FLOAT(10,2);
BEGIN
DECLARE Total Float(10,2);
SELECT Sum(Amount) INTO Total
FROM tblFinances
WHERE email=SearchEmail AND Paid=False ;
RETURN Total;
END;
Any clues as to what I'm doing wrong?
You need to set a different delimiter during your function definition.
So something like this:
DELIMITER $$
CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
RETURNS FLOAT(10,2)
BEGIN
DECLARE Total Float(10,2);
SELECT Sum(Amount) INTO Total
FROM tblFinances
WHERE email=SearchEmail AND Paid=False ;
RETURN Total;
END$$
DELIMITER ;
Solution was a combo of needing a different delimiter and removing ; after RETURNS FLOAT(10,2)
DELIMITER $$
CREATE FUNCTION DebtOwed(SearchEmail Varchar(70))
RETURNS FLOAT(10,2)
BEGIN
DECLARE Total Float(10,2);
SELECT Sum(Amount) INTO Total
FROM tblFinances
WHERE email=SearchEmail AND Paid=False ;
RETURN Total;
END$$
DELIMITER ;
Thanks!

declare and assign value my sql stored procedure(5.0.45)

DELIMITER $$
DROP PROCEDURE IF EXISTS quotations.sp_addservices $$
CREATE PROCEDURE quotations.sp_addservices
(In categoryname varchar(25),in servicename varchar(250),in hours float,in cost float,in basis nvarchar (100))
BEGIN
insert into categorydetails (Category_Name) values (categoryname);
if(categoryname!=null)
then
DECLARE category_id int;
set category_id= select max(Category_Id) from categorydetails ;
insert into servicesdetails (Service_Name,Category_Id,Hours,Cost,Basis) values(servicename,category_id,hours,cost,basis);
end if;
END $$
DELIMITER ;
This is my stored procedure .I have to retrive the value of categoryid that is posted into the database which is auto increased.Here i cant declare the variable and assign value to variable.Am getting error like
Script line: 4 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 'DECLARE category_id int;
set category_id= select max(Category_Id) from categor' at line 9
Can any one help me
Thanks in advance.
Try
SELECT MAX(c.category_id) INTO category_id FROM categorydetails c;