Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Single Record Notification

Author  Topic 

sqlmyworld
Starting Member

38 Posts

Posted - 2013-07-18 : 19:38:55
Is there any way I can get noficiation when only one particular record of a table is updated/deleted. For business, the first record of a table let's say ID = 0 (record) is important and if some one updated that record then we will have some issues. I don't think a trigger can be created on a single record of a table. Is there any other solution for this scenario?

Thanks in advance.

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-07-18 : 19:43:53
You definitely could use a trigger. You can use @@ROWCOUNT combined with checking the inserted trigger table for the important record. If it finds that it occurred, you can write to a table and then you'd have a job that reads that table every minute and sends the email. Do not send email from the trigger, do it asynchronously so that the transaction completes as fast as possible.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

sqlmyworld
Starting Member

38 Posts

Posted - 2013-07-18 : 21:08:32
Thanks Tara. Will follow instructions.
Go to Top of Page

sqlmyworld
Starting Member

38 Posts

Posted - 2013-07-18 : 22:05:54
Hi Tata, one silly question - is that possible, only one record would be read only out of entire table rows. Let's say ID = 0 (record) is read only.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-19 : 01:04:50
quote:
Originally posted by sqlmyworld

Hi Tata, one silly question - is that possible, only one record would be read only out of entire table rows. Let's say ID = 0 (record) is read only.


you can create an instead of trigger to include logic that would avoid any modification to ID=0 record

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-19 : 01:21:26
[code]
CREATE TRIGGER Trg_DataModification
ON TableName
INSTEAD OF UPDATE
AS
BEGIN
IF EXISTS (
SELECT 1
FROM INSERTED
WHERE ID <> 0
)
UPDATE t
SET t.Col1=i.Col1,
t.Col2 = i.Col2,...
FROM Tbale t
INNER JOIN INSERTED i
ON i.PK = t.PK
AND i.ID <> 0
END

PK is the primary key
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

sqlmyworld
Starting Member

38 Posts

Posted - 2013-07-22 : 00:36:40
Thanks a lot Visakh
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-22 : 01:15:08
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -