9

Can someone post sample code or tell me how to set up a listener to notify me (trigger an event) if a new row is inserted into a SQL Server database?

I don't want to use any of the SQL Server messaging or broker services. I currently have a file listener which will notify me when new rows are appended to a flat file. I would like to do the same for a database table.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
greg
  • 121
  • 2
  • 2
  • 3

7 Answers7

9

Are you perhaps looking for the SqlDependency class, that lets your code register to be notified when changes occur?

SqlDependency is ideal for caching scenarios, where your ASP.NET application or middle-tier service needs to keep certain information cached in memory. SqlDependency allows you to receive notifications when the original data in the database changes so that the cache can be refreshed.

Or does this fall within the realm of things that you're disallowing, it's not entirely clear?

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • 1
    SqlDependency uses the brooker service. But the brooker service is always enabled on the server anyway, you should just enable it on the database where sqldependency is used. – Softlion Feb 27 '12 at 10:47
2

You would need to do some sort of database trigger on insert or poll the database regularly to look for new records. I don't recommend the latter suggestion as that can be very performance intensive.

Other than that, you aren't giving us much to go on. What version of SQL Server are you using? What have you tried already? What problems have you encountered?


Here are a few links that might point you in the right direction:

Exploring SQL Server Triggers

How to: Create and Run a CLR SQL Server Trigger

A similar question that suggests an alternative (better?) way of doing this: Can SQL CLR triggers do this? Or is there a better way?

Community
  • 1
  • 1
Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • 1
    on two different occasions, I had programmers write a listener that didnt use any of the database built in functionality. There must have been a class in java and c# that allowed a FileSystemWatcher-like funcationality for a database. So, its definitely possible to do, and its in realtime. I have only implemented a file listener so far, but I would prefer to listen to a sql table. – greg Jun 16 '11 at 00:36
  • 1
    Interesting. I'm not aware of anything like that that doesn't use any of the database engine features for notification. I honestly don't believe there is anything that can do real time notifications without polling a database table on a regular basis. Good luck though. – Kyle Trauberman Jun 16 '11 at 00:49
  • 1
    @greg I'm interested in hearing about how you solve this problem. Can you add a comment to my question when you figure it out? – Kyle Trauberman Jun 16 '11 at 05:23
1

You can use sql notifications to do this but you said you don't want to use the broker. Otherwise you can poll, but as mentioned this can cause performance issues.

Another way to do this is using triggers on the database table to touch a file on the filesystem.

exec master..xp_cmdshell 'echo changed > c:\temp\filewatcher.txt'

Then use a FileSystemWatcher as you mentioned you are already doing in your application to receive a notification when the file changes.

There are security permissions that you will need to grant to your Sql Server user to make this possible but if that is acceptable then this will work without using the broker.

Phill
  • 1,302
  • 1
  • 9
  • 18
  • 1
    The problem is that I dont have access to any settings on the sql server and I have limited access. So, I just want a listener similar to FileSystemWatcher for databases. I know this is possible because I have had two different developers write processes previously. One is java and the other in c#... thanks for your input..much appreciated. – greg Jun 15 '11 at 22:55
1

I also recommend trigger that registers inserts somewhere in db (and you must poll database anyway, which is not so expensive if the trigger modifies a special table with a single row). But if you have IDENTITY on your primary key, you can monitor the value of current identity of the table:

SELECT IDENT_CURRENT('TableName')

It's a hack, however, but it's fast and you don't need to modify your database (of course, it works only for insert operations). However you may skip insert operations if between polls identity value was modified explicitly and was set exactly to the value it was at the moment of previous polling (which is not very probable though).

  • 1
    BTW, identity value may change even if there was no modifications on the table (when the transaction is rolled back, identity value is not restored). – Nicolai Shestakov Jun 16 '11 at 06:57
0

I would consider CLR trigger, Please read following articles...

Create Your First CLR Trigger in SQL Server

CLR Triggers

Behzad
  • 857
  • 1
  • 8
  • 27
0

As per "Damien" you need to use the sqldependency to detect changes.

sample for sqldependency at msdn:- https://msdn.microsoft.com/en-us/library/62xk7953(v=vs.110).aspx

PRathore
  • 93
  • 1
  • 4
-3

Trigger sounds best option:

create TRIGGER [tI_Notifier] ON [dbo].[your_table_name] AFTER INSERT
AS 
BEGIN
    SET NOCOUNT ON;

    declare @id1 int --or same type as your key
        --declare other variables you want to read from the inserted row

        --read columns values from inserted row
    select @id1 = <some_key_column>, @id2=<second_column> from inserted

    --do something with row's new values

    SET NOCOUNT OFF;
END
kheya
  • 7,546
  • 20
  • 77
  • 109