How to identify by a trigger if any insert update delete function operation performed in any table of SQL Server - sql

I have a database in which I want to make a centralized trigger; if any database table is hit with an insert, update or delete operation, then this trigger should be executed and column values that are inserted, updated or deleted from that operation should be saved in my own table by the help of trigger.
I have seen fn_dblog function in SQL Server but it does not return column values which are affected. I need to save that column values also which are going to be inserted or updated.

Related

one sql trigger insert,update,delete for all tables in the database

is it possible to declare one Sql Trigger Insert, Update, Delete for all tables in the database instead of creating a separate trigger for each table? I just want a simple history of what actions have been taken e.g. TABLE A deletes a row ,TABLE B updates a row ,TABLE C Add new row .. and the trigger will insert a new row to another table with that information I want.
No, a trigger can only be defined on a specified table.
You should read up on the auditing features of SQL Server (http://technet.microsoft.com/en-us/library/cc280386.aspx). They are more performant and more flexible in what you want to achieve. Unfortunately they are not available in the Express Edition.

Need a sql statement to do upate and insert at the same time

I need a sql statement, to insert a new row in one database table and update an existing row in another database table based on some conditions.
Is there a way to do this? To insert a row in one table and update a row in another database table in one sql statement?
Thanks in advance!
Yes, they are called Transactions, and are implemented with START TRANSACTION and COMMIT/ROLLBACK with something like:
START TRANSACTION;
INSERT INTO ...
UPDATE table2 SET name='TOTO' WHERE type=1;
COMMIT;
EDIT
This is not in fact one SQL query, but the operation is done atomically - and I think that is what you need.
A single SQL statement allows you to update one table, not several; if that statement is a MERGE then you can specify insert/update/delete actions but still targeting just the same one target table.
If you just want consistency, use transactions; until a transaction is committed, changes within it are not visible to the outside world.
If you want that a single update (which you cannot control) resulted in a coordinated insert, use an on update trigger in the table being updated. The trigger would insert appropriate row(s) into other table(s).
You can use Trigger to update second table on insert of first table
Yes, it's possible with stored procedures.
Watch this: Stored procedures

sql server 2005 trigger after delete

I have written a trigger on a table after delete row to update some other table using a field from the 'deleted' (magic table). It works fine for single row delete.
But when I try to do deletes like
delete from tblIndentDetails
WHERE indentID=5;
(it is supposed to delete more than one records meeting the criterion), the delete operation is not performed due to trigger. How can I work around the situation?

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.

DDL statements against deleted inserted in DML trigger

I am trying to find impact of doing DDL statement against deleted and inserted logical tables inside table trigger. I have:
CREATE TRIGGER [Trigger52]
ON [dbo].[Table1]
FOR DELETE, INSERT, UPDATE
AS
BEGIN
create table inserted (c1 int)
select * from inserted
END
When it is triggered, I expected to get an error. Instead, it seems to ignore create table statement entirely and select rows that have been inserted.
Is there a documentation describing this behavior or explanation?
Inside triggers there are always two pseudo-tables existing: inserted and deleted. See Using the inserted and deleted Tables:
SQL Server automatically creates and
manages these tables. You can use
these temporary, memory-resident
tables to test the effects of certain
data modifications and to set
conditions for DML trigger actions.
You cannot directly modify the data in
the tables or perform data definition
language (DDL) operations on the
tables.