SQL trigger on Truncate - sql

How to Fire a trigger when you do TRUNCATE (instead deleted) in MSSQL

From msdn:
TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.

You can't do this on SQL server.
From MSDN
TRUNCATE TABLE cannot activate a
trigger because the operation does not
log individual row deletions. For more
information, see CREATE TRIGGER
(Transact-SQL).

Are you letting users run TRUNCATE TABLE ad hoc / willy nilly? If not, instead of worrying about using a trigger, why not wrap the TRUNCATE command in a stored procedure that also deals with whatever the trigger would have done after the truncate finished? (But you'd have to do it in the opposite order, of course.)

Related

Is there a way to do a checking on truncate table?

Sometimes I try test scenarios between several schemas , deleting/modifying tables , inserting/updating/deleting queries , some schemas are testing and the others are Important for production. so sometimes by accident I run queries in wrong schemas. so the commit functionality does really help in this scenario.
however Truncate table tab1 doesnt need commit, and if I execute it in a wrong schema .. well you know the scneario.
My question: Is there a workarround like the commit for truncate table like the DML Statment ? If you delete a statment you have to include a commit, or in plsql you have to click the green button to commit.
I use such check , its really annoying every time I want to truncate I have to modify the condition.
select count(1) into cnt from tab1 if cnt =0 then execute'Truncate table tab1'; end if;
I am not searching for flashback. I need a checking on truncate table
As #Boneist said, truncate is DDL statement which implicitly commits. If you are not sure of the action you do in a schema, and want to commit only after a manual verification, then do not TRUNCATE, use DELETE instead.
With DELETE statement, you could control the commit. Having said that, TRUNCATE resets the high watermark back to zero, however, DELETE doesn't. Even if you delete all the rows from the table, Oracle would scan all the blocks under the HWM. Have a look at this AskTom link.
If you are looking to bring back the truncated data, and if you are on 11gR2 and up, you could use the Flashback support for DDL statements.
TRUNCATE is a DDL statement, not DML, and DDL statements automatically include commits. See https://asktom.oracle.com/pls/asktom/f?p=100:11:0%3A%3A%3A%3AP11_QUESTION_ID:7072180788422 for more info.
I'm not entirely sure I understand what it is you're trying to do - you could, as Tom suggests, perhaps use an autonomous transaction to keep the truncate separate? If you're after the ability to separate the commit part from the truncate part (ie. to rollback the truncate if you decide you called it in error), then I'm afraid you're out of luck.

Difference between TRUNCATE and DELETE? [duplicate]

This question already has answers here:
What's the difference between TRUNCATE and DELETE in SQL
(32 answers)
Closed 8 years ago.
TRUNCATE and DELETE commands does the same job, in both the cases data is manipulated, then why does the DELETE command comes under DML commands and TRUNCATE command comes under DDL commands?
DELETE
DELETE is a DML Command.
DELETE statement is executed using a row lock, each row in the table is locked for deletion.
We can specify filters in where clause
It deletes specified data if where condition exists.
Delete activates a trigger because the operation are logged individually.
Slower than truncate because, it keeps logs.
Rollback is possible.
TRUNCATE
TRUNCATE is a DDL command.
TRUNCATE TABLE always locks the table and page but not each row.
Cannot use Where Condition.
It Removes all the data.
TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.
Faster in performance wise, because it doesn't keep any logs.
Rollback is not possible.
When we are using Truncate, we are de-allocating the whole space allocated by the data without saving into the undo-table-space. But, in case of Delete, we are putting all the data into undo table-space and then we are deleting all the data.
The main points that put TRUNCATE in the DDL camp on Oracle, are:
TRUNCATE can change storage parameters (the NEXT parameter), and those are part of the object definition - that's in the DDL camp.
TRUNCATE does an implicit commit, and cannot be rolled back (flashback aside) - most (all?) DDL operations in Oracle do this, no DML does
Well Basically you know the DML AND DDL concept the above difference is just keep in mind but the key difference is that
Truncate only manuplates the data without affecting a table structure and key constraints but
on the other hand
Delete will affect the table with a where conditions.

Column Constraint Sql 2008

I have been trying to find out if I can make a column Open on Insert, and closed on Update.
What I mean by that, is I need a column that I can give it a value only on Insert, but if I try to give it a value with Update, the statement would fail.
I am working with SQL 2008...
You can't do this with constraints - you need to use triggers for this.
A trigger is a special kind of stored procedure that automatically executes when an event occurs in the database server. DML triggers execute when a user tries to modify data through a data manipulation language (DML) event. DML events are INSERT, UPDATE, or DELETE statements on a table or view.

Ensure that tables are not dropped from database

I need to ensure that tables are not dropped from my database. Should I..
Create DDL(or DML ?) trigger that contains COMMIT or create DDL (or DML ?) trigger that contains ROLLBACK ?
Assuming SQL Server there is an example of doing this in BOL
CREATE TRIGGER safety
ON DATABASE
FOR DROP_TABLE, ALTER_TABLE
AS
PRINT 'You must disable Trigger "safety" to drop or alter tables!'
ROLLBACK
;
You would be better off removing permissions from anyone that might DROP the tables inappropriately however. DDL triggers are after triggers, not instead of triggers so a drop table statement might still cause problems even if eventually rolled back.
You can use a DDL trigger to ROLLBACK. DDL is itself a transaction, the trigger is pasrt of the transaction, so you can roll it back.
A better way would be to remove permissions so folk can't delete objects in the first place. With rights to drop objects comes the right to drop triggers too (usually)

New trigger in sql server 2005 database

The database I am using already has a 2 triggers on part number table (on insert and on update). Insert trigger updates creation date, and update trigger updates modification date.
I have to add 3 more triggers to log updates to this table (on insert, on update and on delete)
In what order they will be executed? Before existing trigger or after? Can I control that?
sp_settriggerorder will allow you to set first or last trigger.
If you have more than two and the order matters, combine them into one trigger and split the functionality over the stored procedures.