To quite simply put how my SP looks it is basically like this atleast syntax wise
DELIMITER $$
DROP PROCEDURE IF EXISTS `info_insert_or_update` $$
CREATE PROCEDURE `info_insert_or_update` (
IN in_id bigint,
IN in_name varchar(150),
IN in_details varchar(150))
START TRANSACTION;
INSERT INTO infos (id, name)
VALUES (in_id, in_name)
ON DUPLICATE KEY UPDATE name = in_name;
INSERT INTO details (details_id, details)
VALUES(in_id,
in_details)
ON DUPLICATE KEY UPDATE details = in_details;
COMMIT;
END$$
DELIMITER ;
With this the problem is that it cant recognize the in_ variables and i understand that that is because i need an compound statement with BEGIN END around everything but where ever i seem to put it it is something wrong with the syntax. So what is the correct syntax when i got this type of SP with in parameters that then has an transaction? (want transaction as i will add rollback onto it as well)
You are missing datatype for the in_details-parameter and you are missing the starting BEGIN
DELIMITER $$
DROP PROCEDURE IF EXISTS `info_insert_or_update`
$$
CREATE PROCEDURE `info_insert_or_update` (
in_id bigint,
in_name varchar(150),
in_details varchar(150)
)
BEGIN
START TRANSACTION;
INSERT INTO infos (id, name)
VALUES (in_id, in_name)
ON DUPLICATE KEY UPDATE name = in_name;
INSERT INTO details (details_id, details)
VALUES(in_id, in_details)
ON DUPLICATE KEY UPDATE details = in_details;
COMMIT;
END
$$
I'm a noob im MYSQL and I'm trying to make a trigger who will auto-fill 2 fields(customername, customersurname) in an invoice table. Can anyoane explain me please what I do wrong in that trigger? Thank you very much! :)
CREATE TABLE customer(
customerID INT(6) PRIMARY KEY AUTO_INCREMENT,
customername VARCHAR(20) NOT NULL,
customersurname VARCHAR(20) NOT NULL,
customeraddress VARCHAR(200)
);
CREATE TABLE invoice(
invoiceID INT (6) Primary KEY AUTO_INCREMENT,
customerID INT (6) REFERENCES customer(customerID),
customername VARCHAR(20) REFERENCES customer(customername),
invoicedate DATE,
orderID INT(6) REFERENCES orders(orderID),
products VARCHAR(200) REFERENCES orderlines(productname),
FOREIGN KEY(orderID) REFERENCES orders(orderID)
);
CREATE TRIGGER autofill_invoice BEFORE INSERT ON invoice FOR EACH ROW
BEGIN
IF (new.customerID = customer.customerID,
new.customersurname = customer.customersurname )
THEN
SET new.customername = customer.customername,
new.customersurname = customer.customersurname;
END IF;
END;
As mysql documentation on defining stored programs explains:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The
following example shows how to do this for the dorepeat() procedure
just shown. The delimiter is changed to // to enable the entire
definition to be passed to the server as a single statement, and then
restored to ; before invoking the procedure. This enables the ;
delimiter used in the procedure body to be passed through to the
server rather than being interpreted by mysql itself.
So, use the delimiter command when you define the stored procedure:
DELIMITER //
CREATE TRIGGER autofill_invoice BEFORE INSERT ON invoice FOR EACH ROW
BEGIN
IF (new.customerID = customer.customerID,
new.customersurname = customer.customersurname )
THEN
SET new.customername = customer.customername,
new.customersurname = customer.customersurname;
END IF;
END//
DELIMITER ;
Next time pls provide exact error description in your question!
I am trying to create a stored procedure in MYSQL that populates a field if left blank while creating the record. Specifically we have an application that uses a CC field to populate e-mail address when a new ticket is created.
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `cc_update`()
BEGIN
SELECT * FROM `ehelpdesk`.`ticket` WHERE `CC` = 'Null';
UPDATE `ehelpdesk`.`ticket` SET `CC`='email#mail.com';
END
What i'm not figuring out is how to call the procedure when a new ticket or 'ID' is created.
Here's the full code I used to create the trigger
DELIMITER $$
CREATE TRIGGER `ins_cc` BEFORE INSERT ON `ticket` FOR EACH ROW BEGIN
IF (NEW.CC IS NULL) THEN
SET NEW.CC='email.string';
End If;
end$$
DELIMITER ;
You could create a trigger, which is be execute each time a record is inserted to the ehelpdesk table:
CREATE TRIGGER `ticket_BINS` BEFORE INSERT ON `ticket` FOR EACH ROW
BEGIN
IF (NEW.CC IS NULL) THEN
SET NEW.CC= "email#mail.com";
END IF;
END;
For what you are doing, ensure that CC is not null, I 'd suggest setting the default value of ticket.CC to email#mail.com
ALTER TABLE `ehelpdesk`.`ticket`
CHANGE COLUMN `CC` `CC` VARCHAR(45) NULL DEFAULT 'email#mail.com' ;
Your stored procedure is not correct
CREATE DEFINER=`root`#`localhost` PROCEDURE `cc_update`()
BEGIN
UPDATE `ehelpdesk`.`ticket` SET `CC`='email#mail.com' WHERE `CC` IS NULL;
END
i have this table
create table eveniment( +
evenimentId bigint not null auto_increment primary key,
evenimentDenumire varchar(500),
adresaId int not null);
create table adresa(
adresaId bigint not null auto_increment primary key,
localitate varchar(500),
judet varchar(500),
codPostal varchar(50),
strada varchar(500),
nr varchar(50),
bl varchar(50),
ap varchar(5),
email varchar(500),
www varchar(500));
and this procedure
DELIMITER //
drop procedure IF EXISTS insertEveniment;
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
and i get this error
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 'insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www) values(' at line 4
if i try the insert into adresa ... separately in mysql command i don't get error , what i do wrong?
Try like this (tried and it works for me):
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0;
DECLARE vEvenimentId int DEFAULT 0;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
You are using drop procedure statement with wrong delimiter (you were setting delimiter to // but then you put ; at the end of your statement. Put your drop procedure statement before you set the delimiter, and it will work just fine. Also, put delimiters after DECLARE
DECLARE is a separate statement, thus it needs to be delimited with a ;, like INSERT or SELECT or any other statement.
Also, move the first DELIMITER statement to just before the beginning of the procedure declaration.
So,
DELIMITER //
drop procedure IF EXISTS insertEveniment;
DELIMITER //
CREATE PROCEDURE insertEveniment()
BEGIN
DECLARE vAdresaEvenimentId int DEFAULT 0 ;
insert into adresa(judet,localitate,codPostal,strada,nr,bl,ap,email,www)
values('judet','localitate','cod postal','strada','numar','bloc','ap','email','www');
select last_insert_id() into vAdresaEvenimentId;
DECLARE vEvenimentId int DEFAULT 0 ;
insert into eveniment(evenimentDenumire,adresaId) values('concurs informatic 1',vAdresaEvenimentId);
select last_insert_id() into vEvenimentId;
END //
DELIMITER ;
call insertEveniment();
Getting the warning 0 row(s) affected, 1 warning(s):
1072 Key column 'name' doesn't exist in table And I do not know what it means. Does anybody have an explaination?
The table/SP is as follows
CREATE TABLE IF NOT EXISTS `sectors`
(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`sector` VARCHAR(25) NOT NULL ,
--
PRIMARY KEY (`id`) ,
UNIQUE INDEX `sector_idx` USING BTREE (`sector` ASC)
);
DELIMITER $$
CREATE PROCEDURE `AddSector` (IN sector VARCHAR(25),
OUT result BOOLEAN)
MODIFIES SQL DATA
BEGIN
DECLARE CONTINUE HANDLER FOR SQLWARNING, SQLEXCEPTION SET result = FALSE;
SET result = TRUE;
--
INSERT INTO `sectors` (`sector`) VALUES (sector);
COMMIT;
END $$
Change the procedure to:
DELIMITER $$
CREATE PROCEDURE `AddSector` (IN pSector VARCHAR(25), <<-- fix nameclash here
OUT result BOOLEAN)
MODIFIES SQL DATA
BEGIN
DECLARE CONTINUE HANDLER FOR SQLWARNING, SQLEXCEPTION SET result = FALSE;
SET result = TRUE;
INSERT INTO `sectors` (`sector`) VALUES (pSector); <<-- and here
-- COMMIT; <<-- You don't need a commit.
END $$
All stuff inside a stored proc is already run inside an implicit transaction, so the commit is not needed, and may actually be an error (not sure).
DELIMITER ;