How do i fire a trigger while truncating a table? - sql-server-2008

I am using MS SQL server 2008. Right now am firing my trigger on deleting records from my Master table to delete corresponding records from my child table. Now am trying to fire the trigger on truncate on my Master table. Is it possible? If yes, kindly help me to find the solution.

TRUNCATE TABLE cannot activate a trigger because the operation does
not log individual row deletions.
Ref.
Suggest you perform an actual delete, and perform in batches if this was the original reason (i.e. locking) you used TRUNCATE instead of DELETE

Related

Query related to trigger fire in SQL database?

Is it possible to fire a trigger when a row value in the table equals a certain value? Like if a value in the table hits zero, I want a certain DML operation to occur using a trigger, is this possible?
Triggers fire based on events on DB objects. For example you can define a trigger to fire before or after insert, update or delete events on tables. What you do in the trigger implementation is entirely up to you, DB will make old record (for update and delete) and new record (for insert and update) available to you.
For your requirement, you can implement a logic that will check for the attribute's value and if it hits zero, you perform the DML operation else just return without doing anything.

How to create batch jobs in Mysql?

I would like to create batch jobs in a Mysql database. To fetch data from child tables, and insert them into a master table, whenever the child table gets updated or new records got inserted.
How could I do this?
What you are looking for is called a trigger.
Triggers are functions executed when an update insert or delete action takes place on a specified table.
Read more here
http://www.mysqltutorial.org/mysql-triggers.aspx

How to automatically run a query when a table is amended by INSERT/UPDATE/DELETE?

I have a query which basically "syncs" all the data from a table in one database, to a replicated table in another database.
Here is the simple query:
TRUNCATE TABLE [Database2].[dbo].[USER_SYNC]
INSERT INTO [Database2].[dbo].[USER_SYNC]
SELECT * FROM [Database1].[dbo].[USER]
Now, after some research, I had a look into using a trigger to do this, however, I read up that stored procedures and heavy queries such as this should not be used within a trigger.
Therefore, what is the best way in which I can automatically run this query from within SQL, whenever a record in database1 is inserted, amended or deleted?
And if what I read up about triggers was incorrect, then how would I go about creating one for my procedure? Thanks.
If you need to sync tables you do not need to truncate one every time on update, delete or insert.
Create identical copy of user table.
Create on update, on delete, on insert triggers on the original user table.
In the trigger update, delete or insert to the duplicate table only one row at a time - the one that was updated, deleted or inserted to the original user table. This will not be a heavy query.
UPDATE:
http://www.mysqltutorial.org/create-the-first-trigger-in-mysql.aspx
http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

SQL Server trigger not working while inserting values

Is it possible the insert trigger is not run while the values are inserted on the table? Anybody was experience this?
No it's not possible, if you have the trigger set up correctly. Are you sure you've indicated you want the trigger to fire on an Insert? You have the option of specifying a trigger to fire on an Insert, Update, Delete or any combination.
Or the trigger could be disabled. Or there could be a Return statement in the first line of the trigger. I've seen people do that, as a way to disable a trigger.
Also, if you are inserting multiple rows into the table, you need to make sure your trigger is created correctly to handle that. Handling multiple inserts can be a bit more complicated than handling a single insert, and unexpected results could occur if you are not aware of the difference.

When should i create mysql trigger?

Are triggers specifics to each mysql connection? Or is it done when i create trigger one time?
A trigger is created on a table, not on a connection, and once defined it remains on the table until it is explicitly dropped. It can be defined to fire for INSERT, UPDATE and/or DELETE operations on the given table.