I want to set a trigger that execute to extract date and time in separate column in a same table when new row insert the table as shown in below:
What I have tried:
CREATE TRIGGER date_time_insert`
BEFORE INSERT ON tbl_raw_attendance
FOR EACH ROW
INSERT INTO tbl_attn_temp (TraineeID,
ScannerID,
attnDate,
attnTime )
VALUES( NEW.TraineeID,
NEW.ScannerID,
CURRENT_DATE,
CURRENT_TIME );`
thanks for any help.
Related
I am creating a query, wherein i have created select query from table 1 and then inserting values to table 2, but how can create a command that when new data comes in table 1 then update table 2 values
and this insertion happen daily with job.
I tried creating trigger but i don t know how i can relate this to my scenario
IF OBJECT_ID('TEMPDB..#temp')IS NOT NULL
DROP TABLE #temp
Select Getdate()[Data Till],Sum[Quantity] Qty
into #temp from XYZ w
here w.Date <=Getdate()
Insert into dbo.table1
Select [Data Till],Qty from #temp >
As of now data is coming in table 2 like
1)[Data Till]-2019/05/22 Qty-100
2)[Data Till]-2019/05/23 Qty-150
3)[Data Till]-2019/05/24 Qty-120
Now what if in 2019/05/22 back dated entry of qty -20 comes in table 1 and how will table 2 has to update values in table 2
1)[Data Till]-2019/05/22 Qty-80
2)[Data Till]-2019/05/23 Qty-150
3)[Data Till]-2019/05/24 Qty-120
CREATE TRIGGER [schema_name.]trigger_name
ON Table_name
AFTER INSERT
AS
BEGIN
-- write your update statement here
END
This is how you can write after update trigger that executes your statement between 'BEGIN' and 'END' when you insert new row into 'Table_name'.
AS MY ASSUMPTION
Update means you have the data in table 2 already. If the new data comes in table 1 what kind of update you want to do ?
is it existing data update or new record insert then update.
From your question if it is updated above two kinds of things can be happened
For this why you have two write insert trigger
create trigger abc
on table1
For insert
as
begin
--update table2
end
i have created two tables into student database
1) Admission table (rollNO,Name,fname,address,department,addmission_date)
2) Fees Table(rollNO,Name,fname,address,department,Total_fees,Fees_Payment_date)
i want to insert same data of addmission table into fees table which contain same column names like rollNO,Name,fname,address,department using trigger's event "after"
how can i do it? if i insert data into addmission table it will also automatically insert into fees table????
Try this code, NEW is ref of admission table insert record contains
DELIMITER //
CREATE TRIGGER `fee_trigg` AFTER INSERT ON `admission`
FOR EACH ROW BEGIN
INSERT INTO fee (rollNO,Name,fname,address,department)
VALUES( NEW.rollNO, NEW.Name, NEW.fname,NEW.address,NEW.department );
END; //
DELIMITER ;
I have a table called history with the fields id, g.id, date, value. I want to put a trigger that will update the table when a new row is inserted and divide the number inserted in the value field by 2.
I have been trying for hours with no luck, any help would be appreciated.
for eg, after
INSERT INTO `online_game_shop`.`history`
(`id`, `gameID`, `dateofPurchase`, `Value`)
VALUES ('1001', '101', '2014-02-22', '10');
so the trigger will automatically divide 10 by 2 and update the field with result.
CREATE TRIGGER pointstovalue
AFTER INSERT ON history
FOR EACH ROW
BEGIN
UPDATE history
SET value = new.value/2
WHERE history.id = NEW.id
END;
You want a before insert trigger:
CREATE TRIGGER pointstovalue
BEFORE INSERT ON history
FOR EACH ROW
BEGIN
set new.value = new.value/2;
END;
I have to make only one insertion / day on a Mysql table .
DELIMITER $$
CREATE TRIGGER trig
BEFORE INSERT ON Table
FOR EACH ROW BEGIN
/** compare the date with the latest inserted **/
IF () THEN
// if it's ok => insert
ELSE
// nothing
END IF;
END$$
DELIMITER ;
I want to compare the last inserted date and current date and then initiate my trigger to do the work.
Thanks
You can achieve this without trigger (which is a good thing right?) by creating a UNIQUE constraint on the date column and then using INSERT IGNORE syntax.
Suppose you have a table with the following schema
CREATE TABLE table1
(
date DATE,
value INT
);
Let's create a UNIQUE constraint
CREATE UNIQUE INDEX idx_date_unique ON table1 (date);
Now you can use IGNORE clause
INSERT IGNORE INTO table1 VALUES (CURDATE(), 1);
INSERT IGNORE INTO table1 VALUES (CURDATE(), 2);
INSERT IGNORE INTO table1 VALUES (CURDATE(), 3);
As a result you'll have only first row in the table for each day.
Here is SQLFiddle demo
ALTER trigger [dbo].[trg_Billing_TotalFee] on [dbo].[tblBilling] after insert as insert into tblTotalFee(DueFromPreviousMonth,StudentID,MonthName) select RemainingAmount,StudentID,MonthName from inserted
This is my trigger. What I want is in place of MonthName i want MonthName++ in tbltotalfee i.e. Let's say MonthName in tblBilling is January then in tblTotalFee i want month to be inserted as Febraury.How can i do this??
Try this:
Create a table called nextMonths with 2 columns - currentMonthName and nextMonthName. Populate the table with currentMonth and corresponding nextMonth name for each.
Now your trigger should be :
ALTER trigger [dbo].[trg_Billing_TotalFee]
on [dbo].[tblBilling] after insert as
insert into tblTotalFee(DueFromPreviousMonth,StudentID,MonthName)
select RemainingAmount,StudentID,NextMonthName from inserted join nextMonths on inserted.MonthName = nextMonths.currentMonthName