How can i increment month name in trigger? - sql-server-2008

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

Related

Automatically add days to a column based on other table columns

I have a table A which has column NoOfDays, and another table B which has a column MyDate of type datetime and it is set to Default as "CURRENT_TIMESTAMP".
Now in table B, I want to add a new column (called ValidDate), which should AUTOMATICALLY store the sum value of (B.MyDate + A.NoOfDays)
For example, if B.MyDate has value of "2022-07-25 04:50:26" and A.NoOfDays has value of "60", then B.ValidDate should get the value of "2022-09-23 04:50:26"
What is the way in MySQL to set this new column value to store this summed value automatically.
Checked for existing threads, but found only this one which does not offer a solution to store, but to create a view output only.
MySQL - Add number of days to date in 3rd column automatically
For existing rows in table_b , use an update statement like UPDATE table_b set ValidDate=date_add(MyDate , interval + (select NoOfDays from table_a) day);
For new inserts, we can use a trigger to handle that. Here is the complete script written and tested in workbench:
create table table_a (NoOfDays int);
create table table_b(MyDate timestamp,ValidDate datetime);
insert table_a values(60);
DELIMITER //
drop trigger if exists auto_sum //
create trigger auto_sum before insert on table_b for each row begin
set new.ValidDate=date_add(new.MyDate , interval + (select NoOfDays from table_a) day);
end //
delimiter ;
insert into table_b (MyDate) values (default); -- The value of ValidDate is calculated in the trigger,so we only need to specify the value for MyDate.
Please try this:
CREATE TABLE TableA (NoOfDays INT);
INSERT INTO TableA VALUES(60);
CREATE TABLE TableB(MyDate DATETIME DEFAULT CURRENT_TIMESTAMP);
INSERT INTO TableB VALUES('2022-07-25 04:50:26');
ALTER TABLE TableB
ADD ValidDate DATETIME;
UPDATE TableB
SET ValidDate = date_add(MyDate, INTERVAL (SELECT NoOfDays FROM TableA) DAY);
If we test the result :
SELECT * FROM TableB;

Sum up columns using group by and store as a new column (permanently) in MySQL

I have 50 columns in the MySQL table. I want to sum all these columns and make a new column into the same table (sum50).
This should be stored permanently into the MySQL table whenever I update or insert new data.
I know how to sum up while performing the query but it does not store into the table permanently.
CREATE TRIGGER `name`
AFTER INSERT ON `table`
FOR EACH ROW
UPDATE `table` SET `table`.`coulms` = `table`.`col1`+.... `table`.`col50`+
I am trying the above trigger, but not working.
Should I have a blank column inserted into the table and perform trigger? Which trigger would be correct?
Instead of the trigger, add a generated column to your table:
alter table tablename add(sum50 int generated always as (col1 + col2 + ...) stored);
See a simplified demo.

Is it possible to find which tables have new data in the last n days?

My current project doesn't use any database migrations and so if any changes arise they are found manualy.
Is it possible to find out which tables in a database have new rows that were created in the last n days (for example 7 days)?
Thanks for any suggestions!
Create a Trigger for insert to make a value in the table for 1 as newly inserted or 0 it is inserted before .... its a flag that shows that a new value is added ... and then you can make an update statement to make sure that they are all take 0 value ... so that every time you insert in the table you add this column with 1
Example :
id StudentName flag
1 Frank 0
2 Andrew 1
insert into tablename(id,StudentName,flag) Values(3,'Alice',1)
always make the flag with 1 to know that it is a new record and it is recently added.
update tablename set flag = 0
this statment to make sure that all is inserted before and it is not new !!
Create A common table Which shows when table Update
Create table TableUpdatedRrecord (Table_Name varchar(max),Flag int,TableCreatedDate DateTime,Modified Date)
First Create table Which Has Record when New Rows were Inserted in table
Create table TableUpdatedRrecord (Table_Name varchar(max),Flag INT,TableCreatedDate DateTime,Modified DatetIME)
Now Create a Trigger for each table which is executed when table has a new insert
CREATE TRIGGER TriggerName
ON dbo.TableName
AFTER INSERT AS
BEGIN
DECLARE #CreatedDate datetime
Set #CreatedDate=(Select t.create_date from sys.tables t where name='TableName')
INSERT INTO TableUpdatedRrecord
(Table_Name,[Flag],[TableCreatedDate],[Modified])
VALUES
('TableName'
,1
,#CreatedDate
,GetDate())
END

mysql: extract Timestamp in separate column as date and time with trigger

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.

Trigger to insert percentage value in a table based on the values of two different fields

I have a table 'attendance' having 3 fields 'class_total','class_attended' and 'attendance_percent'.
I want to insert/update the value of 'attendance_percent' whenever values are inserted/updated in the fields 'class_total' and 'class_attended' by (class_attended/class_total)*100.
For this I am using the trigger:
CREATE TRIGGER percent_update
BEFORE INSERT ON attendance
FOR EACH ROW
SET NEW.attendance_percent =(attendance.class_attended/attendance.class_total)*100 ;
But its not working.
Use a NEW clause instead of table name -
...
SET NEW.attendance_percent = (NEW.class_attended/NEW.class_total)
...