Ok, this is only the second stored procedure I've written. I think you'll get the idea, I'm trying to close a credit line, and all invoices, charges, notes, etc with it. But I get a syntax error.
The goal is to CALL close_account_proc(398985994)
DELIMITER $$
CREATE
PROCEDURE `cc`.`close_account_proc`(cid INT)
#uid_usr := uid_usr FROM credit_acc WHERE type_acc = 'init' AND credit_used_acc = cid;
UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW() WHERE creditid_acc = cid;
UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW() WHERE credit_used_acc = cid;
INSERT INTO notes_not SET uid_usr = #uid_usr, initials_not = 'SYS',status_not = 'complete', date_not = NOW(), text_not = 'Closed credit line '.cid;
UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW() WHERE credit_used_acc = cid;
BEGIN
END$$
DELIMITER ;
So, anway, I get this 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 '#uid_usr := uid_usr from credit_acc where type_acc = 'init' and credit_used_acc ' at line 5.
Any ideas?
DELIMITER $$
CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
BEGIN
/* Check that it's what you wanted */
SELECT uid_usr
INTO #uid_usr
FROM credit_acc
WHERE type_acc = 'init'
AND credit_used_acc = cid;
UPDATE credit_acc SET status_acc = 'closed', void_date_acc = NOW() WHERE credit_used_acc = cid;
UPDATE payment_acc SET status_acc = 'voided', void_date_acc = NOW() WHERE creditid_acc = cid;
UPDATE sbal_sbl SET status_sbl = 'voided', void_date_sbl = NOW() WHERE credit_used_acc = cid;
/* Check that it's what you wanted */
INSERT
INTO notes_not (uid_usr, initials_not, status_not, date_not, text_not)
VALUES (#uid_usr, 'SYS', 'complete', NOW(), CONCAT('Closed credit line ', cid));
UPDATE invoices_inv SET status_inv = 'voided', void_date_inv = NOW() WHERE credit_used_acc = cid;
END
$$
DELIMITER ;
Hmm, im no expert at stored procedures, but isn't it.
CREATE PROCEDURE `cc`.`close_account_proc`(cid INT)
BEGIN
// your stuff
END$$
Related
According phpmyadmin, I have a syntax error in this trigger :
CREATE TRIGGER insert_device
AFTER INSERT ON table_e
FOR EACH ROW
BEGIN
DECLARE m_id_a INTEGER;
DECLARE m_id_d INTEGER;
m_id_d := 0;
SELECT id_a INTO m_id_a FROM table_ua WHERE ua_eui = NEW.eui LIMIT 1;
SELECT id_d INTO m_id_d FROM table_d WHERE d_idapp = m_id_a ORDER BY id asc LIMIT 1;
IF (m_id_d == 0) THEN
INSERT INTO table_d (d_addr, d_eui, d_apps, d_nwks, d_idapp)
VALUE (NEW.addr, NEW.eui, NEW.apps, NEW.nwks, m_id_a);
ELSE
UPDATE TABLE table_d
SET
d_addr = NEW.addr,
d_eui = NEW.eui,
d_apps = NEW.apps,
d_nwks = NEW.nwks
WHERE id_d = m_id_d;
END IF;
END
The error is :
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 ':= 0;
What is the error ? I don't understand what I am doing wrong..
Thanks.
There are several syntax errors in your trigger:
declare a variable, you should invoke it with set;
IF expression can not use ==, just one equal is ok;
update syntax only need to specify table name like UPDATE table_d SET ....
So please try following trigger:
delimiter $$
CREATE TRIGGER insert_device
AFTER INSERT ON table_e
FOR EACH ROW
BEGIN
DECLARE m_id_a INTEGER;
DECLARE m_id_d INTEGER;
set m_id_d = 0;
SELECT id_a INTO m_id_a FROM table_ua WHERE ua_eui = NEW.eui LIMIT 1;
SELECT id_d INTO m_id_d FROM table_d WHERE d_idapp = m_id_a ORDER BY id asc LIMIT 1;
IF (m_id_d = 0) THEN
INSERT INTO table_d (d_addr, d_eui, d_apps, d_nwks, d_idapp)
VALUE (NEW.addr, NEW.eui, NEW.apps, NEW.nwks, m_id_a);
ELSE
UPDATE table_d
SET
d_addr = NEW.addr,
d_eui = NEW.eui,
d_apps = NEW.apps,
d_nwks = NEW.nwks
WHERE id_d = m_id_d;
END IF;
END
$$
is there something wrong with this trigger ? It suppose to update a datefield on a table in another database. Can anyone give me a hand ? thanks
DROP TRIGGER IF EXISTS depoisDeInserir_invoice;
DELIMITER $$
CREATE TRIGGER depoisDeInserir_invoice AFTER INSERT ON pag_invoice
FOR EACH ROW
BEGIN
DECLARE trial datetime;
SET trial = (select dataTrial
from networkcen_1.companyproductuser
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey));
IF (trial < CURDATE()) THEN
UPDATE networkcen_1.companyproductuser
SET dataVencimento = DATE_ADD(CURDATE(),INTERVAL NEW.dias DAY)
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey);
ELSE
UPDATE networkcen_1.companyproductuser
SET dataVencimento = DATE_ADD(trial,INTERVAL NEW.dias DAY)
where (CompanyKey = NEW.companyKey)
and (ProductKey = NEW.productKey)
and (UserKey = NEW.productKey);
END IF;
END;
$$
DELIMITER ;
I have a MySQL trigger that is being used to call the rsaencrypt function to encrypt a particular value.
DELIMITER $$
CREATE TRIGGER ssninsertencrypt BEFORE INSERT ON redcap_data
FOR EACH ROW
BEGIN
IF new.project_id = (SELECT ProjectID FROM redcap_encryption)
AND new.field_name = (SELECT FieldName FROM redcap_encryption)
THEN
SET #PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
SET new.value = rsaencrypt(new.value,#PublicKey);
END IF;
END$$
DELIMITER ;
This seems to be inconsistently working/not working so i'd like to insert the completed statement the trigger produces into another table so can see what is being passed to the rsaencrypt or not passed. I thought i could just do a SET #SQL () like the following
SET #SQL =
(IF new.project_id = (SELECT ProjectID FROM redcap_encryption)
AND new.field_name = (SELECT FieldName FROM redcap_encryption)
THEN
SET #PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
SET new.value = rsaencrypt(new.value,#PublicKey);
END IF;)
I get syntax errors on this and i'm unsure how best to proceed
Thanks
Resolved this a while ago but forgot to post :-
BEGIN
IF new.field_name = (SELECT FieldName FROM redcap_encryption WHERE new.project_id = ProjectID)
THEN
SET #PublicKey = (SELECT PublicKey FROM redcap_encryption WHERE ProjectID = new.project_id);
SET new.value = rsaencrypt(new.value,#PublicKey);
END IF;
END$$
DELIMITER $$
CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand`
FOR EACH ROW
BEGIN
DECLARE bill_no INT DEFAULT 0;
IF NEW.bill_no = 0 THEN
SET bill_no = (SELECT bill_no FROM cnitrand
WHERE (tx_no = NEW.tx_no AND
input_area = NEW.input_area AND
tx_line_no = NEW.tx_line_no AND
bill_no <> "") OR
(tx_no = NEW.tx_no AND
bill_no <> "")
LIMIT 1);
ELSE
SET bill_no = NEW.bill_no;
END IF;
INSERT INTO tblrptcnitrandmap
(tx_no, input_area, tx_line_no, bill_cust_code, bill_no)
VALUES
(NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no);
END;$$
It returns an 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 14"
Can you please tell me, what is wrong with code..
DELIMITER $$
CREATE TRIGGER update_cnitrand_cust_code AFTER INSERT ON `cnitrand`
FOR EACH ROW
BEGIN
DECLARE bill_no INT DEFAULT 0;
IF NEW.bill_no = 0 THEN
SET bill_no = (SELECT bill_no FROM cnitrand WHERE (tx_no = NEW.tx_no AND input_area = NEW.input_area AND tx_line_no = NEW.tx_line_no AND bill_no <> "") OR (tx_no = NEW.tx_no AND bill_no <> "") LIMIT 1);
ELSE
SET bill_no = NEW.bill_no;
END IF;
INSERT INTO tblrptcnitrandmap (tx_no, input_area, tx_line_no, bill_cust_code, bill_no) VALUES (NEW.tx_no, NEW.input_area, NEW.tx_line_no, NEW.bill_cust_code, bill_no);
END$$
DELIMITER ;
I forget put delimiter directive, some semicolon and was using tsl syntax like [select variable = field] that is not valid in mysql.
Mysql error when you use tsl syntax is [not allowed to return a result set from a function] and dont help much.
#AndreKR point all of it to me, thanks.
Im using mysqlworkbench 5.2.30 CE.
The work function become:
delimiter //
CREATE FUNCTION nextval (seq_name varchar(100))
RETURNS bigint(20)
READS SQL DATA
NOT DETERMINISTIC
BEGIN
DECLARE workval bigint(20);
SELECT count(1) into workval
FROM tip_sequence
WHERE sequencename = seq_name;
IF workval <> 1 THEN
DELETE
FROM tip_sequence
WHERE sequencename = seq_name;
INSERT
INTO tip_sequence (sequencename, sequenceval, sequencestep)
VALUES (seq_name, 1, 1);
END IF;
SELECT sequenceval into workval
FROM tip_sequence
WHERE sequencename = seq_name;
UPDATE tip_sequence
SET sequenceval = sequenceval + sequencestep
WHERE sequencename = seq_name;
RETURN workval;
END//
delimiter ;
I don't think the SELECT workval = count(1) FROM ... syntax is valid. I think what you mean is: SELECT count(1) INTO workval FROM ....