MySQL Stored Procedure Issue #1064 - mysql

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

Related

MySQL migrate data from one table to another

I'm attempting to move data from one table to another, as part of a restructure to the database architecture. I'm using PHPMyAdmin and MySQL to do so.
The SQL is meant to, for each emergency_contacts.id, move e_c.id and e_c.activity_id to activities_emergency_contacts, where the pair will form a composite key to link a contact with an activity.
The following SQL returns an error:
CREATE PROCEDURE dowhile()
BEGIN
SET #cid = (SELECT MIN(`id`) FROM `emergency_contacts`);
SET #aid = (SELECT `activity_id` FROM `emergency_contacts` WHERE `id` = #cid);
WHILE #cid IS NOT NULL DO
INSERT INTO activities_emergency_contacts (activity_id, contact_id)
VALUES (#aid, #cid);
SET #cid = (SELECT MIN(id) FROM emergency_contacts WHERE #cid < id);
SET #aid = (SELECT activity_id FROM emergency_contacts WHERE id = #cid);
END WHILE;
END;
CALL dowhile();
SQL query:
CREATE PROCEDURE dowhile() BEGIN SET #cid = (SELECT MIN(id) FROM
emergency_contacts)
MySQL said:
#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
I have searched the MySQL specific documentation to try and find any issues with my SET, WHILE, BEGIN/END, INSERT, CREATE - just about every line. I'm unsure how to proceed. Any advice would be greatly appreciated.
EDIT: I missed a closing bracket inside the WHILE on SELECT MIN (id), however I've fixed this and am still getting the exact same issue.
EDIT: The issue was that I was not using DELIMITER. Correct SQL:
DELIMITER $$
CREATE PROCEDURE dowhile()
BEGIN
SET #cid = (SELECT MIN(`id`) FROM `emergency_contacts`);
SET #aid = (SELECT `activity_id` FROM `emergency_contacts` WHERE `id` = #cid);
WHILE #cid IS NOT NULL DO
INSERT INTO activities_emergency_contacts (activity_id, contact_id) VALUES (#aid, #cid);
SET #cid = (SELECT MIN(id) FROM emergency_contacts WHERE #cid < id);
SET #aid = (SELECT activity_id FROM emergency_contacts WHERE id = #cid);
END WHILE;
END$$
DELIMITER ;
CALL dowhile();

SQL variable in IF exists

I can't figure out the correct way to assign query output into a variable that i could later use in an INSERT clause.
The error I get 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 'SET #lennuid = (select lend_id from lend where sihtpunkt=in_kuhu AND kuupaev=in_' at line 8
I've tried looking at every guide on variables in SQL but none of them could help me solve this.
Iam using the below code:
CREATE PROCEDURE proov(
in_kuhu varchar(50), in_nimi1 varchar(50), in_nimi2 varchar(50), in_adre varchar(50), in_telo varchar(20), in_email varchar(100), in_date date)
BEGIN
DECLARE viga INTEGER DEFAULT 0 ;
DECLARE lennuid INT;
START TRANSACTION;
IF exists (
SET #lennuid = (select lend_id from lend where sihtpunkt=in_kuhu AND kuupaev=in_date)) THEN
INSERT INTO broneering
(lend_id, bron_aeg, eesnim, perenimi, aadress, telefon, email)
VALUES
(lennuid, NOW(), in_nimi1, in_nimi2, in_adre, in_telo, in_email);
ELSE
set viga=1;
SELECT 'Muudatus ebaonnestus ',viga;
END IF;
IF viga=0 then
COMMIT;
select 'korras';
ELSE
select 'tagasi';
ROLLBACK;
END IF;
THEN causing the issue.
IF exists (
SET #lennuid = (select lend_id from lend where sihtpunkt=in_kuhu AND kuupaev=in_date))
BEGIN -- Instead of THEN use BEGIN , END
INSERT INTO broneering
(lend_id, bron_aeg, eesnim, perenimi, aadress, telefon, email)
VALUES
(lennuid, NOW(), in_nimi1, in_nimi2, in_adre, in_telo, in_email);
END
ELSE
BEGIN
set viga=1;
SELECT 'Muudatus ebaonnestus ',viga;
END
END IF;
You might want to use SELECT INTO:
select lend_id into #lennuid
from lend
where sihtpunkt=in_kuhu AND kuupaev=in_date;
Alternatively:
select #lennuid := lend_id
from lend
where sihtpunkt=in_kuhu AND kuupaev=in_date;
Either way works the same, then you can use that variable however you like...
In your particular example, though, the problem is actually how your exists is being used along with the set. To fix this, use the following:
IF exists (select lend_id from lend where sihtpunkt=in_kuhu AND kuupaev=in_date) THEN ...
What the problem here is, is you were setting a variable with SET inside of the EXISTS check.
Or you could do your select first as a COUNT into a numeric variable:
DECLARE cnt INTEGER;
select Count(lend_id) into #cnt
from lend
where sihtpunkt=in_kuhu
and kuupaev=in_date;
IF #cnt > 0 THEN
END IF;

ERROR 1064 (42000): check the manual that corresponds to your MySQL server version for the right syntax to use near 'END'

here is mycode
i am trying to create a trigger after insert on a table say product table to a change traker table called audit table
like this
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
)
END
//
DELIMITER ;
this is the error i am getting
i tried all these ways
used | ,S etc instead of // and
removed ; after end, placed // or \ or $$ together and below end
nothing works, some one please help me
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 'END' at line 32
some one please help me
Place sql terminator ";" after create table statement
remove ";" after end delimeter.
So your code should be as per below,
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number into #l_product_description, #l_product_number from product_table p where p.product_description = (select max(pg.product_number)from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
);
END
//
DELIMITER
The solution provided by #juergen in comments is working fine, that is, Add a ; after the insert statement (before the END) Thanks for finding my mistake. I was looking for it for about 4 hours so Answer is here:
DELIMITER //
CREATE TRIGGER product_table_after_insert
AFTER INSERT
ON product_table
FOR EACH ROW
BEGIN
DECLARE l_product_description varchar(500);
DECLARE l_product_number int;
set #l_table_name = 'product_table';
set #l_action = 'INSERT';
set #l_table_column = 'all columns';
set #l_description = 'new row inserted';
select p.product_description ,p.product_number
into #l_product_description, #l_product_number
from product_table p
where p.product_description =
(select max(pg.product_number)
from product_table pg);
-- Insert record into audit table
INSERT INTO audit_table_test
( table_name,
changed_row_id,
action,
table_column,
change_desciption,
change_time
)
VALUES
( l_table_name,
l_product_number,
l_action,
l_table_column,
l_description,
SYSDATE()
); //<<---- Semicolon needed to be here
END
//
DELIMITER;

error in trigger (mysql)

I am trying to create a trigger with an IF condition and I am getting an error on line 6.
DELIMITER $$
CREATE TRIGGER trgSoftwareLicenseDetails
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW BEGIN
IF new.Flag = 0 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID =
(SELECT SoftwareNameFK FROM SoftwareLicenseDetails
WHERE ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
FROM SoftwareLicenseDetails
WHERE ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
ELSE IF new.Flag = 1 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', 'N/A', (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Deleted',old.ComputerFK);
END IF;
END; $$
DELIMITER ;
Error:
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 'FROM SoftwareLicenseDetails WHERE
ComputerFK=new.ComputerFK ORDER BY ID Desc LI' at line 6 0.000 sec
What am I missing?
Check this:
DELIMITER $$
CREATE TRIGGER trgSoftwareLicenseDetails
AFTER UPDATE ON SoftwareLicenseDetails
FOR EACH ROW
BEGIN
IF new.Flag = 0 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', (SELECT Title FROM SoftwareTypes WHERE ID =
(SELECT SoftwareNameFK FROM SoftwareLicenseDetails
WHERE ComputerFK=new.ComputerFK
ORDER BY ID Desc
LIMIT 1)), (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Added',new.ComputerFK);
ELSEIF new.Flag = 1 THEN
INSERT INTO audithistory (audit_date, audit_field, audit_oldvalue, audit_changelog_fk, audit_newvalue, audit_assetid_fk) VALUES (Now(),'Software License Details', 'N/A', (SELECT MAX(ID) FROM Changelog as ChangelogID ), 'License Deleted',old.ComputerFK);
END IF;
END; $$
DELIMITER ;

MySQL Throws an Error IF Function in Stored Procedure

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
...