Stored procedure if mysql - mysql

create procedure update_keyoffice(out p_key varchar(30),out p_hostname varchar(30))
begin
declare p_keyoff varchar(30);
select keyoff into p_keyoff from masterpc where keyoffice = p_key;
if(p_keyoff = p_key) then
insert into keyofficehistory (id,keyoff,hostname,datecreate) values
(null,p_key,p_hostname,now())
end if
end
error
SQL query:
CREATE PROCEDURE update_keyoffice( out p_key varchar( 30 ) , out
p_hostname varchar( 30 ) ) BEGIN DECLARE p_keyoff varchar( 30 ) ;
MySQL said: Documentation
#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

While defining stored procedures and triggers, you have to use custom delimiter which, it seems, is missing in your code.
Change your code as below:
delimiter //
-- your stored procedure code here
-- but don't forget to use new delimiter after end statement
end; //
-- now reset delimiter
delimiter ;
Refer to:
CREATE PROCEDURE and CREATE FUNCTION Syntax

Related

Syntax problem of bacth sql execution in procedure

Failed to run followwing code in mysql 8.0.
drop table if exists test ;
create table if not exists test(rowid int);
delimiter$$
drop procedure if exists line_sum $$
create procedure line_sum()
begin
declare i int ;
declare exe_sql varchar(100);
set i=5 ;
while i>0 do
set exe_sql = concat('alter table test add column d ',i,' int') ;
prepare ppsql from exe_sql ;
execute ppsql ;
deallocate prepare ppsql ;
set i = i-1;
end while ;
end$$
delimiter ;
It reported:
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 'while i>0 do
set exe_sql = concat('alter table test add column d ',i,' int'
at line 11
However my colleague and I have checked it many times, finding no issues in the syntax and getting more confusion.

Mysql Procedure Syntax error on Update

DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (FAT_UCI, FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD)
SELECT FAT_UCI, FAT_DESCRICAO, FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE, FAT_DATA_CAD
FROM pad_fato WHERE (FAT_ID = IDFATO);
END//
BEGIN
UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI = UCI);
END//
DELIMITER ;
Error (12,1): 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 UPDATE tad_termo_de_ajustamento SET TAD_STATUS_ID="2" WHERE (TAD_FK_PRE_UCI=UCI) at line 2
You have double "END" word after each query.
And not pretty formatted code :)
DELIMITER //
DROP PROCEDURE IF EXISTS pad_fato_to_tad_fato//
CREATE PROCEDURE pad_fato_to_tad_fato(IN IDFATO BIGINT, IN UCI BIGINT)
BEGIN
INSERT INTO tad_fato (
FAT_UCI,FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,FAT_DATA_CAD
)
SELECT
FAT_UCI,
FAT_DESCRICAO,
FAT_DATA_CIENCIA_AUTORIDADE,
FAT_CADASTRANTE,
FAT_DATA_CAD
FROM pad_fato
WHERE (FAT_ID=IDFATO);
UPDATE tad_termo_de_ajustamento
SET
TAD_STATUS_ID="2"
WHERE (TAD_FK_PRE_UCI=UCI);
END//
DELIMITER ;

mysql error when creating new stored procedure

simple question
this is the code to creat a simple stored procedure
DELIMITER $$
CREATE PROCEDURE `check_mobile_sp`(
IN mobile_numberEntered VARCHAR(12)
)
BEGIN
SELECT id, mobile_number, `date`
from users
WHERE mobile_number = mobile_numberEntered;
END $$
DELIMITER ;
and this is the 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 'DELIMITER' at line 1
try placing // instead of $$:
DELIMITER //
CREATE PROCEDURE `check_mobile_sp`(
IN mobile_numberEntered VARCHAR(12)
)
BEGIN
SELECT id, mobile_number, `date`
from users
WHERE mobile_number = mobile_numberEntered;
END //
DELIMITER ;

MySQL Create Procedure Yields Error 1064

Good Morning,
I'm creating the following procedure:
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_inventory_audit`(
IN `pitem_id` int,
IN `puser_id` int,
IN `pfield_name` varchar(265),
IN `pold_value` mediumtext,
IN `pnew_value` mediumtext
)
BEGIN
INSERT INTO inventory_audit (item_id, user_id, field_name, old_value, new_value)
VALUES (pitem_id, puser_id, pfield_name, pold_value, pnew_value);
END$$
It is being copied to our new server running MySQL 5.5.19 from our old server running MySQL 5.0.45.
When I excecute the above code on the new server, I recieve the 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 '' at line 11
Does this mean that each entry inside the VALUES parentheses must be surrounded by '' eg. 'pitem_id' ?
You need to have DELIMITER $$ before the create statement.
You didn't change the delimiter from the default ;, so the ; you're using there is actually terminating the procedure, not the query.
DELIMITER $$ <--- add this line
CREATE ....
...
END$$

mySQL stored procedure error

I am trying to use a if statement in my stored mySQL procedure, but when I try to create it in mySQL workbench I get this error 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 'database'.'table' WHERE date=dateIn;.
Here is the code:
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDate`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN EDIT `database`.`table` WHERE date=dateIn;
ELSE SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END$$
I am new to stored procedures, so it's probably a very noob mistake.
Thanx in advance!
date is a reserved word in mySQL. You will have to wrap that in backticks as well.
Here is correct version of your procedure
DELIMITER $$
CREATE DEFINER=`rates_d_db` PROCEDURE `byDatee`(in dateIn VARCHAR(255),in action VARCHAR(255))
BEGIN
IF action = "edit" THEN
-- I used select below as i don't know what you want in edit either alter table or update table
SELECT * FROM `database`.`table` WHERE date=dateIn;
ELSE
SELECT * FROM `database`.`table` WHERE date=dateIn;
END IF;
END $$