#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 = -1 else SELECT audit_id INTO t_new_id FROM a_audit_reg at line 11
delimiter //
CREATE FUNCTION get_audit_id (p_pubco_id int(10),
p_audit_id int(10),
p_fiscal_date date)
RETURNS int(10)
BEGIN
DECLARE t_new_id int(10);
#check paremeters here
if p_pubco_id = 0 or p_audit_id = 0
then set t_new_id = -1
else
set t_new_id = (SELECT audit_id
FROM a_audit_reg
WHERE p_pubco_id = a_audit_reg.pubco_id
and p_audit_id = a_audit_reg.audit_id
and p_fiscal_period = a_audit_reg.fiscal_period_date);
if found_rows() = 0
then
insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
values (p_pubco_id, p_audit_id, p_fiscal_date);
set t_new_id = last_insert_id();
end if;
end if;
return t_new_id;
END //
delimiter ;
change the DELIMITER
use SET
query,
DELIMITER //
CREATE FUNCTION get_audit_id
(
p_pubco_id INT,
p_audit_id INT,
p_fiscal_date DATE
)
RETURNS INT
BEGIN
DECLARE t_new_id INT;
IF p_pubco_id = 0 or p_audit_id = 0 THEN
SET t_new_id = -1;
ELSE
SET t_new_id = (SELECT audit_id
FROM a_audit_reg
WHERE p_pubco_id = a_audit_reg.pubco_id
and p_audit_id = a_audit_reg.audit_id
and p_fiscal_period = a_audit_reg.fiscal_period_date);
IF found_rows() = 0 then
insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
values (p_pubco_id, p_audit_id, p_fiscal_date);
SET t_new_id = last_insert_id();
end if;
end if;
return t_new_id;
END //
DELIMITER ;
Related
My Code MYSQL
DELIMITER //
CREATE FUNCTION fNonUnicode(p_inputVar LONGTEXT )
RETURNS LONGTEXT
BEGIN
IF (p_inputVar IS NULL OR p_inputVar = '') THEN RETURN '';
END IF;
DECLARE v_RT LONGTEXT;
DECLARE v_SIGN_CHARS NVARCHAR(256);
DECLARE v_UNSIGN_CHARS NVARCHAR (256);
SET v_SIGN_CHARS = Concat(N'ăâđêôơưàảãạáằẳẵặắầẩẫậấèẻẽẹéềểễệếìỉĩịíòỏõọóồổỗộốờởỡợớùủũụúừửữựứỳỷỹỵýĂÂĐÊÔƠƯÀẢÃẠÁẰẲẴẶẮẦẨẪẬẤÈẺẼẸÉỀỂỄỆẾÌỈĨỊÍÒỎÕỌÓỒỔỖỘỐỜỞỠỢỚÙỦŨỤÚỪỬỮỰỨỲỶỸỴÝ' , NCHAR(272) + NCHAR(208));
SET v_UNSIGN_CHARS = N'aadeoouaaaaaaaaaaaaaaaeeeeeeeeeeiiiiiooooooooooooooouuuuuuuuuuyyyyyAADEOOUAAAAAAAAAAAAAAAEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOUUUUUUUUUUYYYYYDD';
DECLARE v_COUNTER int;
DECLARE v_COUNTER1 int;
SET v_COUNTER = 1;
WHILE (v_COUNTER <= CHAR_LENGTH(RTRIM(p_inputVar)))
DO
SET v_COUNTER1 = 1;
WHILE (v_COUNTER1 <= CHAR_LENGTH(RTRIM(v_SIGN_CHARS)) + 1)
DO
IF UNICODE(SUBSTRING(v_SIGN_CHARS, v_COUNTER1,1)) = UNICODE(SUBSTRING(p_inputVar,v_COUNTER ,1))
THEN
IF v_COUNTER = 1 THEN
SET p_inputVar = CONCAT(SUBSTRING(v_UNSIGN_CHARS, v_COUNTER1,1) , SUBSTRING(p_inputVar, v_COUNTER+1,CHAR_LENGTH(RTRIM(p_inputVar))-1));
ELSE
SET p_inputVar = CONCAT(SUBSTRING(p_inputVar, 1, v_COUNTER-1) ,SUBSTRING(v_UNSIGN_CHARS, v_COUNTER1,1) , SUBSTRING(p_inputVar, v_COUNTER+1,CHAR_LENGTH(RTRIM(p_inputVar))- v_COUNTER));
END IF;
BREAK
END IF;
SET v_COUNTER1 = v_COUNTER1 +1;
END WHILE;
SET v_COUNTER = v_COUNTER +1;
END WHILE;
-- SET #inputVar = replace(#inputVar,' ','-')
RETURN p_inputVar;
END;
//
DELIMITER ;
But Error: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 'DECLARE v_RT LONGTEXT;
Finnaly: I want to use:fNonUnicode("Xin chào các bạn") return Xin chao cac ban
MySQL Function to remove accents and special characters
DROP FUNCTION IF EXISTS fn_remove_accents;
DELIMITER |
CREATE FUNCTION fn_remove_accents( textvalue VARCHAR(10000) ) RETURNS VARCHAR(10000)
BEGIN
SET #textvalue = textvalue;
-- ACCENTS
SET #withaccents = 'ŠšŽžÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝŸÞàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿþƒ';
SET #withoutaccents = 'SsZzAAAAAAACEEEEIIIINOOOOOOUUUUYYBaaaaaaaceeeeiiiinoooooouuuuyybf';
SET #count = LENGTH(#withaccents);
WHILE #count > 0 DO
SET #textvalue = REPLACE(#textvalue, SUBSTRING(#withaccents, #count, 1), SUBSTRING(#withoutaccents, #count, 1));
SET #count = #count - 1;
END WHILE;
-- SPECIAL CHARS
SET #special = '!##$%¨&*()_+=§¹²³£¢¬"`´{[^~}]<,>.:;?/°ºª+*|\\''';
SET #count = LENGTH(#special);
WHILE #count > 0 do
SET #textvalue = REPLACE(#textvalue, SUBSTRING(#special, #count, 1), '');
SET #count = #count - 1;
END WHILE;
RETURN #textvalue;
END
|
DELIMITER ;
please try with code below
DROP FUNCTION IF EXISTS fn_remove_accents;
DELIMITER //
CREATE FUNCTION fn_remove_accents(textvalue TEXT)
RETURNS TEXT
BEGIN
SET #textvalue = textvalue;
-- ACCENTS
SET #withaccents = 'ăâđêôơưàảãạáằẳẵặắầẩẫậấèẻẽẹéềểễệếìỉĩịíòỏõọóồổỗộốờởỡợớùủũụúừửữựứỳỷỹỵýĂÂĐÊÔƠƯÀẢÃẠÁẰẲẴẶẮẦẨẪẬẤÈẺẼẸÉỀỂỄỆẾÌỈĨỊÍÒỎÕỌÓỒỔỖỘỐỜỞỠỢỚÙỦŨỤÚỪỬỮỰỨỲỶỸỴÝ';
SET #withoutaccents = 'aadeoouaaaaaaaaaaaaaaaeeeeeeeeeeiiiiiooooooooooooooouuuuuuuuuuyyyyyAADEOOUAAAAAAAAAAAAAAAEEEEEEEEEEIIIIIOOOOOOOOOOOOOOOUUUUUUUUUUYYYYY';
SET #count = LENGTH(#withaccents);
WHILE #count > 0 DO
SET #textvalue = REPLACE(#textvalue, SUBSTRING(#withaccents, #count, 1), SUBSTRING(#withoutaccents, #count, 1));
SET #count = #count - 1;
END WHILE;
RETURN #textvalue;
END
//
DELIMITER ;
I am creating Trigger into Mysql Workbanch , but not able to create the trigger.
Can Anyone Help me For This.
DELIMITER $$
create TRIGGER Insert_Test
After Delete ON Test
FOR EACH ROW
BEGIN
START TRANSACTION;
DECLARE v_ID INT;
DECLARE v_Error TINYINT;
DECLARE v_AgentID INT;
SELECT v_Error = 0;
v_ID=Old.id;
BEGIN
DELETE Test2 WHERE id = SELECT id FROM Test where id=v_ID;
Rollback;
SET v_Error = 1;
END
IF v_Error = 0;
THEN
COMMIT;
ELSEIF
v_Error = 1;
THEN
ROLLBACK;
END IF;
END
DELIMITER ;
Sql server Trigger
ALTER TRIGGER [dbo].[tr_DelRecordTypeID] ON [dbo].[luRecordType] FOR DELETE
AS
SET NOCOUNT ON
BEGIN TRANSACTION
DECLARE #ID INT, #GroupTypeID INT, #Error BIT, #Msg VARCHAR(500)
SELECT #Error = 0
SELECT #ID = RecordTypeID FROM deleted
SELECT #GroupTypeID = 30
IF EXISTS ( SELECT g.GroupID
FROM luGroup g,
[luGroupDetail] gd
WHERE g.[GroupID] = gd.[GroupID]
AND g.[GroupTypeID] = #GroupTypeID
AND gd.[MemberID] = #ID )
BEGIN
DELETE [agAgent] WHERE [AgentID] = (SELECT TOP 1 AgentID FROM agAgentPayType)
Rollback transaction
SET #Error = 1
END
IF #Error = 0
BEGIN
COMMIT TRANSACTION
END
ELSE
IF #Error = 1
BEGIN
ROLLBACK TRANSACTION
END
This trigger which i am trying to achieve into mysql workbanch, please check
I shall Be thankful,
Thanks
Aman
When I am trying to create a function in mysql 5.7.20, I met error 1064:
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case
when exists (select 1 from user_favorstocks where uid=uid_int and stockid = stockid_char)
then begin
insert into user_favorstocks (uid, stockid, added_date) values (uid_int, stockid_char, added_date);
set ret=1;
end
else
begin
case (select is_deleted from user_favorstocks where uid=uid_int and stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks set is_deleted=0, db_update_time=now() where uid=uid_int and stockid=stockid_char;
set ret=3;
end;
end
end
end
return ret
end//
delimiter ;
The error message is
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 'else
begin
case (select is_deleted from user_favorstocks whe' at line 15
Does this because that I am using begin...end in an else clause?
I found the example in MySQL's documentation:
DELIMITER |
CREATE PROCEDURE p()
BEGIN
DECLARE v INT DEFAULT 1;
CASE v
WHEN 2 THEN SELECT v;
WHEN 3 THEN SELECT 0;
ELSE
BEGIN
END;
END CASE;
END;
|
It seems valid to use a begin...end statement in an else clause.
Try:
DROP FUNCTION IF EXISTS `add_favorstocks`;
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case when exists (select 1
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
then begin
insert into user_favorstocks
(uid, stockid, added_date)
values
(uid_int, stockid_char, added_date);
set ret=1;
-- end <-- missing a semicolon
end;
else
begin
case (select is_deleted
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks
set is_deleted=0,
db_update_time=now()
where uid=uid_int and
stockid=stockid_char;
set ret=3;
end;
-- end <-- missing CASE and a semicolon
end case;
-- end <-- missing a semicolon
end;
-- end <-- missing CASE and a semicolon
end case;
-- return ret <-- missing a semicolon
return ret;
end//
delimiter ;
So I have this procedure to calculate freights. I need to select two values from matching row. And according to condition based on in_quantity, out of these two previously selected values one will be set to out_total.
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
out_total = val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2;
END IF;
END$$
DELIMITER ;
When executed it gives following 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 '= val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2' at line 16
beacause any value assign to a variable in mysql , we use SET keyword.
so change only in procedure
SET out_total = val1;
SET out_total = val1;
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
SET out_total = val1;
END IF;
IF(in_quantity > 9) THEN
SET out_total = val1;
END IF;
END$$
DELIMITER ;
I am just getting my feet wet with stored procedures. According to the tutorials that I have seen, this should be valid (MySQL 5.5):
CREATE PROCEDURE someFunction ( a VARCHAR(256), b VARCHAR(256) )
BEGIN
DECLARE haveAllVariables INT;
SET haveAllVariables = 1;
IF a = "" THEN SET haveAllVariables = 0
ELSEIF b = "" THEN SET haveAllVariables = 0
END IF;
However, it is throwing this 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 'ELSEI
F b = "" THEN SET haveAllVariables = 0
Where is the error in my syntax?
Thanks.
You're missing a semicolon
CREATE PROCEDURE someFunction ( a VARCHAR(256), b VARCHAR(256) )
BEGIN
DECLARE haveAllVariables INT;
SET haveAllVariables = 1;
IF a = "" THEN SET haveAllVariables = 0;
ELSEIF b = "" THEN SET haveAllVariables = 0;
END IF;
Stored procedures are a bit tricky. But here is an example I tested and posted for you. In your example you were missing a couple of semicolons and the final "END".
DELIMITER $$
CREATE PROCEDURE someFunction ( a VARCHAR(256), b VARCHAR(256) )
BEGIN
DECLARE haveAllVariables INT;
SET haveAllVariables = 1;
IF a = '' THEN
SET haveAllVariables = 0;
ELSEIF b = '' THEN
SET haveAllVariables = 0;
END IF;
END $$