Cannot run mysql syntax on select - mysql

I tried running this in phpmyadmin...
It turned out something is not right.
I cannot figure out what is wrong here.
DELIMITER ;
create definer=proiect_wd_user#localhost FUNCTION
f_suma(id_s int, price_f double,product_code_n char(255),quantity_b int)
returns double
BEGIN
select price_f into #price_f
from orders_details WHERE (id=id_s)
select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
end ;
Error:
MySQL said: Documentation
#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 'select quantity_b into #quantity_b
from orders_details WHERE (id=id_s)
set #su' at line 7

There are various problems here with your delimiters. See Working with Stored Procedures in the MySQL documentation.
DROP FUNCTION IF EXISTS f_suma;
DELIMITER //
CREATE DEFINER=proiect_wd_user#localhost
FUNCTION f_suma(id_s int,price_f double,product_code_n char(255),quantity_b int)
RETURNS double
BEGIN
SELECT price_f INTO #price_f FROM orders_details WHERE (id=id_s);
SELECT quantity_b INTO #quantity_b FROM orders_details WHERE (id=id_s);
SET #suma_f=(#price_f*#quantity_b);
RETURN #suma_f;
END //
DELIMITER ;

Related

MySQL procedure results in a syntax error

This is my MySQL procedure. I got an error while executing this.
DELIMITER //
CREATE PROCEDURE GET_ORDER_HISTORY_LIST(IN distid int)
BEGIN
SELECT
ort.order_id,
ort.transaction_id,
ort.user_id,
ort.transaction_date,
ort.insert_by,
ort.organization_id,
odt.course_id,
count(odt.quantity),
ct.course_name,
ct.course_code
FROM cdp_order_master as ort
JOIN cdp_order_detail as odt ON odt.order_id = ort.order_id
JOIN cdp_course as ct ON ct.course_id = odt.course_id
WHERE ort.user_id = distid
GROUP BY ort.order_id
END //
DELIMITER ;
Error is
#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' at line 19
DELIMITER //
CREATE PROCEDURE GET_ORDER_HISTORY_LIST(IN distid int)
BEGIN
SELECT
ort.order_id,
ort.transaction_id,
ort.user_id,
ort.transaction_date,
ort.insert_by,
ort.organization_id,
odt.course_id,
count(odt.quantity),
ct.course_name,
ct.course_code
FROM cdp_order_master as ort
JOIN cdp_order_detail as odt ON odt.order_id = ort.order_id
JOIN cdp_course as ct ON ct.course_id = odt.course_id
WHERE ort.user_id = distid
GROUP BY ort.order_id; //I had made change at this line.
END //
DELIMITER ;
You can try above query.
Here is a SQL Fiddle.

mysql stored procedure declare error

I don't really get the error ...
i put the declare statement right after begin. i usually do a lot of MS SQL so i am not that practiced in mysql syntax. please help me out
DELIMITER //
CREATE DEFINER=`root`#`localhost` PROCEDURE `AddEntry2`(IN `inDate` DATE, IN `inUser` INT, IN `inComment` TEXT, IN `inStart` INT, IN `inEnd` INT)
BEGIN
DECLARE commID UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT commID := MAX(id)
FROM dimcomment
WHERE comment = inComment;
INSERT INTO factentry (FKDate, FKUser, FKComment,FKStart,FKEnd,FKAbsence)
VALUES (
inDate,
inUser,
commID,
inStart,
inEnd,
1
);
END //
Error I get:
#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 'UNSIGNED;
INSERT IGNORE INTO dimcomment (comment) values (inComment);
SELECT c' at line 3
well i changed UNSIGNED to INT and Error Message changed:
#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 ':= MAX(id)
FROM dimcomment
' at line 5
Seems like i cannot set via SELECT VAR := VALUE ?

SQL If Statment / select into in a stored procedure

I cannot understand why the following SQL procedure will not store on my database and is reporting an error, I've been at it for ages and am totally puzzled.
DELIMITER $$
CREATE PROCEDURE add_brand(IN inBrandName TEXT)
BEGIN
DECLARE brandexists INT DEFAULT 0;
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
IF brandexists = 1 THEN
select "exists";
ELSE
INSERT INTO tblBrand (BrandName) VALUES (inBrandName);
SELECT LAST_INSERT_ID();
END IF;
END
The error i'm getting is this:
#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 'WHERE BrandName = inBrandName INTO brandexists; IF brandexists = 1 THEN sele' at line 6
any ideas?
You are missing a table where you select from:
SELECT count(*) WHERE BrandName = inBrandName INTO brandexists;
^---here (from some_table)

syntax error # casting new.value in table trigger

is there any problem :
DELIMITER ;;
CREATE TRIGGER `UPD_after_financialStatus` AFTER UPDATE ON `financialStatus`
FOR EACH ROW
BEGIN
DECLARE `#grossAmountTillNow` varchar(100);
SET #grossAmountTillNow = CONCAT(
CAST(OLD.grossAmountTillNow AS varchar(50)),
"---",
CAST(NEW.grossAmountTillNow AS varchar(50)));
...
it says Syntax error near 'varchar(50)),"---",CAST(NEW.grossAmountTillNow AS varchar(50))); SET #grossAmoun' at line 4.
can you help please ?
Please compare:
SELECT CAST(CURRENT_TIMESTAMP AS VARCHAR(50))
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 'VARCHAR(50))' at line 1
SELECT CAST(CURRENT_TIMESTAMP AS CHAR(50))
2012-04-10 12:30:29
The allowed values are explained at Cast Functions and Operators.

Stored procedures written in MySQL 5.5.8 don't work in 5.1

I have some stored procedures and a trigger that work great in MySQL 5.5.8 but for some reason don't work in 5.1. The error descriptions aren't enough for me to figure out the problem. Here is the code and the errors.
CREATE PROCEDURE `cg_getMatchingContent`(
MatchTerm VARCHAR(255),
MaxResults INT)
BEGIN
SELECT * FROM (
SELECT t.*, INSTR(t.`Title`,MatchTerm) as Pos
FROM cg_content t ) c
WHERE Pos>0 ORDER BY Pos LIMIT 0, MaxResults;
END
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 'MaxResults' at line 8
DELIMITER ;;
CREATE TRIGGER `cg`.`cg_content_UrlDup_ConstTrig`
BEFORE INSERT ON `cg`.`cg_content`
FOR EACH ROW
Begin
DECLARE errorString VARCHAR(500);
DECLARE insert_error CONDITION FOR SQLSTATE '99001';
IF new.Url = '' THEN
SET errorString = CONCAT('Url cannot be blank
Title: ' , new.Title);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
END if;
IF Exists(SELECT id FROM cg.cg_content WHERE Url=new.Url) THEN
SET errorString = CONCAT('Url is not unique
Title: ' , new.Title , '
Url: ' + new.Url);
SIGNAL insert_error
SET MESSAGE_TEXT=errorString;
End if;
End ;;
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 'insert_error
SET MESSAGE_TEXT=errorString;END if;IF ' at line 10
From the docs:
Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
5.1 does not support variables in LIMIT and OFFSET.
The second one is easy to figure, hard to fix. SIGNAL and RESIGNAL commands were introduced in MySQL 5.5. You can't convert it easily to 5.1. One way to do it, would be to run a query that errors. For example a SELECT from a non-existent table.