Error in creating procedure in MySQL - 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 ;

Related

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;

How do I create a stored procedure in MySQL that will select data and insert new values?

Hi I am trying to create a database that will check out document numbers per project. I am trying to keep the db server action limited to stored procedures.
The numbering system goes
XXXX-YY-ZZZZ
XXXX is the job number
YY is the document type table reference
ZZZZ is the document specific number and increments from 1 up for each type in each job...
Because of the numbering system, I can not use an auto incremented column... That would be nice... I would have to generate a table for each document type, for each job... unless anyone sees anything I do not? I am pretty new at this, so any help would be appreciated.
Here is what I have for my procedure that I am having issues with.
DELIMITER $$
DROP PROCEDURE IF EXISTS ADD_NEW_DOC$$
CREATE DEFINER=RMDNA#localhost PROCEDURE ADD_NEW_DOC(IN in_job INT, IN in_type VARCHAR(2), IN in_name VARCHAR(32), IN in_desc VARCHAR(512))
BEGIN
SELECT MAX(doc_id) INTO #highnum FROM Doc_Entries WHERE job_id = in_job AND doc_type_ID = in_type$$
IF #highnum = null THEN SET #newnum = 1$$
ELSE SET #newnum = #highnum + 1$$
END IF$$
insert into Doc_Entries (job_id, doc_type_ID, doc_id, doc_name, doc_desc) values (in_job, in_type, #newnum, in_name, in_desc)$$
SELECT #newnum$$
END$$
DELIMITER ;
and the error I am getting is:
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 2
UPDATE:
My attempt has refined...
DROP PROCEDURE IF EXISTS ADD_NEW_DOC;
DELIMITER //
CREATE DEFINER=RMDNA#localhost PROCEDURE ADD_NEW_DOC(IN in_job INT, IN in_type VARCHAR(2), IN in_name VARCHAR(32), IN in_desc VARCHAR(512))
BEGIN
SELECT MAX(doc_id) INTO #highnum FROM Doc_Entries WHERE job_id = in_job AND doc_type_ID = in_type;
IF (#highnum is null) THEN SET #newnum = 1;
ELSE SET #newnum = #highnum + 1;
END IF;
insert into Doc_Entries (job_id, doc_type_ID, doc_id, doc_name, doc_desc) values (in_job, in_type, #newnum, in_name, in_desc);
SELECT #newnum;
END//
DELIMITER ;
Working good now. Learned how to use delimiter properly and fixed a few things that then became obvious.

Sql syntax error in procedure

I am writing one procedure in order to get data from database .
Here is my procedure .
CREATE DEFINER=`root`#`localhost` PROCEDURE `getCdrListnew`(from_date DATETIME, to_date DATETIME,
p_category VARCHAR(255), p_callType VARCHAR(255), p_businessId VARCHAR(255), p_dnc VARCHAR(255),
p_cli VARCHAR(255), p_dni VARCHAR(255), p_callNumber VARCHAR(255), p_dialType VARCHAR(255), p_contractType VARCHAR(255) )
BEGIN
DECLARE a INT ;
DECLARE setFilter INT DEFAULT FALSE;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET a = 0;
BEGIN
END;
SET #sqlCalls = 'SELECT agentcdr.isdnCauseCode,COUNT(*) as count FROM cdr';
-- SET #sqlCalls= CONCAT(#sqlCalls,' LEFT JOIN agentcdr ON cdr.idCDR=agentcdr.cdr_idCDR ');
IF( (from_date IS NOT NULL AND from_date != '')&& (to_date IS NOT NULL AND to_date != '') ) THEN
SET #sqlCalls= CONCAT(#sqlCalls,' WHERE cdr.createdDt >=\'',from_date,'\' AND cdr.createdDT<=', '\'',to_date,'\'');
SET setFilter = TRUE;
END IF;
SET #sqlCalls= CONCAT(#sqlCalls,' LEFT JOIN agentcdr ON cdr.idCDR=agentcdr.cdr_idCDR GROUP BY agentcdr.isdnCauseCode ');
PREPARE prepsqlstr1 FROM #sqlCalls;
EXECUTE prepsqlstr1;
END
When I am executing the above code I am getting an error as below .
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 'LEFT JOIN agentcdr ON cdr.idCDR=agentcdr.cdr_idCDR GROUP BY agentcdr.isdnCauseCo' at line 1
I know this is the syntax problem but not able to figure out what I am doing wrong here .
Please help me out .
ALso do let me know if there any way to print the query which is executing in procedure for debugging purpose .
Looks like you have a wrong order in the generated statement:
You get a select ... where ... left join .... This is wrong syntax.

Compare dates in MySQL trigger

I have two tables, round and event. One round has many events.
create table round (
round_id INTEGER AUTO_INCREMENT NOT NULL,
round_start_date DATETIME NOT NULL,
round_end_date DATETIME NOT NULL,
CONSTRAINT round_pk PRIMARY KEY (round_id)
);
create table event (
event_id INTEGER AUTO_INCREMENT NOT NULL,
round_id INTEGER NOT NULL,
event_date DATETIME NOT NULL,
CONSTRAINT event_pk PRIMARY KEY (event_id),
CONSTRAINT round_fk FOREIGN KEY (round_id) REFERENCES round (round_id),
);
When a row is inserted into the event table, I want to use a trigger to compare the event_date field of the newly inserted row to the round_start_date and round_end_date fields in its corresponding entry in the round table. If event_date is earlier than round_start_date, round_start_date should be updated with the new event_date. If event_date is after round_end_date, round_end_date should be updated with the new event_date.
This is my trigger. It does not work, and I do not understand why. I cannot find anywhere on the web where anyone else has tried to use a datetime type in a trigger, so I have no frame of reference for where I am going wrong.
create trigger update_round_date
after insert on event for each row
begin
declare curSdate datetime;
declare curEdate datetime;
set curSdate = (select round_start_date from round where round_id = NEW.round_id);
set curEdate = (select round_end_date from round where round_id = NEW.round_id);
if (NEW.event_date < curSdate) then
update round set round_start_date = NEW.event_date where round_id = NEW.round_id;
else if (NEW.event_date > curEdate) then
update round set round_end_date = NEW.event_date where round_id = NEW.round_id;
end if;
end;
EDIT: I simply can't create the trigger. phpMyAdmin gives me this 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 4"
EDIT 2: Updated with a delimiter set
DELIMITER $$
create trigger update_round_date
after insert on event for each row
begin
declare curSdate datetime;
declare curEdate datetime;
set curSdate = (select round_start_date from round where round_id = NEW.round_id);
set curEdate = (select round_end_date from round where round_id = NEW.round_id);
if (NEW.event_date < curSdate) then
update round set round_start_date = NEW.event_date where round_id = NEW.round_id;
else if (NEW.event_date > curEdate) then
update round set round_end_date = NEW.event_date where round_id = NEW.round_id;
end if;
end$$
This returns 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 '' at line 13"
MySQL is probably stopping at the first ';', interpreting your command as:
create trigger update_round_date
after insert on event for each row
begin
declare curSdate datetime;
You have to set your delimiter to something else first, then terminate the create trigger command with that delimiter instead (and put the delimiter back at the end):
delimiter ^
create trigger update_round_date
after insert on event for each row
begin
...
end;
^
delimiter ;
I believe the last semicolon after end may be necessary.
There may be a problem with delimiters in phpmyadmin, try to use this trigger -
CREATE TRIGGER trigger1
AFTER INSERT
ON event
FOR EACH ROW
UPDATE
round
SET
round_start_date =
IF(NEW.event_date < round_start_date, NEW.event_date, round_start_date),
round_end_date =
IF(NEW.event_date > round_end_date, NEW.event_date, round_end_date)
WHERE
round_id = NEW.round_id;

MySQL Stored Procedure Declare Issue

This problem has cost me almost an hour now and I know it is something simple.
I am 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 'IN VARCHAR(256), hl7PatientId IN VARCHAR(256))
BEGIN
DECLARE mainQueue INT' at line 1
Here is my query which looks right to me:
DROP PROCEDURE IF EXISTS insert_data;
CREATE PROCEDURE `insert_data`(hl7PatientName IN VARCHAR(256), hl7PatientId IN VARCHAR(256))
BEGIN
DECLARE mainQueue INT DEFAULT 1;
SELECT `queueid` INTO mainQueue FROM `queues` WHERE `description` LIKE 'Main' AND `enabled` = 1 LIMIT 1;
INSERT INTO `queue_data`
(`queueid`, `patientname`, `patientid`, `location`, `creationtime`, `priority`)
VALUES
(mainQueue, hl7PatientName, hl7PatientId, 'QUEUE_NUMBER', TIMESTAMP(), '');
END;
I am using MySQL 5.0.77 for this.
Can anybody see anything in this that is wrong?
i've tidied up your example a little - note the use of delimiter and in params !
drop procedure if exists insert_queue_data;
delimiter #
create procedure insert_queue_data
(
in p_patientname varchar(255), -- size ? i always prefix my params p_ and keep the same name as the db field
in p_patientid varchar(255) -- size ? are you sure this isnt an integer ?
)
begin
-- i always prefix my variables v_ and keep same name as the db field
declare v_queueid int unsigned default 1;
select queueid into v_queueid from queues where
description like 'Main' and enabled = 1 limit 1;
insert into queue_data(queueid, patientname, patientid, location, creationtime, priority) values
(v_queueid, p_patientname, p_patientid, 'QUEUE_NUMBER', now(), '');
end#
delimiter ;
Reverse the order of IN and parameter name.
...(IN hl7PatientName VARCHAR(256), IN hl7PatientId VARCHAR(256))...