I have to create a trigger a which captures old and new value while updation & deletion taken place
Audit table :
create table if not EXISTS tbl_audits
(
TA_Auditid int AUTO_INCREMENT PRIMARY key,
TA_Name varchar(100),
TA_Oldvalue varchar(1000),
TA_Newvalue varchar(1000),
TA_Actiontaken varchar(100),
TA_createddt datetime,
TA_createdby varchar(100)
)
DELIMITER //
create TRIGGER TRfiledetails_update
BEFORE update on tbl_file_details
for EACH ROW
BEGIN
insert into tbl_audits
set TA_createddt =CURRENT_TIMESTAMP,
TA_Actiontaken ='update',
TA_Name ='File',
TA_Oldvalue =old.FL_dtstartdt ,
TA_Newvalue =new.FL_dtstartdt
end //
DELIMITER ;
In my main table i have to create trigger an event on two columns startdt and enddate.
In this case I would like to know whether I need to create two triggers separately for each column.
Is it possible to create two columns action on same trigger or need to create separate columns in audit table.
Related
I have a procedure like this :
create
procedure new_generation(IN id bigint unsigned)
BEGIN
create temporary table if not exists tmp_mine
(
id bigint unsigned primary key not null auto_increment,
created_at timestamp,
);
end
now I want to create an event for delete old rows.
create event per_ten_second on schedule
every '10' SECOND
starts '2021-10-26 11:49:16'
enable
do
delete from tmp_mine where created_at < now();
but it doesn't work. Can anybody give me a clue? Can we use generated temp table inside stored procedure out of it? for example inside an event.
I created a table
Databases Name - mytrigger;
Table name - employee_audit
use mytrigger;
create table employee_audit(
id int auto_increment primary key,
employeeNumber int not null,
lastName varchar(50) not null,
changee datetime default null,
action varchar(50) default null
);
After that, I created one update trigger
My trigger name is
before_employees_update
DELIMITER $$
create trigger before_employees_update
before update on employee_audit
for each row
begin
insert into employee_audit
set action ='update',
employeeNumber = OLD.employeeNumber,
lastName = OLD.lastName,
changee = now();
end$$
DELIMITER ;
After that, I inserted values in table using this command ->
insert into employee_audit values(1,112,'prakash','2015-11-12 15:36:20' ,' ');
After that, I want to update my table row where id =1
update employee_audit set lastName = 'Sharma' where employeeNumber =112;
But it is not executed give an error
ERROR 1442 (HY000): Can't update table 'employee_audit' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
When I searched on Google I found a lot of Question with the same error. But not able to fix my problem. what is the reason I'm not able to update my row?
What i suggest,you can create one log table like employee_audit_LOG .
And on every insert or update in main table you can make new entry in this table or update existing record.
Also you can add updated_timestamp column to that LOG table which maintain when did specific record get updated.
The error itself tells you the answer. This is because, you can't use the same table on which trigger is being executed. You need to store your audit logs into some different table.
Hey all so I have created a table named products that looks like so:
`CREATE TABLE products
(
Prod_ID int(10),
Prod_name varchar(20),
Prod_qty varchar(20),
Prod_price int(20)
);`
Product_log table is nearly identical to another table called products:
`CREATE TABLE product_log
(
Prod_ID int(10),
Prod_name varchar(20),
Action_Date date,
Updated_by varchar(30),
Action varchar(30)
);`
Next I have created a trigger called products_after_insert which should insert data into the product_log table after a row in products is inserted.
The requirement for after insert trigger is that action date should be inserted in the product_log table and the user name should be inserted like who inserted the data ex: data operator,
and on last the action should be inserted in the product_log table automatically like action here is insertion.
here is my trigger:
`DELIMITER //
CREATE TRIGGER products_after_insert
AFTER INSERT
ON products
FOR EACH ROW
BEGIN
DECLARE data_operator int(10);
DECLARE action_per varchar(200);
SET data_operator = 1;
SET action_per = 'INSERTION';
IF data_operator=1 THEN
INSERT INTO product_log(prod_id,prod_name,Action_date,Updated_by,Action)
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),'data_operator','action_per');
END IF;
END;
//DELIMITER;`
Now, I assume I am constructing my trigger incorrectly because it appears to not be working.
Does anyone know what I am doing wrong? Any help would be greatly appreciated! Thanks!
There's at least this you are doing wrong :
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),'data_operator','action_per');
You have properly defined the variables data_operator and action_per previously:
SET data_operator = 1;
SET action_per = 'INSERTION';
But in your INSERT instruction you surrounded them with quotes, which turned them into strings. Your instruction should be :
VALUES(NEW.prod_id,NEW.prod_name,SYSDATE(),data_operator,action_per);
I need a solution for copy the auto increment value to userid column when insert the record.
I have two column like id(AI),userid. Here I have used a trigger
CREATE TRIGGER adduserid BEFORE INSERT ON user
FOR EACH ROW SET NEW.userid = NEW.id
The problems is triggers invoke before insert,so it will get id as 0.
Suggest me how can I modify the trigger?
Sample:
Table definition:
CREATE TABLE departments (
ID NUMBER(10) NOT NULL,
DESCRIPTION VARCHAR2(50) NOT NULL);
ALTER TABLE departments ADD (
CONSTRAINT dept_pk PRIMARY KEY (ID));
CREATE SEQUENCE dept_seq;
Trigger definition:
CREATE OR REPLACE TRIGGER dept_bir
BEFORE INSERT ON departments
FOR EACH ROW
BEGIN
SELECT dept_seq.NEXTVAL
INTO :new.id
FROM dual;
END;
Syntax Link1:https://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
I'm trying to create a procedure which adds / removes a table to the database. I'm working in SQL Server. The query works (it succeeds) but the table isn't added to the database.
I did refreshed...
ALTER procedure [dbo].[upgrade_1]
as
begin
create table awards (
ID int NOT NULL IDENTITY,
name nvarchar(256) DEFAULT 'award',
description nvarchar(256)
PRIMARY KEY (ID)
)
/*update goto_vs
set current_version = 1*/
end
The script you have in the question will only modify the PROCEDURE. The procedure needs to be executed for it to perform the required task e.g. create the table
Execute the procedure with this statement
EXEC upgrade_1
That should create the table