I have the following SQL update trigger that works properly:
BEGIN
DECLARE myID INT;
SELECT user_id INTO myID FROM writer WHERE writer_id = NEW.writer_id;
IF (NEW.status_id = 2) THEN
INSERT INTO activity (
user_id,
work_id,
activity,
date_created
) VALUES (
myID,
NEW.work_id,
'confirmed',
now()
);
ELSE
INSERT INTO activity (
user_id,
work_id,
activity,
date_created
) VALUES (
myID,
NEW.work_id,
'modified',
now()
);
END IF;
END
I need to add an additional trigger as the following:
CREATE TRIGGER updateWorkStatus AFTER UPDATE ON writer_split
FOR EACH ROW
BEGIN
UPDATE work a
JOIN writer_split b
ON a.work_id = b.work_id AND a.current_version = b.version
SET a.status_id = 2
WHERE a.work_id NOT IN (
SELECT ab.work_id
FROM (SELECT s.work_id
FROM work w INNER JOIN writer_split s
ON w.work_id = s.work_id AND s.status_id != 2) ab
);
END;
when I run this create script, I am getting a syntax error. Any ideas?
For me i will use UPDATE work as a
Related
i need help for the below code. My problem is that i want only that code executes once per statement (after i search i checked that expression don't exists anymore only once per row).
So i tried to add:
IF NOT EXISTS
(Select count(*) FROM replay_replays_access WHERE id_game = new.id_game GROUP BY id_game HAVING count(*) <5)
THEN
But didn't work either what can i do, its duplicating sometime triplicating the information?
TRIGGER replay
AFTER UPDATE
ON table_replays FOR EACH ROW
begin
IF EXISTS
(SELECT
replay_games.room_name
FROM replay_games
WHERE replay_games.room_name = 'Tournament Room' and replay_games.id = new.id_game)
THEN
IF NOT EXISTS
(Select
count(*)
FROM replay_replays_access
WHERE id_game = new.id_game
GROUP BY id_game
HAVING count(*) <5)
THEN
INSERT INTO replay_replays_access(id_game, id_player, replay_name, do_not_hide)
SELECT
new.id_game,
replay_users.id ,
CONCAT(
(SELECT game_types
FROM replay_games
WHERE id=new.id_game),
': ',
(SELECT
descr
FROM replay_games
WHERE id=new.id_game)) ,
0
FROM replay_users
WHERE
(replay_users.admin > 0 OR
replay_users.privlevel = 'TOURNAMENT MEMBER')
AND NOT replay_users.name = (
SELECT
replay_games.creator_name
FROM replay_games
WHERE replay_games.id = new.id_game);
END IF;
END IF;
END
I am using MYSQL version 5.5
I am trying to insert the following procedure:
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
(INSERT INTO `tbl_search_counter` (`user_id` ,`time_searched`) VALUES ('165', '7'));
END IF
But for whatever reason I get the following error message
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 'INSERT INTO tbl_search_counter (user_id ,time_searched)
VALUE' at line 6
I really don't understand because when I do the following request I do not experience problem
CREATE PROCEDURE `myTestProceed2ure` ( IN `_id` INT )
INSERT INTO `tbl_search_counter` (`user_id`, `time_searched`) VALUES ('15', '7')
or the following
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
END IF
If someone has any idea what I am doing wrong, let me know...
Thanks
What is your stored procedure supposed to be doing? You are not referencing the parameter to the stored procedure and u_userid seems undefined.
If you just want to insert an id that doesn't currently exist, then here is one way:
CREATE PROCEDURE `myTestProceed3ure` (IN _id INT )
INSERT INTO tbl_search_counter(user_id, time_searched)
VALUES (_id, '7')
ON DUPLICATE KEY UPDATE user_id = values(user_id)
END IF;
Final answer: I should not have put () before the INSERT...
CREATE PROCEDURE `myTestProceed3ure` ( IN _id INT )
IF( SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid ) >0
THEN
(SELECT COUNT( * ) FROM `tbl_search_counter` WHERE user_id = u_userid);
ELSE
INSERT INTO `tbl_search_counter` (`user_id` ,`time_searched`) VALUES ('165', '7');
END IF
I have a trigger that calculates the sum using a previous row and a current row and outputs the sum in balance field of the current row,The problem is that it doesnt seem to calculate the sum of the first row inserted into the table my trigger is:
delimiter $$
CREATE TRIGGER `ledger_calc` AFTER INSERT ON `sp_records`
FOR EACH ROW BEGIN
SET #PrevBal := (SELECT balance FROM ledger WHERE cmp_name = NEW.cmp_name AND inv_for = NEW.inv_for ORDER BY id DESC
LIMIT 1);
SET #CmpName := NEW.cmp_name;
SET #InvFor := NEW.inv_for;
IF (NEW.type = 'sales') THEN
SET #Type := 'sales';
INSERT INTO ledger (id,inv_for,cmp_name,date,debit,balance)
SELECT
TransactDet.id,
TransactDet.inv_for,
TransactDet.cmp_name,
TransactDet.date,
TransactDet.tot_amnt,
#PrevBal := #PrevBal + TransactDet.tot_amnt as balance
from
( select
Transact.id,
Transact.date,
Transact.tot_amnt,
Transact.cmp_name,
Transact.inv_for
from
sp_records Transact
where Transact.type = #Type
AND Transact.inv_for = #InvFor
AND Transact.cmp_name = #CmpName
order by
Transact.id DESC
limit 1 ) as TransactDet;
ELSE
SET #Type := 'purchase';
INSERT INTO ledger (id,inv_for,cmp_name,date,credit,balance)
SELECT
TransactDet.id,
TransactDet.inv_for,
TransactDet.cmp_name,
TransactDet.date,
TransactDet.tot_amnt,
#PrevBal := #PrevBal - TransactDet.tot_amnt as balance
from
( select
Transact.id,
Transact.date,
Transact.tot_amnt,
Transact.cmp_name,
Transact.inv_for
from
sp_records Transact
where Transact.type = #Type
AND Transact.inv_for = #InvFor
AND Transact.cmp_name = #CmpName
order by
Transact.id DESC
limit 1) as TransactDet;
END IF;
END;
#
This query returned what i want.. but it doesnt seem to convert to a trigger...
SELECT
PreAgg.id,
PreAgg.tot_amnt,
#PrevBal := #PrevBal + PreAgg.tot_amnt as balance
from
( select
YT.id,
YT.tot_amnt
from
sp_records YT
order by
YT.id ) as PreAgg,
( select #PrevBal := 0.00 ) as SqlVars
Please have a try with this one:
delimiter $$
CREATE TRIGGER `ledger_calc` BEFORE INSERT ON `sp_records`
FOR EACH ROW
BEGIN
SET #PrevBal : = (
SELECT balance
FROM ledger
WHERE cmp_name = NEW.cmp_name
AND inv_for = NEW.inv_for
ORDER BY id DESC LIMIT 1
);
INSERT INTO ledger (id, inv_for, cmp_name, DATE, debit, balance)
SELECT Transact.id, Transact.inv_for, Transact.cmp_name, Transact.DATE, Transact.tot_amnt, #PrevBal + Transact.tot_amnt AS balance
FROM sp_records Transact
WHERE Transact.type = NEW.type
AND Transact.inv_for = NEW.inv_for
AND Transact.cmp_name = NEW.cmp_name
ORDER BY Transact.id DESC limit 1;
END $$
I don't know, what your table structure looks like, but you might as well forget about the trigger and just do
INSERT INTO ledger (id, inv_for, cmp_name, DATE, debit, balance)
SELECT t.id, t.inv_for, t.cmp_name, t.DATE, t.tot_amnt, l.balance + t.tot_amnt AS balance
FROM sp_records t
INNER JOIN ledger l ON t.inv_for = l.inv_for AND t.cmp_name = l.cmp_name
WHERE t.type = 'a_value'
AND t.inv_for = 'another_value'
AND t.cmp_name = 'yet_another_value'
ORDER BY t.id DESC limit 1;
or as a trigger
DELIMITER $$
CREATE TRIGGER `ledger_calc` AFTER INSERT ON `sp_records`
FOR EACH ROW
BEGIN
INSERT INTO ledger (id, inv_for, cmp_name, DATE, debit, balance)
SELECT t.id, t.inv_for, t.cmp_name, t.DATE, t.tot_amnt, l.balance + t.tot_amnt AS balance
FROM sp_records t
INNER JOIN ledger l ON t.inv_for = l.inv_for AND t.cmp_name = l.cmp_name
WHERE t.type = NEW.type
AND t.inv_for = NEW.inv_for
AND t.cmp_name = NEW.cmp_name
ORDER BY t.id DESC limit 1;
END $$
If all this doesn't help, sample data and desired result would be helpful, preferably on http://sqlfiddle.com
I want a query to insert a row into a table I know it is simple but the scenario is the table should not have more than 5 rows. If table has more than five rows I need to remove the old row(Or replace with new row ) (Based on the insert time stamp) then i need to insert a new row.If number of rows less than count 5 then i can directly insert a row.
Please share me the query.
How about something like this.
declare #count int
SELECT #count=COUNT(*)
from EP_ANSWERS
IF (#count<5)
// DO your insert here
ELSE
DELETE FROM TABLE
WHERE inserttimestamp = (SELECT x.inserttimestamp
FROM (SELECT MAX(t.inserttimestamp) AS inserttimestamp
FROM TABLE t) x)
// DO your insert here
If it is impossible for the table to have more than 5 rows:
DELETE FROM yourtable
WHERE 5 <= (SELECT COUNT(*) FROM yourtable)
AND yourtimestamp = (SELECT MIN(yourtimestamp) FROM yourtable)
;
INSERT INTO yourtable ...
;
If it is possible for the table to have more than 5 rows:
DELETE FROM yourtable
WHERE 5 <= (SELECT COUNT(*) FROM yourtable)
AND yourtimestamp NOT IN (SELECT yourtimestamp
FROM yourtable
ORDER BY yourtimestamp DESC
LIMIT 4)
;
INSERT INTO yourtable ...
;
It sounds like you want to put a trigger on the table to maintain this rule, in MySQL the something like this should work
CREATE TRIGGER trg__my_table__limit_rows
BEFORE INSERT
ON my_table
FOR EACH ROW
BEGIN
IF ((SELECT COUNT(1) FROM my_table) = 5)
BEGIN
DELETE FROM my_table
WHERE id = (SELECT MIN(id) FROM my_table) -- change this to fit your logic for which record should be removed
END
END
Some of the code here is in pseudo (you didn't wrote your schema), but i wrote where you need to complete your own code.
DECLARE #NumberOfRowsToInsert INT = -- select from the data you want to insert
DECLARE #MaxNumberOfRows INT = 5
DECLARE #NumberOfExistingRows INT
DECLARE #Query VARCHAR(MAX) = 'SELECT TOP #rows id FROM SomeTable ORDER BY createdDate ASC'
SELECT #NumberOfExistingRows = COUNT(*)
FROM SomeTable
SET #Query = REPLACE(#Query,'#rows',
CAST(#NumberOfRowsToInsert - (#MaxNumberOfRows - #NumberOfExistingRows))) AS VARCHAR(1))
CREATE TABLE #IdsToDelete(id INT PRIMARY KEY)
INSERT INTO #IdsToDelete
EXEC(#Query)
DELETE FROM SomeTable
WHERE id IN (SELECT * FROM #IdsToDelete)
-- insert here..
This question already has answers here:
Oracle Replace function
(3 answers)
Closed 9 years ago.
I need to replace the Table1's filed values from Table2's values while select query.
Eg:
Table1:
Org Permission
--------------------------------------
Company1 1,3,7
Company2 1,3,8
Table2:
Permission Permission
--------------------------------------
1 Read
3 Write
7 Execute
8 Delete
I need like this:
Org Permission
--------------------------------------
Company1 Read,Write,Execute
Company2 Read,Write,Delete
I have been following your post since it was tagged in Oracle :D
In oracle it was looking in much possible ways, But in mysql you have to follow it with the help of procedure:
Schema:
create table table1 (org varchar(50), permission_id varchar(50));
create table table2 (permission_id int, permission_name varchar(50));
insert into table1 values ('Company1','1,3,7'),('Company2','1,3,8');
insert into table2 values (1,'Read'),(3,'Write'),(7,'Execute'),(8,'Delete');
Procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS `Update_Table_data`$$
CREATE PROCEDURE `Update_Table_data`()
BEGIN
declare max_row int;
declare p1 int;
Set p1 = 0;
SET max_row = (SELECT max(#i:=#i+1) AS row_num FROM table2 AS t,(SELECT #i:=0) AS foo);
label1: LOOP
set p1 = p1 + 1;
IF p1 <= max_row THEN
UPDATE Table1
SET permission_id =
replace(permission_id, (select permission_id from
(SELECT #i:=#i+1 AS row_num ,t.* FROM table2 AS t,(SELECT #i:=0) AS foo) a
where row_num = p1),
(select permission_name from
(SELECT #i:=#i+1 AS row_num ,t.* FROM table2 AS t,(SELECT #i:=0) AS foo) a
where row_num = p1));
Iterate label1;
END IF;
LEAVE label1;
END LOOP label1;
-- SET #x = p1;
END$$
DELIMITER ;
and then make a call to update your values :
call Update_Table_data;
Hope it helps :)
See SQLFiddle for initial data and this is resultant data SQLFiddle
UPDATE
organization
SET
permisson = (SELECT
GROUP_CONCAT(VALUE)
FROM
( SELECT
org,
SUBSTRING_INDEX(permisson,',',1) AS `permisson`
FROM
organization
UNION
SELECT
org,
SUBSTRING_INDEX(SUBSTRING_INDEX(permisson,',',2),',',-1) AS `permisson`
FROM
organization
UNION
SELECT
org,
SUBSTRING_INDEX(permisson,',',-1) AS `permisson`
FROM
organization
) AS t
JOIN
permission p
WHERE
p.p_id = t.permisson AND
t.org = organization.org
GROUP BY org
)
Try to replace your numbers using REPLACE.It work properly only when you are using single digit values for permission.
SQL = " SELECT Org, REPLACE(REPLACE(REPLACE(REPLACE(Permission,"1","Read'"),"3","Write'"),"7","Execute'"),"8","Delete'") as Permission FROM myTable "
REPLACE()