trigger mysql unknown table - mysql

I've been trying to solve this problem. Here is the code:
CREATE TRIGGER some_trigger AFTER UPDATE ON table_a
FOR EACH ROW BEGIN
DECLARE tname VARCHAR(20);
IF (table_a.field_offer=1) THEN
SET tname='table_b';
ELSEIF (table_a.field_offer=0) THEN
SET tname='table_c';
END IF;
IF ((new.field_state = 0)) THEN
UPDATE tname join table_a on tname.ID=table_a.ref_field
SET tname.STOCK = tname.STOCK -1;
ELSEIF ((new.field_state = 1)) THEN
UPDATE tname join table_a on tname.ID=table_a.ref_field
SET tname.STOCK = tname.STOCK +1;
END IF;
END;
I am getting:
Unknown table 'table_a' in field list. Field_offer and field_state can
be null.

I've shown below what was said in the comments to the question:
CREATE TRIGGER some_trigger AFTER UPDATE ON table_a
FOR EACH ROW BEGIN
DECLARE tname VARCHAR(20);
IF (NEW.field_offer=1) THEN
UPDATE `table_b`
SET STOCK = CASE NEW.field_state
WHEN 0 THEN STOCK - 1
WHEN 1 THEN STOCK + 1
ELSE STOCK
END
WHERE ID=NEW.ref_field
;
ELSEIF (NEW.field_offer=0) THEN
UPDATE `table_c`
SET STOCK = CASE NEW.field_state
WHEN 0 THEN STOCK - 1
WHEN 1 THEN STOCK + 1
ELSE STOCK
END
WHERE ID=NEW.ref_field
;
END IF;
...
Note that I changed the updates from UPDATE ... JOIN as MySQL does not allow you to change the data of the triggered table; while you were not actually updating table_a, the JOIN could have been enough for MySQL to have objected... that and the joins would've updated every row in table_b/c that had a match in table_a, not just table_a rows related to the values in or row of the trigger.

Related

How to compare two tables SUM and Update another Table With MySql Trigger

