mysql, syntax issue in procedure - mysql

I have problems with syntax in mysql procedure
CREATE PROCEDURE savep(
IN p_uuid VARCHAR(36),
IN p_sp INTEGER,
IN p_cd VARCHAR(250)
)
BEGIN
INSERT INTO maintable
(
uuid,
sp,
cd,
last_time_saved
)
VALUES
(
p_uuid,
p_sp,
p_cd,
now()
)
ON DUPLICATE KEY UPDATE
sp = VALUES(p_sp),
cd = VALUES(p_cd),
cd = VALUES(now()); -- syntaxerror, unexcepted NOW_SYM
END -- syntax error, unexcepted END
what am i doing wrong? uuid is a primary key in the main table.

Two minor things I can see.
You have no delimiter set at the top (but you might have that in the version you are trying to use).
Secondly you are setting cd twice in the on duplicate key clause, and one of those is setting it to VALUES(now()) , where what should be in the brackets is the name of a column whose value from the VALUES clause of the insert that you are setting it to.
Try this:-
DELIMITER //
CREATE PROCEDURE savep(
IN p_uuid VARCHAR(36),
IN p_sp INTEGER,
IN p_cd VARCHAR(250)
)
BEGIN
INSERT INTO maintable
(
uuid,
sp,
cd,
last_time_saved
)
VALUES
(
p_uuid,
p_sp,
p_cd,
now()
)
ON DUPLICATE KEY UPDATE
sp = VALUES(p_sp),
cd = VALUES(p_cd),
last_time_saved = now();
END

Related

Unable to diagnose the problem with MySQL stored procedure

I have defined the following stored procedure to add/update a table called ImportedProduct.
If the primary key, ImportedProductId is provided and is greater than zero, then update the exiting record otherwise insert a new one:
DELIMITER //
CREATE PROCEDURE AddOrUpdateImportedProduct (
IN ImportedProductId BIGINT,
IN UniqueThirdPartyCode VARCHAR(64),
IN BranchId BIGINT
)
BEGIN
IF ImportedProductId <= 0 THEN
INSERT INTO ImportedProduct(UniqueThirdPartyCode, BranchId)
VALUES(UniqueThirdPartyCode, BranchId);
ELSE
UPDATE
ImportedProduct
SET
UniqueThirdPartyCode = UniqueThirdPartyCode,
BranchId = BranchId
WHERE
ImportedProductId = ImportedProductId;
END IF;
END //
DELIMITER ;
Now I run the following code to update an existing row:
CALL AddOrUpdateImportedProduct (1, 'y-105', 24);
I can see that the record with with ImportedProductId = 1 exists in the table, but I am getting the following error:
You are using safe update mode and you tried to update a table without
a WHERE that uses a KEY column To disable safe mode
I am pretty sure ImportedProductId = ImportedProductId holds always.. Perhaps rename your variable or add an alias to the updated table.

What is the issue with this trigger (for the right syntax to use near '' )

