I am creating Trigger into Mysql Workbanch , but not able to create the trigger.
Can Anyone Help me For This.
DELIMITER $$
create TRIGGER Insert_Test
After Delete ON Test
FOR EACH ROW
BEGIN
START TRANSACTION;
DECLARE v_ID INT;
DECLARE v_Error TINYINT;
DECLARE v_AgentID INT;
SELECT v_Error = 0;
v_ID=Old.id;
BEGIN
DELETE Test2 WHERE id = SELECT id FROM Test where id=v_ID;
Rollback;
SET v_Error = 1;
END
IF v_Error = 0;
THEN
COMMIT;
ELSEIF
v_Error = 1;
THEN
ROLLBACK;
END IF;
END
DELIMITER ;
Sql server Trigger
ALTER TRIGGER [dbo].[tr_DelRecordTypeID] ON [dbo].[luRecordType] FOR DELETE
AS
SET NOCOUNT ON
BEGIN TRANSACTION
DECLARE #ID INT, #GroupTypeID INT, #Error BIT, #Msg VARCHAR(500)
SELECT #Error = 0
SELECT #ID = RecordTypeID FROM deleted
SELECT #GroupTypeID = 30
IF EXISTS ( SELECT g.GroupID
FROM luGroup g,
[luGroupDetail] gd
WHERE g.[GroupID] = gd.[GroupID]
AND g.[GroupTypeID] = #GroupTypeID
AND gd.[MemberID] = #ID )
BEGIN
DELETE [agAgent] WHERE [AgentID] = (SELECT TOP 1 AgentID FROM agAgentPayType)
Rollback transaction
SET #Error = 1
END
IF #Error = 0
BEGIN
COMMIT TRANSACTION
END
ELSE
IF #Error = 1
BEGIN
ROLLBACK TRANSACTION
END
This trigger which i am trying to achieve into mysql workbanch, please check
I shall Be thankful,
Thanks
Aman
Related
I am working on a procedure which is doing the following:
With a cursor declared I want to go through all entries of a table to find entries where the given period is covering the one from the cursor.
Example: a student A went to school at grade x from date 3 to date 6. Let's find all who studied with him at least one time for the same class.
The while-loop would be applied to the same table. But I want to just find out if there is at least one entry or not. So the while loop should stop if the first entry appears.
Example: A student B could have visited the same class as A did. But for a year he has been in another class. Just for example.
And here is my problem. I have two variables to set and I get a syntax error.
DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$
CREATE PROCEDURE coworkers(
IN gus INT,
IN rus INT,
OUT gto INT
)
BEGIN
DECLARE recCID INT;
DECLARE recSDT DATE;
DECLARE recEDT DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE ctr INT;
DECLARE cwrk CURSOR FOR
SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET done = TRUE;
OPEN cwrk;
SET ctr = 0;
loop_cwrk: WHILE(ctr<1) DO
FETCH cwrk INTO recCID, recSDT, recEDT;
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1,
SET gto = 1;
IF done THEN
LEAVE loop_cwrk;
END IF;
END WHILE loop_cwrk;
CLOSE cwrk;
end $$
delimiter ;
CALL coworkers(2,1,#gto);
I have tried different formats for the section THEN SET... which is the problem.
Here is the error code:
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 ''ctr' := 1,
SET 'gto' ;= 1;
Here you can see what else I have tried (among others). Probably it's a totally wrong approach.
Btw: the Select within the loop is only a placeholder to get the whole procedure work.
But now, what am I doing wrong?
Thanks in advance.
SOLUTION
DELIMITER $$
DROP PROCEDURE IF EXISTS coworkers$$
CREATE PROCEDURE coworkers(
IN gus INT,
IN rus INT,
OUT gto INT
)
BEGIN
DECLARE recCID INT;
DECLARE recSDT DATE;
DECLARE recEDT DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE ctr INT;
DECLARE cwrk CURSOR FOR
SELECT comp_id, start_date, end_date FROM skill_cv_test WHERE usr_id = rus;
DECLARE CONTINUE HANDLER
FOR NOT FOUND
SET done = TRUE;
OPEN cwrk;
SET ctr = 0;
loop_cwrk: WHILE(ctr<1) DO
FETCH cwrk INTO recCID, recSDT, recEDT;
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT
OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1;
SET gto = 1;
END IF;
IF done THEN
LEAVE loop_cwrk;
END IF;
END WHILE loop_cwrk;
CLOSE cwrk;
end $$
delimiter ;
CALL coworkers(2,1,#gto);
You need add END IF for your first IF
IF EXISTS
(SELECT *
FROM skill_cv_test AS m
WHERE m.usr_Id = gus AND m.usr_id != rus AND (m.start_date < recSDT
OR m.end_date <= recEDT) AND m.comp_id = recCID)
THEN
SET ctr = 1;
SET gto = 1;
END IF; -- Add END IF here
IF done THEN
LEAVE loop_cwrk;
END IF;
DELIMITER $$
CREATE PROCEDURE `remove_schedule_duplicate` ()
BEGIN
BLOCK1 : BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE schedule_id CHAR(36);
DECLARE gamePk INTEGER;
DECLARE keep_entry TINYINT(1);
DECLARE scheduleDuplicate CURSOR FOR SELECT game_pk FROM schedule where is_active = 1 group by game_pk,home_team_id,away_team_id,venue_id having count(game_pk) > 1 limit 2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN scheduleDuplicate;
get_schedule_duplicate: LOOP
FETCH scheduleDuplicate INTO gamePk;
IF finished = 1 THEN
LEAVE get_schedule_duplicate;
END IF;
**BLOCK2 : BEGIN
DECLARE block_finished INTEGER DEFAULT 0;
DECLARE blockDuplicate CURSOR FOR SELECT id FROM schedule where game_pk = gamePk and is_active = 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET block_finished = 1;
OPEN blockDuplicate;
block_schedule_duplicate: LOOP
FETCH blockDuplicate INTO schedule_id;
IF block_finished = 1 THEN
LEAVE block_schedule_duplicate;
END IF;
IF keep_entry = 0 THEN
UPDATE schedule set is_active = 0 where id = schedule_id;
END IF;
END LOOP block_schedule_duplicate;
CLOSE blockDuplicate;
END BLOCK2;**
END LOOP get_schedule_duplicate;
CLOSE scheduleDuplicate;
END BLOCK1;
END
$$
The problem is Innerloop first time execute well after that block_finished always be 1 so. always it quits the inner block.
How to resolve this issue. What i do ? Some one help me to resolve the issue.
I have Changed My Stored procedure like this:
DELIMITER $$
CREATE PROCEDURE `remove_schedule_duplicate`()
BEGIN
DECLARE finished INTEGER DEFAULT 0;
DECLARE schedule_id,temp CHAR(36) DEFAULT NULL;
DECLARE gamePk INTEGER;
DECLARE keep_entry TINYINT(1);
DECLARE scheduleDuplicate CURSOR FOR SELECT game_pk,id FROM schedule where game_pk in (SELECT game_pk FROM schedule where is_active = 1 group by game_pk,home_team_id,away_team_id,venue_id having count(game_pk) > 1) and is_active = 1 order by game_pk;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
OPEN scheduleDuplicate;
get_schedule_duplicate: LOOP
FETCH scheduleDuplicate INTO gamePk,schedule_id;
IF finished = 1 THEN
LEAVE get_schedule_duplicate;
END IF;
IF ((temp IS NULL) AND (gamePk IS NOT NULL)) OR ((temp IS NOT NULL) AND (temp <> gamePk)) THEN
SET temp = gamePk;
SET keep_entry = 1;
#ELSE IF temp IS NOT NULL AND temp = gamePk THEN
ELSE
SET keep_entry = 0;
END IF;
IF keep_entry = 0 THEN
UPDATE schedule set is_active = 0 where id = schedule_id;
END IF;
END LOOP get_schedule_duplicate;
CLOSE scheduleDuplicate;
END$$
DELIMITER ;
I am making a procedure that inserts a place ("Sted") and I would like to check if the inputs are NULL. However, whenever I try to add an if-statement at the start to surround my code (marked CRASH below), it gives me an error saying my syntax is not right at "DECLARE varStedskodeID INT;" which is the part after the IF-statement I'm trying to add.
To my eyes the syntax of my if-statement is the same inside the code, but only my soon-to-be-NULL-check if-statement crashes even with just a simple IF(TRUE) THEN.
Can anyone give me a hint of what causes this one if to crash?
DROP PROCEDURE IF EXISTS InsertSted;
DELIMITER $$
CREATE PROCEDURE InsertSted(
IN inputStedsnavn VARCHAR(255),
IN inputStedstype VARCHAR(255),
IN inputKommunenavn VARCHAR(255))
BEGIN
IF(TRUE) THEN <<------ CRASH
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF; <<------ CRASH
END$$
DELIMITER ;
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
http://dev.mysql.com/doc/refman/5.0/en/declare.html
MySQL follows strict rules for DECLARE. You have to DECLARE variables, tables, etc... at the beginning of Stored Procedure.
Change Stored Procedure like this
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(TRUE) THEN
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF;
I wrote a trigger which get trigered after update statement of table1. If there is no row in table1 based on the conditions, then update table2.
CREATE TRIGGER trigger1
AFTER UPDATE ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF new.column1 = 1 THEN
SELECT COUNT(1) INTO #cnt FROM my_database.table1
WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2
SET isDeleted = 1
WHERE userSeqID = new.userID;
END IF
It gives me error i nline number 4, I couldn't understand the problem
Every IF statement has to ended with END IF clause. Try this statement -
DELIMITER $$
CREATE TRIGGER trigger1
AFTER UPDATE
ON my_database.table1
FOR EACH ROW
BEGIN
DECLARE cnt int;
IF NEW.column1 = 1 THEN
SELECT COUNT(1) INTO cnt FROM my_database.table1 WHERE userID = 1 AND isDeleted = 0;
IF cnt = 0 THEN
UPDATE my_database.table2 SET isDeleted = 1 WHERE userSeqID = NEW.userID;
END IF;
END IF;
END$$
DELIMITER ;
The error on line 4 is because you're not using a delimiter.
Try this:
DELIMITER //
create trigger trigger1 after update on my_database.table1
for each row
begin
declare cnt int;
...
end if;
END;//
DELIMITER ;
You'll also need an END IF on the line after the select statement, and a terminal END; to match your begin, as I have in my example.
when the table user delete fail,why the next sql will be exceute.who can give me a full example for mysql procedure.i want to delete example.please help?
use test;
delimiter //
create procedure proc_name(in paramter int)
begin
declare t_error int default 0;
declare continue handler for sqlexception set t_error=1;
set autocommit = 0;
START TRANSACTION;
delete from `user` where id = paramter;
delete from `user_info` where uid = paramter;
if t_error=1 then
rollback;
else
commit;
end if;
end;
//
delimiter ;
Dinel, I would change a little bit of your logic to achieve what you are looking for:
use test;
delimiter //
create procedure proc_name(in paramter int)
begin
declare continue handler for sqlexception set t_error=1;
SET #t_error = 0;
START TRANSACTION;
delete from `user` where id = paramter;
SELECT #t_error := COUNT(*) from `user` where id = paramter;
if #t_error != 0 then
ROLLBACK;
else
DELETE FROM `user_info` WHERE uid = paramter;
COMMIT;
end if;
end;
//
delimiter ;
I didn't try the syntax, but it should work. ;-)