Pre-insert/update trigger in SQL Server - sql

All we need is to create several pre-insert/update triggers in SQL Server 2008 R2 which do some select * or select count(*) and explicitly fail if it is empty or non-empty.
Please, how to achieve that?

Create an INSTEAD OF INSERT, UPDATE trigger. Now to prevent DML you have 2 options. In the body of trigger execute RAISERROR so your application can catch the error. Another way is not to perform actual insert/update in the trigger, so no records will be inserted/updated, but the user will not know if DML succeeds.

Related

Under what circumstances are SQL Server triggers executed?

The database I'm working on has a trigger which calls a stored procedure which takes 42 seconds to run if I do an UPDATE using T-SQL. If I edit the row in SQL Server Management Studio, the row updates instantly. Triggers are executed in the edit window as well as on T-SQL UPDATES, aren't they?
The SQL code in the stored procedure comes back instantly if I run it directly or call it using EXEC, the only circumstances when it runs slowly are when the trigger is called by an UPDATE statement.
It depends how the trigger was set up, Triggers will only run on Update, Delete and Insert statements (Depending which of the three are chosen) On the table it is set against.
Could you give the code used to create the trigger?
These triggers run after an insert, update or delete on a table
click here for details about different type of triggers and demo for triggers

Trigger to prevent updates but allow inserts

Can SQL Server triggers prevent updating records and allow inserting new records?
Thanks,
Przemek
Couldn't this be accomplished by creating a SQL Server user that has INSERT access on the destination tables but not UPDATE access? Create a new user with no permissions, then:
GRANT INSERT ON [blah] TO [user]
I admit, my knowledge of SQL Server triggers is limited, but this seems like it would work.
To expand on INSTEAD OF UPDATE, this trigger will fire for UPDATE commands. INSERT and DELETE commands work as usual; if you wanted to intercept either of those you'd use an INSTEAD OF INSERT or INSTEAD OF DELETE trigger.
The full solution goes something like this:
CREATE TRIGGER myTable_IO_Upd ON myTable INSTEAD OF UPDATE AS
BEGIN
RAISERROR('myTable does not allow UPDATE queries.', 18, 0);
END;
GO
More information on RAISERROR is here. Basically the second argument (18) is the severity, with 18 being the highest severity allowed for user-defined exceptions. The third argument (0) is the "state", which you don't normally need, hence the zero.
One more example: if you wanted to prevent UPDATE and DELETE, you'd use something like this:
CREATE TRIGGER myTable_IO_Upd ON myTable INSTEAD OF UPDATE, DELETE AS
BEGIN
RAISERROR('myTable does not allow UPDATE or DELETE queries.', 18, 0);
END;
GO
To prevent update on table, you can use "INSTEAD OF UPDATE" Trriger
you can check this note "https://msdn.microsoft.com/en-us/library/aa258254%28SQL.80%29.aspx?f=255&MSPPError=-2147217396"

prevent updates, deletes and truncates on database table

I am in the process of creating a sql server 2008 database table for auditing users actions.
Is it possible to create a database table which can only inserted in to - no truncates, deletes or updates allowed on the data in the table. One option I know of is to use a different user with limited rights, but this isnt option for me. So looking at other options?
You need to create a TRIGGER that fires on UPDATE and DELETE and throws an error:
CREATE TRIGGER user_action_update on UserActions FOR UPDATE, DELETE AS
BEGIN
RAISERROR ('Cannot modify or delete user actions', 16, 1)
ROLLBACK TRAN
RETURN
END
GO
Source: http://msdn.microsoft.com/en-us/magazine/cc164047.aspx
Another way to do that is to Write a trigger creation script for the table and set the action to " INSTEAD OF " which will override the triggering action (unwanted action in your case ) for some other code, or null code.
INSTEAD OF Property
Specifies that the DML trigger is executed instead of the triggering SQL statement, therefore, overriding the actions of the triggering statements.
Here is a link in how to Write the SQL statement for the trigger creation:
http://technet.microsoft.com/en-us/library/ms189799.aspx
Good luck
Adrian

Get caller of trigger in SQL Server

Can you advice me how I can retrieve caller ID or Name which executed a trigger?
For example I want to know which SP executes a trigger or maybe trigger called by updates from management studio?
I know that ##PROCID returns ID of trigger and can't be used.
Also I know solution when in SP we write CONTEXT_INFO and read it from trigger.
But in this case we should SET CONTEXT_INFO in all SPs that modifies some table.
Is there exists some simplest way like ##PROCID ?
If it's SQL Server 2005 or 2008, and a DDL trigger, you can use eventdata().
Here's a link to the msdn page.
Basically it returns an XML dataset that you can parse to get things like who called it (data(/EVENT_INSTANCE/LoginName)[1]), what the command was, etc.

Stopping a delete on MySQL via Triggers

I'm trying to intercept any DELETE commands against a particular table. MySQL supports triggers but it doesn't seem to support a way to raise an error yet like SQL Server and other databases.
Can I do this with just an empty Trigger definition? Something like:
create trigger trListsDelete on lists
instead of delete
as
begin
/* Do nothing */
end
In SQL Server I could add a RAISEERROR('You cannot delete lists.') statement to force it to fail and that way I know the Delete wouldn't be executed. Since MySQL doesn't support raising errors, how do I simply ignore the Delete command?
The technique is you do something that will cause an error, such as update a column that doesn't exist.
Details here: MySQL Triggers: How do you abort an INSERT, UPDATE or DELETE with a trigger?