Please I did this trigger to update table rating after each insert when 2 columns (id_prof,id_etud) inserted are already in the table but it gives mi this error
> #1064 - 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 '' at line 6
The trigger :
CREATE TRIGGER Before_Insert_Rate
BEFORE INSERT ON rating
FOR EACH ROW
BEGIN
IF (EXISTS(SELECT * FROM rating WHERE id_prof=NEW.id_prof and id_etud = NEW.id_etud)) THEN
UPDATE `rating` SET `rate` = NEW.rate WHERE `id_prof` = NEW.id_prof and `id_etud` = NEW.id_etud;
ELSE INSERT INTO rating VALUES (NEW.idprof,New.rate,New.id_etud);
END IF
END
DELIMITER ;
You must set the DELIMITER before. Change it to:
DELIMITER //
CREATE TRIGGER Before_Insert_Rate
BEFORE INSERT ON rating
FOR EACH ROW
BEGIN
IF (EXISTS(SELECT * FROM rating WHERE id_prof=NEW.id_prof and id_etud = NEW.id_etud)) THEN
UPDATE `rating` SET `rate` = NEW.rate WHERE `id_prof` = NEW.id_prof and `id_etud` = NEW.id_etud;
ELSE INSERT INTO rating VALUES (NEW.idprof,New.rate,New.id_etud);
END IF
END; //
DELIMITER ;
from the official reference manual.
A trigger can access both old and new data in its own table. A trigger
can also affect other tables, but it is not permitted to modify a
table that is already being used (for reading or writing) by the
statement that invoked the function or trigger.
This means no query on table X can INSERT, UPDATE, or DELETE from table X; it also means that if a trigger on table X modifies table Y, it will fail for any query using both table X and Y that "triggers" it.
Example: UPDATE x INNER JOIN y ON x.id = y.id SET x.something = 1, y.something = 2 will cause a BEFORE UPDATE ON x trigger (that updates, inserts, or deletes from y) to fail.
I solved it using ON DUPLICATE KEY on a query instead of trigger
CREATE TABLE `rating` (
`id_prof` int(11) NOT NULL,
`rate` float NOT NULL,
`id_etud` int(11) NOT NULL,
UNIQUE (id_prof,id_etud)
)
the query :
INSERT INTO rating (id_prof,rate,id_etud) VALUES (1,2,5)
ON DUPLICATE KEY UPDATE rate=2

Error in creating procedure in MySQL

What may be the possibilities of getting an error in the following query?
DELIMITER $$
CREATE PROCEDURE `tbl_assessment_notes`(
`var_reason` VARCHAR,
'var_attr2' VARCHAR,
'var_note' VARCHAR
)
BEGIN
IF EXISTS
(
SELECT
*
FROM
tbl_assessment_notes
WHERE
reason = var_reason AND attr2 = var_attr2
) THEN
UPDATE
tbl_assessment_notes
SET
note = CONCAT(note, var_note),
TIMESTAMP = 'CURRENT_TIMESTAMP'
WHERE
attr1 = var_attr1 AND reason = var_reason ELSE
INSERT
INTO
tbl_assessment_notes(
pk_assess_note_id,
attr2,
attr3,
reason,
note,
TIMESTAMP
)
VALUES(
NULL,
var_attr2,
NULL,
'confirmation',
var_note,
'CURRENT_TIMESTAMP'
) ;
END IF ;
END $$
DELIMITER ;
I'm getting 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 '
'var_reason' VARCHAR,
'var_attr2' VARCHAR,
`var_note` VARCHAR,
'var' at line 2
Basically what I'm trying to do is to update a row if it exists or else creates a new row and insert values into it.
Please use updated query
DELIMITER $$
CREATE PROCEDURE `tbl_assessment_notes`
(var_reason VARCHAR(255),
var_attr2 VARCHAR(255),
var_note VARCHAR(255))
BEGIN
IF EXISTS
(
SELECT
*
FROM
tbl_assessment_notes
WHERE
reason = var_reason AND attr2 = var_attr2 ) THEN UPDATE tbl_assessment_notes SET note = CONCAT(note, var_note), TIMESTAMP
= 'CURRENT_TIMESTAMP' WHERE attr1 = var_attr1 AND reason = var_reason ELSE INSERT INTO tbl_assessment_notes(
pk_assess_note_id,
attr2,
attr3,
reason,
note,
TIMESTAMP) VALUES(NULL,var_attr2,NULL,'confirmation', var_note, 'CURRENT_TIMESTAMP' ) ;
END IF ;
END $$
DELIMITER ;

MySQL - Update a column after information get's inserted

