syntax error # casting new.value in table trigger - mysql

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.

Related

Cannot run mysql syntax on select

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 ;

mariadb - what wrong in this create function statement

CREATE FUNCTION f_TP20avg (p_date date, p_symbol varchar(10)) RETURNS Decimal
BEGIN
DECLARE v_TP20avg Decimal(12,2);
SELECT avg(x.TypicalPrice) into v_TP20avg
FROM (SELECT TypicalPrice
FROM StockData
WHERE symbol = p_symbol
AND PriceDate <= p_date
ORDER BY PriceDate DESC LIMIT 20) x;
RETURN v_TP20avg;
END;
I am getting following error while trying to create function in MariaDB. I am not able to figure out what's wrong with syntax.
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
please help.

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)

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.