Sql Server Triggers. How does the trigger checks for Update Command? - sql

I have a trigger for auditing purchase and sales table. The trigger is on INSERT and DELETE. To check if its "Insert", the condition is
IF (SELECT COUNT(*) FROM INSERTED) > 0
BEGIN
END
How do I check if its an Update command inside the Trigger?
Arun

The "tables" that are in play in an update trigger are still called inserted and deleted, and the old values of the rows are in the deleted table, and the new values are in the inserted table.
You can use logic along these lines to detect whether you are in a insert, update or a delete:
CREATE TRIGGER MyTrigger
ON MyTable
AFTER INSERT,DELETE,UPDATE -- you need to add the "update" here,
-- in order to catch updates as well
AS
BEGIN
Declare #insertedCount int
Declare #deletedCount int
select #insertedCount = COUNT(*) from inserted
select #deletedCount = COUNT(*) from deleted
if (#insertedCount != 0) -- we have some new values
if (#deletedCount = 0) -- do we have old values?
print 'Inserting'
else
print 'Updating'
else
print 'Deleting'
END

An INSERT,DELETE trigger will not fire for updates.
.........................

CREATE an INSERT, UPDATE, DELETE all in one table
IF ##ROWCOUNT = 0
BEGIN
RETURN;
END;
IF EXISTS(SELECT * FROM inserted)
BEGIN
IF EXISTS(SELECT * FROM deleted)
BEGIN
-- for UPDATE
END
ELSE
BEGIN
-- for INSERT
END
END
ELSE
BEGIN
-- for DELETE
END;

Once you've updated your trigger (as Mitch says) to also apply to UPDATEs, you need to check for updates first:
IF EXISTS(select * from inserted) and EXISTS(select * from deleted)
BEGIN
--Update
END
Note that I've switch to EXISTS rather than COUNT(*). Exists more properly describes what we want to establish - that rows exist in the table. We don't care how many rows are in there.

I would like to thank "SWeko" for the clue.
Now, this what I did and the Trigger worked.
CREATE TRIGGER dbo.Sample
ON TableName
FOR INSERT, UPDATE, DELETE
AS
IF (SELECT COUNT(*) FROM INSERTED) > 0
BEGIN
IF (SELECT COUNT(*) FROM DELETED) > 0
BEGIN
-- UPDATE (TABLE).
END
ELSE
BEGIN
-- INSERT (TABLE).
END
END
ELSE
-- DELETE (TABLE).
BEGIN
END

Related

SQL Trigger prevent delete on single row

I have a table where there is a row of data that I don't want anyone to deleted it. The Table name is ProjectInProcessData. I want to make sure the data with ID 6050 can not delete. How can I do that?
Create Trigger [dbo].[triggerPreventDeleteFormula]
ON [dbo].[AHSC_Project_InprocessData]
INSTEAD OF DELETE
AS
BEGIN
IF EXISTS(
SELECT *
FROM deleted d
where d.AHSC_Project_InprocessData_ID = '6610' or d.AHSC_Project_InprocessData_ID = '6666'
)
BEGIN
ROLLBACK;
RAISERROR('Can not delete this record: this record contains default formula',16,1);
END
ELSE
BEGIN
DELETE [AHSC_Project_InprocessData]
WHERE EXISTS (Select * from deleted d where d.AHSC_Project_InprocessData_ID = [AHSC_Project_InprocessData].AHSC_Project_InprocessData_ID)
END
END
use where condition in times of delete
delete from ProjectInProcessData where id!=6050

sql DELETE trigger won't rollback

The trigger is supposed to rollback if the statement in the SELECT exists. The thing is, it doesn't. When I only run the SELECT from the trigger, it shows that a row exists. But when I try DELETE with the same values as I have hard coded in the trigger, the trigger fires but it does not do a rollback. Anyone got any ideas what might be wrong?
CREATE TRIGGER trg_del ON Projektbefattningar
FOR DELETE
AS
SELECT #befNr = Befattningsnr, #pNr = pNr, #EtappNr = Etappnr FROM deleted
-- Not currently using these. Using hard coded values to illustrate my problem
IF EXISTS (
SELECT *
FROM projektbefattningar
WHERE Befattningsnr = 2 AND pNr = 1 and Etappnr = 1 AND Anställningsnr is not null
)
BEGIN
RAISERROR('Could not delete, Anställningsnr is not null', 16, 1)
--THROW 50001, 'Could not delete, Anställningsnr is not null', 1;
ROLLBACK TRANSACTION;
END
GO
When you use FOR DELETE you consider that this TRIGGER will fired after the delete statement is complete. To Verify a condition before DELETE you need to use INSTEAD OF DELETE. Your Trigger declaration will be:
CREATE TRIGGER trg_del ON Projektbefattningar
INSTEAD OF DELETE
AS
Now To Confirm your delete statement you need to include the delete alghouth it will not be deleted.
The final procedure will be like this:
CREATE TRIGGER trg_del ON Projektbefattningar
INSTEAD OF DELETE
AS
SELECT #befNr = Befattningsnr, #pNr = pNr, #EtappNr = Etappnr FROM deleted
-- Not currently using these. Using hard coded values to illustrate my problem
IF EXISTS (
SELECT *
FROM projektbefattningar
WHERE Befattningsnr = 2 AND pNr = 1 and Etappnr = 1 AND Anställningsnr is not null
)
BEGIN
RAISERROR('Could not delete, Anställningsnr is not null', 16, 1)
--THROW 50001, 'Could not delete, Anställningsnr is not null', 1;
ROLLBACK TRANSACTION;
END
ELSE
BEGIN
DELETE FROM YourTableToDelete Where idOfYourTable in ( Select idOfYourTable from Deleted )
END
GO
Try this
CREATE TRIGGER trg_del ON Projektbefattningar
FOR DELETE
AS
SELECT #befNr = Befattningsnr
,#pNr = pNr
,#EtappNr = Etappnr
FROM deleted
-- Not currently using these. Using hard coded values to illustrate my problem
IF EXISTS (
SELECT 1
FROM deleted
WHERE Befattningsnr = 2
AND pNr = 1
AND Etappnr = 1
AND Anställningsnr IS NOT NULL
)
BEGIN
RAISERROR (
'Could not delete, Anställningsnr is not null'
,16
,1
)
--THROW 50001, 'Could not delete, Anställningsnr is not null', 1;
ROLLBACK TRANSACTION;
END
GO

Record Counts in Stored Procedures

I have a SP that INSERT INTO TBL_DOMAIN from TBL_STAGING, but first I want check table TBL_STAGING to make sure the table is not empty before I truncate table TBL_DOMAIN, if table TBL_STAGING got more than one record then proceed the truncate table TBL_DOMAIN then run the INSERT, ELSE message say the table TBL_STAGING is EMPTY and exit the SP. My goal is to make sure the table TBL_DOMAIN still have the data even is old. I'm very new SQL please help.
CREATE PROCEDURE [dbo].[SP_INSERT_ALL_DOMAIN]
WITH EXECUTE AS CALLER
AS
BEGIN
BEGIN TRANSACTION NT_ALL_DOMAIN
INSERT INTO TBL_DOMAIN
(DOMAIN_NAME,
DISTINGUISHED_NAME,
EMAIL_ADDR_I)
SELECT DOMAIN_NAME,
DISTINGUISHED_NAME,
EMAIL_ADDR_I
FROM TBL_STAGING
First you need to check if TBL_STAGING has data:
IF EXISTS (SELECT TOP 1 1 FROM TBL_STAGING)
BEGIN
BEGIN TRANSACTION NT_ALL_DOMAIN
INSERT INTO TBL_DOMAIN
(DOMAIN_NAME,
DISTINGUISHED_NAME,
EMAIL_ADDR_I)
SELECT DOMAIN_NAME,
DISTINGUISHED_NAME,
EMAIL_ADDR_I
FROM TBL_STAGING
COMMIT
END
ELSE
BEGIN
RETURN 'no data on table'
END

T-SQL trigger after deleting

A need a trigger to run after a row is deleted from a table.
This trigger should delete rows from another table where a field on that table has a value matching the value of one of the columns of the deleted row from the other table.
If triggering the delete from a delete:
create trigger trg_mainTable
on dbo.mainTable
after delete
as
begin
delete
from dbo.relatedTable
where someColumn in
(
select someColumn
from deleted
)
end
go
If triggering the delete from an update:
create trigger trg_mainTable
on dbo.mainTable
after update
as
begin
if update(someColumn)
begin
delete
from dbo.relatedTable
where someColumn in
(
select someColumn
from deleted
)
end
end
go

Create Database trigger for custom ChangeTable

I'm trying to create a database trigger in SQL Server 2008 for a tblCustomer table. I need it to add a new row into the tblChanges table every time there is an insert, update, or delete within the tblCustomer table.
Specifically, I need it to insert the CustomerId (PK) that was changed, the dateTime that it the changed occurred, and the type of change (insert, update, delete).
I've got something along these lines so far but can't figure out the rest:
CREATE TRIGGER change_trigger
AFTER INSERT OR UPDATE OR DELETE
ON tblCustomer
DECLARE log_action varchar(30)
BEGIN
IF INSERTING THEN
log_action := 'I';
ELSEIF UPDATING THEN
log_action := 'U';
ELSEIF DELETEING THEN
log_action := 'D';
ELSE
DBMS_OUTPUT.PUT_LINE('undefined');
END IF;
INSERT INTO tblChanges(ChanedPK, ChangedTime, ChangedType)
VALUES ...
I'm unsure if any of the above SQL is correct as I haven't tried running it yet and my knowledge of SQL is limited. Any help completing the code and correcting errors would be appreciated.
Try something along these lines:
CREATE TRIGGER change_trigger AFTER INSERT, UPDATE, DELETE
ON tblCustomer
AS
BEGIN
DECLARE log_action varchar(30)
IF EXISTS (SELECT * FROM INSERTED) AND NOT EXISTS(SELECT * FROM DELETED)
SET log_action = 'INSERT'
IF EXISTS NOT (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
SET log_action = 'DELETE'
IF EXISTS (SELECT * FROM INSERTED) AND EXISTS(SELECT * FROM DELETED)
SET log_action = 'UPDATE'
-- Your other code goes here
END
Maybe you should read up un T-SQL and DML triggers in SQL Server first. MSDN and many other sites provide excellent examples on how to go about things.