I have a table in MySql table like the following
row_timestamp timestamp
, row_id int(11) auto_increment
, mrn char(17)
, patients_last_name varchar(50)
, patients_first_name varchar(50)
, ssn char(4) default '0000'
, visit_key NULL
upon the insertion of a record, I'd like visit_key to bet set to visit_key = concat(mrn, row_id) I was trying to accomplish this with a before insert trigger to no avail, I kept getting that the mrn column was not in the field select list.
Update
I tried the following, which seems not to work because the auto_increment has not yet incremented:
set new.visit_key = concat(new.mrn, new.row_id)
I also tried
set new.visit_key = concat(new.mrn, max(row_id)+1)
I am thinking of the trigger to sort of act like a calculated field in MS Access, is this reasonable? Thoughts? Would it not be possible to do since the visit_key would technically be NULL and you cannot update a new value?
UPDATE
I used the following code that I adapted from this question here
DELIMITER //
CREATE TRIGGER vkt AFTER INSERT ON demographic_information
FOR EACH ROW
BEGIN
UPDATE `demographic_information` SET visit_key_test = concat(new.mrn, mew.row_id) WHERE row_id = NEW.row_id;
END;
//
DELIMITER ;
and got the following error message:
INSERT INTO `manchuco_nys_trauma`.`demographic_information` (
`row_timestamp` ,
`row_id` ,
`mrn` ,
`patients_last_name` ,
`patients_first_name` ,
`ssn` ,
`visit_id` ,
`visit_key_test`
)
VALUES (
CURRENT_TIMESTAMP , NULL , '123456', 'Sanderson', 'Steven', '1234', '12345670', NULL
)
MySQL said: Documentation
#1442 - Can't update table 'demographic_information' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
Thank you,
What you observe seems normal: since you're using BEFORE INSERT the id value doen't exist yet.
And the same applies for your try with concat(new.mrn, **new**.row_id): NEW has no sense at the moment.
So I suggest you use AFTER INSERT instead.
Try selecting the auto increment value inside the trigger:
CREATE TRIGGER trigger_name AFTER INSERT ON yourTable FOR EACH ROW
BEGIN
DECLARE next_id integer;
SET next_id = (SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA='yourDB' AND TABLE_NAME='yourTable');
SET new.visit_key = CONCAT(new.mrn, next_id);
END
I discovered this approach in this SO article.

MySql procedure insert if foreign key not exist

first of all my english is not so good but i think maybe enogh. Ok, I have a procedure for inserting.
Procedure is
CREATE PROCEDURE `PhotoUpdate`(IN `uid` INT, IN `foto` VARCHAR(255))
BEGIN
INSERT INTO photos (Dosya, UyeID)
VALUES (foto, uid)
ON DUPLICATE KEY UPDATE UyeID = uid;
END
And Im calling it like this
Call PhotoUpdate(87,'87_54284.jpg');
Photos table is
id Dosya UyeID
1 55_48615.jpg 55
2 87_95165.jpg 87
Im trying to ('uid'->87) if uid equals to photos.UyeID, I mean if UyeID=87 then Update row. If else then insert.
This querys that i tryd. But didnt work.
IF EXISTS (SELECT ID FROM photos WHERE UyeID = uid)
UPDATE photos SET Dosya = foto WHERE UyeID = uid;
ELSE
INSERT INTO photos (Dosya, UyeID) Values(foto, uid);
END IF;
How can i do this guys?
Alternatively:
DELIMITER $$
DROP PROCEDURE IF EXISTS `PhotoUpdate`$$
CREATE PROCEDURE `PhotoUpdate`(`uid` INT UNSIGNED, `foto` VARCHAR(255))
BEGIN
INSERT INTO `photos` (`Dosya`, `UyeID`)
VALUES (`foto`, `uid`)
ON DUPLICATE KEY UPDATE `Dosya` = `foto`;
END$$
DELIMITER ;
SQL Fiddle demo
Try REPLACE INTO photos (Dosya, UyeID) VALUES(foto, uid);
If you create an unique index on UyeID, or even better get rid of the ID column and make UyeID the primary key, you can use INSERT .... ON DUPLICATE KEY UPDATE.
Example
INSERT INTO photos (Dosya, UyeID)
VALUES ('example.jpg', 87)
ON DUPLICATE KEY UPDATE
Dosya = VALUES(Dosya)
If you use VALUES you can use this also with a bulk insert