MySQL Throws an Error IF Function in Stored Procedure - mysql

i have some problem regarding my mysql stored procedure, the mysql always shows up error 1064, error mysql syntax. where i was wrong?
DELIMITER $$
USE `itin`$$
DROP PROCEDURE IF EXISTS `sp_Transaction`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_Transaction`(IN inpt TINYINT(4), IN inpt2 TINYINT(4))
BEGIN
IF((inpt = 0) AND (inpt2 = 0)) THEN
SELECT idtr_inventory, showStatus(idms_status) AS idms_status, showLocation(idms_location) AS idms_location
, showUnitType(idms_unittype) AS idms_unittype, USER, user_email
, showDepartment(idms_department) AS idms_department, asset_tag, sn, brand, model, showBBUser(bb_user) AS bbUser
, dt_purchase, warranty, dt_warranty_exp, nm_computer, ip_address, remarks
FROM tr_inventory;
ELSE IF(inpt = 0) THEN
SELECT idtr_inventory, showStatus(idms_status) AS idms_status, showLocation(idms_location) AS idms_location
, showUnitType(idms_unittype) AS idms_unittype, USER, user_email
, showDepartment(idms_department) AS idms_department, asset_tag, sn, brand, model, showBBUser(bb_user) AS bbUser
, dt_purchase, warranty, dt_warranty_exp, nm_computer, ip_address, remarks
FROM tr_inventory WHERE (idms_location = inpt2);
ELSE IF(inpt2 = 0) THEN
SELECT idtr_inventory, showStatus(idms_status) AS idms_status, showLocation(idms_location) AS idms_location
, showUnitType(idms_unittype) AS idms_unittype, USER, user_email
, showDepartment(idms_department) AS idms_department, asset_tag, sn, brand, model, showBBUser(bb_user) AS bbUser
, dt_purchase, warranty, dt_warranty_exp, nm_computer, ip_address, remarks
FROM tr_inventory WHERE (idms_unittype = inpt)
ELSE
SELECT idtr_inventory, showStatus(idms_status) AS idms_status, showLocation(idms_location) AS idms_location
, showUnitType(idms_unittype) AS idms_unittype, USER, user_email
, showDepartment(idms_department) AS idms_department, asset_tag, sn, brand, model, showBBUser(bb_user) AS bbUser
, dt_purchase, warranty, dt_warranty_exp, nm_computer, ip_address, remarks
FROM tr_inventory WHERE (idms_unittype = inpt) AND (idms_location = inpt2);
END IF;
END IF;
END IF;
END$$
DELIMITER ;
is it wrong in the end if statement? if yes, where i need to place? thanks.
error:
Query : CREATE DEFINER=root#localhost PROCEDURE sp_Transaction(in inpt tinyint(4), IN inpt2 TINYINT(4)) begin if((inpt = 0) AND...
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 'else

You missed a;
...
FROM tr_inventory WHERE (idms_unittype = inpt);
^------here
ELSE
...

Related

How to create a parameterized STORED PROCEDURE with CRUD operation in MYSQL Version(6.0.11-alpha-community)

I created a parameterized STORED PROCEDURE with CRUD operation in MYSQL Version(6.0.11-alpha-community)
DELIMITER $$
CREATE PROCEDURE `mae`.`USP_CustomersToken_CURD`(IN CID INT , IN CName VARCHAR(50), IN CToken VARCHAR(250), IN CTokenTime DATETIME , IN OPType INT)
BEGIN
IF (OPType = 1) --Insert
THEN
INSERT INTO tbl_CustomersTokenInfo (ID, CustomerName, Token, TokenTime) VALUES (CID, CName, CToken, CTokenTime)
ELSEIF (OPType = 2) --delete
THEN
DELETE FROM tbl_CustomersTokenInfo WHERE ID = CID
ELSEIF (OPType = 3) --Update
THEN
UPDATE tbl_CustomersTokenInfo SET CustomerName = CName, Token = CToken , TokenTime = CTokenTime WHERE ID = CID
ELSEIF (OPType = 4) --Select
THEN
SELECT * FROM tbl_CustomersTokenInfo WHERE ID =CID
ELSEIF (OPType = 5)--Fetch
THEN
SELECT * FROM tbl_CustomersTokenInfo
END IF
END$$
DELIMITER ;
but when I executing this SP I'm getting this below error :
Query: CREATE PROCEDURE mae.USP_CustomersToken_CURD(IN CID INT , IN CName VARCHAR(50), IN CToken VARCHAR(250), IN CTokenTime DATETI...
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 'THEN
INSERT INTO tbl_CustomersTokenInfo (ID, CustomerName, Token, TokenTime) VAL' at line 4
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0 sec
I'm new to MYSQL commands and syntaxes if I did any mistake give the best approach .
MySQL (unlike most other DBMS) doesn't like comments introduced with -- where no space follows --.
You need to terminate all the statements, including the IF statement (after END IF), with a ;.
Correct that:
DELIMITER $$
CREATE PROCEDURE `mae`.`USP_CustomersToken_CURD`(IN CID INT , IN CName VARCHAR(50), IN CToken VARCHAR(250), IN CTokenTime DATETIME , IN OPType INT)
BEGIN
IF (OPType = 1) -- Insert
THEN
INSERT INTO tbl_CustomersTokenInfo (ID, CustomerName, Token, TokenTime) VALUES (CID, CName, CToken, CTokenTime);
ELSEIF (OPType = 2) -- delete
THEN
DELETE FROM tbl_CustomersTokenInfo WHERE ID = CID;
ELSEIF (OPType = 3) -- Update
THEN
UPDATE tbl_CustomersTokenInfo SET CustomerName = CName, Token = CToken , TokenTime = CTokenTime WHERE ID = CID;
ELSEIF (OPType = 4) -- Select
THEN
SELECT * FROM tbl_CustomersTokenInfo WHERE ID =CID;
ELSEIF (OPType = 5)-- Fetch
THEN
SELECT * FROM tbl_CustomersTokenInfo;
END IF;
END$$
DELIMITER ;

MySQL Stored Procedure Issue #1064

This little one gives me syntax problem, i cant solve that i'll appreciate that.
DELIMITER $$
CREATE procedure kullanicibilgicek
(
p_kadi varchar(50)
)
begin
declare v_sonuc INT;
if(CHAR_LENGTH(RTRIM(p_kadi)) > 4)
then
if exists (select kullanicilar.kullaniciadi, kullanicilar.sifre from kullanicilar where kullaniciadi = p_kadi)
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1), upper(left(soyad,1))+ right(soyad, char_length(rtrim(soyad)) -1), yetki, upper(left(kullaniciadi,1))+ right(kullaniciadi, char_length(rtrim(kullaniciadi)) -1), kullanicilar.sifre from kullanicilar where kullaniciadi = p_kadi;
else
set v_sonuc = 0;
select v_sonuc;
end if;
else
if exists (select kullanicilar.kullaniciadi, kullanicilar.sifre from kullanicilar where id = Cast(p_kadi as int))
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1), upper(left(soyad,1))+ right(soyad, char_length(rtrim(soyad)) -1), yetki, upper(left(kullaniciadi,1))+ right(kullaniciadi, char_length(rtrim(kullaniciadi)) -1), kullanicilar.sifre from kullanicilar where id = cast(p_kadi as int);
else
set v_sonuc = 0;
select v_sonuc;
end if;
end if;
END;
$$
DELIMITER ;
#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 'int))
then
select id, upper(left(ad,1))+ right(ad, char_length(rtrim(ad)) -1' at line 18

MySQL: Insert with conditional

I must perform the following situation down, but when you run got the error:
SQL 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 'INTO produto_seriais(serial_id) VALUES( SELECT id
FROM seriais WHERE serial =' at line 5
SELECT CASE WHEN (
SELECT COUNT(id) FROM seriais WHERE serial = '2020'
) > 1
THEN
(INSERT INTO produto_seriais(serial_id) VALUES(
SELECT id FROM seriais WHERE serial = '2020'
))
ELSE (
INSERT INTO seriais (serial) VALUE('2020');
SET #last_id_in_table1 = LAST_INSERT_ID();
INSERT INTO produto_seriais (serial_id) VALUES (#last_id_in_table1);
)
END;
The case is as follows:
I'll get in "serial" table by serial "X". If it already exists, unless your ID in the "produto_seriais" table. If there is (serial), I will save the same, recover your ID and save to "produto_seriais". Any suggestions for how to do this?
Important Note: This routine will run will be thousands of times each execution (10,000 or more, depending on the quantity of serial).
P.s.: Sorry for my bad english.
Try a stored procedure
DELIMITER //
CREATE PROCEDURE sp_produto_seriais(IN `p_serial_id`)
IF EXISTS (SELECT * FROM seriais WHERE serial = p_serial_id )
BEGIN
INSERT INTO produto_seriais (serial_id)
SELECT id
FROM seriais
WHERE serial = p_serial_id
END
ELSE
BEGIN
INSERT INTO seriais (serial) VALUE(p_serial_id);
INSERT INTO produto_seriais (serial_id) VALUES (LAST_INSERT_ID());
END //
DELIMITER ;
usage:
CALL sp_produto_seriais('2020')
You could use the if exists..else statement.
If exists (select * from ....)
Begin
Insert into ... Select id from table
End
Else
Begin
Insert..
End
Please fill .... with your statements. You could use the link here to convert it for MySQL.
Convert if exists for MySQL

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)

Mysql sql error #1064

check the manual that corresponds to your MySQL server version for the right syntax to use near: 'SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed' at line 2
my code:
DROP TRIGGER IF EXISTS newsmonitoringrawdata.TNTInsertionTrigger;
CREATE TRIGGER TgtInsertionTrigger BEFORE INSERT ON tnews
FOR EACH ROW BEGIN
SET #isRecordCreated =
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
IF #isRecordCreated>0 THEN
UPDATE capacity
SET MsgNumber=MsgNumber+1,
size=size+NEW.MSize
WHERE
server=NEW.server
AND
feed=NEW.feed
and
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i');
ELSE
INSERT INTO capacity(server, feed, streamdt, msgNumber, size)
VALUES
(
NEW.server,
NEW.feed,
date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i:00'),
1,
NEW.size
);
END IF;
END
SET #isRecordCreated = (
SELECT count(*) FROM capacity
WHERE
server=NEW.server
AND
feed=NEW.Feed
AND
date_Format(streamdt,'%Y-%m-%d %H:%i')=date_Format(NEW.StreamDT,'%Y-%m-%d %H:%i')
);
You have to add parantheses () around the query.