1

Using Azure Sql Server, and in C# if that's relevant, is there a way to get an event when data in specific table changes?

In one table we need an event if any column changes in a row.

In a 2nd table we need an event only when either of 2 columns change (although any change would be ok).

And we need to know which row.

Is it possible to do this? If so, how?

Update: We need a call into our C# code for this event. A database trigger where the database can do something doesn't help, our program needs to take action on a change.

thanks - dave

2

2 Answers 2

1

Windows Azure SQL Database (WASD) does partially support triggers so you could roll your own auditing. See here. Here's a really simple example:

CREATE TABLE dbo.Users ( 
    userId INT IDENTITY PRIMARY KEY,
    userName VARCHAR(30) NOT NULL UNIQUE,
    dateAdded DATETIME NOT NULL DEFAULT GETDATE(),
    addedBy VARCHAR(30) NOT NULL DEFAULT 'admin',
    dateModified DATETIME NULL,
    modifiedBy VARCHAR(30) NULL,
)
GO

INSERT INTO dbo.Users ( userName )
VALUES ( 'wBob' ), ( 'David' )
GO

CREATE TRIGGER dbo.utrg_Users ON dbo.Users
FOR UPDATE
AS 
BEGIN

    -- INSERT INTO ... other table
    SELECT 'old record' source, *
    FROM deleted

END
GO

UPDATE dbo.Users 
SET userName = 'wDavid'
WHERE userName = 'David'

WASD has also recently previewed an Audit feature which does include DML auditing. It's only available for the Basic, Standard and Premium editions and writes off to an audit log stored on an Azure storage account (not a conventional relational table) so you could also have a look at that. Looks like an interesting feature and includes an Excel template for quickly analysing the logs. Start here.

4
  • Sorry, I wasn't clear. I need an event that calls my C# code, not just a trigger in the DB. thank you Sep 21, 2014 at 19:41
  • 1
    Well then no is the short answer. WASD does not support CLR. You could however write some code which polls either of the above audit logs to achieve the same effect. Or in the C# code the writes the original record, also write the audit record.
    – wBob
    Sep 21, 2014 at 19:56
  • That's what I was afraid of. So, better to watch the audit log and parse it? Or have it create records in a table that is the PKs of all changed records and I do a select for all records in that table (and then delete entries)? Sep 21, 2014 at 20:14
  • Well I have mentioned a different pattern, where the code that writes the main record also writes the audit log. Doesn't that sound simpler? Maybe there will be some other suggestions.
    – wBob
    Sep 21, 2014 at 20:20
0

A WebJob or function in the app service could be bound to the blob storage container which contains the audit log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.