compare two tables SUM and Update another Table With MySql Trigger
DELIMITER $$
CREATE TRIGGER change_com_status_on_sales_table AFTER INSERT ON `commision_calculation` // COMMISSION TABLE
FOR EACH ROW
BEGIN
DECLARE totalQnt1, totalQnt2 DOUBLE;
DECLARE sales_id INT;
SET #sales_id=(SELECT sales_info_id
FROM commercial_invoice WHERE ci_no = NEW.ci_no);
SET #totalQnt1=(SELECT sum(cc.quantity) as total_qnt1
FROM commision_calculation cc
LEFT JOIN commercial_invoice ci ON cc.ci_id = ci.id
WHERE cc.comission_type = 'Remex' AND ci.sales_info_id= #sales_id
GROUP BY #sales_id);
SET #totalQnt2=(SELECT SUM(sii.quantity) as total_income
FROM sales_info_item sii WHERE sii.sales_info_id = #sales_id
GROUP BY #sales_id);
IF (#totalQnt2 > #totalQnt1) THEN
UPDATE sales_information
SET ci_status = 'Partial'
WHERE id = #sales_id;
END IF;
IF (#totalQnt2 = #totalQnt1) THEN
UPDATE sales_information
SET ci_status = 'Full'
WHERE id = #sales_id;
END IF;
END; $$
DELIMITER ;
The error in above as mentioned by Jahid Hossain
I get "SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row"

error 1329 (02000): no data - zero rows fetched, selected, or processed

I am getting an error in the following code below. The error is #1329 - No data - zero rows fetched, selected, or processed. What exactly does this mean and what is it that i am doing incorrectly? Thanks
create or replace procedure grade()
begin
declare no,s1,s2,s3,cnt,i,per int(20);
declare c1 cursor for select roll,sub1,sub2,sub3 from student;
set i=0;
select count(*) into cnt from student;
open c1;
while i<=cnt do
fetch c1 into no,s1,s2,s3;
set per=s1+s2+s3/3;
if per>=90 then
update student set grade='A+' where roll=no;
elseif (per<90 and per>=80)then
update student set grade='A' where roll=no;
elseif (per<80 and per>=70)then
update student set grade='B+' where roll=no;
elseif (per<70 and per>=60)then
update student set grade='B' where roll=no;
elseif (per<60 and per>=50)then
update student set grade='C+' where roll=no;
elseif (per<50 and per>=40)then
update student set grade='C' where roll=no;
else
update student set grade='FAIL' where roll=no;
end if;
set i=i+1;
end while;
close c1;
end$$
This logic:
set i = 0;
select count(*) into cnt from student;
while i <= cnt do
is going through the loop 1 extra time. It is on the last trip through the loop that you are getting the error.
If cnt is 3, for instance, then the loops are:
0
1
2
3
You either want:
set i = 1
or alternatively
while i < cnt
In SQL, I would use the first method, because things usually start counting at 1 rather than 0 in SQL -- but they are equivalent.

MySQL Trigger updates all rows even if a distinct where clause is given

Refer to the following trigger,
create trigger trigger_update_rows after insert on transaction for each row
DECLARE tr_type VARCHAR(15);
DECLARE tr_op VARCHAR(10);
DECLARE tr_amt FLOAT(8,2);
DECLARE tr_qnt INTEGER;
DECLARE item_id INTEGER;
SET tr_type = NEW.TRANSACTION_TYPE;
SET tr_op = (SELECT TRANSACTION_TYPE_OPERATION FROM transaction_type WHERE
TRANSACTION_TYPE_NAME=tr_type limit 1);
SET tr_amt = NEW.TRANSACTION_AMOUNT;
SET tr_qnt = NEW.QUANTITY;
SET item_id = NEW.ITEM_ID;
IF tr_op = 'ADD' THEN
update item set QUANTITY_AVAILABLE=QUANTITY_AVAILABLE-
tr_qnt,QUANTITY_SOLD=QUANTITY_SOLD+tr_qnt,AVAILABLE_GOODS_VALUE =
AVAILABLE_GOODS_VALUE - tr_amt,SOLD_GOODS_VALUE=SOLD_GOODS_VALUE+tr_amt
where ITEM_ID=item_id;
ELSEIF tr_op = 'SUBTRACT' THEN
update item set QUANTITY_AVAILABLE=QUANTITY_AVAILABLE+tr_qnt,QUANTITY_BOUGHT=QUANTITY_BOUGHT+tr_qnt,AVAILABLE_GOODS_VALUE=AVAILABLE_GOODS_VALUE+tr_amt,BOUGHT_GOODS_VALUE=BOUGHT_GOODS_VALUE+tr_amt where ITEM_ID=item_id;
END IF;
END
ITEM_ID is a primary key in the table item and the following is the transaction row inserted
insert into transaction(TRANSACTION_DATE,TRANSACTION_TYPE,ITEM_ID,QUANTITY,TRANSACTION_AMOUNT,ACCOUNT_ID,BUYER,SELLER) values ('2017-08-12','BUY',2,5,500.00,3,1,2);
What is making the above trigger update all rows in item instead following the where clause
You problem is:
where ITEM_ID = item_id;
You may think that one of the item_ids is somehow connected to the variable. It is not.
Rename the variable to something like v_item_id and then use:
where item_id = v_item_id

How to create a before insert trigger based on previously inserted values in the table

I have a table which contains two columns
1.Clanid
2.Active
My problem is that i dont want any value in Clanid column to be inserted in this table if this value is already in Clanid column and Active for that value is 1.
For example
Clanid Active
1 1
2 1
3 0
Now it should not be possible to insert a record with Clanid=1 and Active=1 but i can insert Clanid=3 and Active=1 as this record is not there.
Try this:
delimiter //
create trigger unique_clanid
before insert on mytable
for each row
begin
if new.active and exists (
select * from mytable
where clanid = new.clanid
and active) then
signal sqlstate '02000' set MESSAGE_TEXT = 'Duplicate ClanID';
end if;
end//
delimiter ;
I think you should handle in your app level, but you want to handle in DB lavel, you can write trigger check it
you can check count record ( where Clanid = #param and Active =1)
if count > 1 : rollback
I am not available mysql to test , i just can describe my solution as following ( i;m not sure the syntax correct, it is too long time i don't write trigger in mysql)
CREATE TRIGGER test BEFORE INSERT ON table_name
FOR EACH ROW
BEGIN
DECLARE newClanId integer;
DECLARE counts integer;
Set #newClanId = NEW.Clanid;
Set #count := (SELECT count (*) FROM table_name
WHERE Clanid = #newClanId and Active =1)
IF #count > 1 THEN ROLLBACK;
END;

MySQL: Conditional to run Query 1 or Query 2 based on a certain field

I'm creating a trigger which is supposed to run Query 1 if the value of the field statuscode from the newly inserted row is 200. But if the statuscode is say 300 run Query 2 instead. Is this at all possible in MySQL?
I tried the first thing which came to mind but it's obviously wrong.
CREATE TRIGGER mytrigger AFTER INSERT ON tableA
FOR EACH ROW BEGIN
IF(new.statuscode = 200,
(DELETE FROM tableB WHERE new.guid = tableB.guid),
(UPDATE tableB SET is_complete = 0 WHERE new.guid = tableB.guid))
END
//
DELIMITER ;
The syntax for IF/ELSE in mysql procedures is:
IF (NEW.statuscode = 200) THEN
DELETE FROM tableB WHERE new.guid = tableB.guid;
ELSE
UPDATE tableB SET is_complete = 0 WHERE new.guid = tableB.guid;
END IF;
Here are the docs for trigger syntax:
http://dev.mysql.com/doc/refman/5.5/en/if.html
I think you just have your IF syntax wrong. If statements should have a THEN and ENDIF keyword.
http://dev.mysql.com/doc/refman/5.0/en/if.html
I haven't used MySQL in 6 months or so, so hopefully this syntax is right:
CREATE TRIGGER mytrigger AFTER INSERT ON tableA
FOR EACH ROW BEGIN
IF new.statuscode = 200 THEN
DELETE FROM tableB WHERE new.guid = tableB.guid;
ELSE
UPDATE tableB SET is_complete = 0 WHERE new.guid = tableB.guid;
END IF;
END