[quoted text, click to view] "Jonathan Furman" <jonremovemewest@msn.com> wrote in message news:<eWcp7H6rEHA.2580@TK2MSFTNGP15.phx.gbl>...
> Thank you for both of your replies. That pretty much clears it up for me. I
> can see the need for a "deferred trigger/constraint" so I'm not surprised
> that there is such a thing defined in ANSI SQL. It sure would simplify
> procedure coding to not have to juggle data as the system transitions from
> state A to state B just to stay consistant when only the initial and final
> states are what's really important. Maybe in the future...then. OK I will
> juggle..I still refuse to use cursors to do it though!
>
> Thanks again.
>
> Jon
May I make a suggestion. I have faced this problem before, and the
following solution has occurred to me, although I have never
implemented it:
Suppose I have the following DB:
CREATE TABLE Widget (WidgetID int primary key, WidgetAttachment int,
Weight float)
CREATE TABLE Attachment (AttachmentID int primary key, Weight float)
And I want to ensure, at the end of each transaction, that the Weight
of the attachment is less than the weight of the widget; but allow
inconsistencies within a transaction. I could do something like this:
CREATE TABLE #UpdatedWidgets (WidgetID int)
CREATE TABLE #UpdatedAttachments (AttachmentID int)
I could then put triggers on the Widget and Attachment tables which
would insert rows into #UpdatedWidgets, #UpdatedAttachments, with the
ID of the attachment/widget affected by the DML operation...
Finally, instead of calling COMMIT directly, I create a stored
procedure sp_COMMIT, which works as follows:
For every entry in #UpdatedWidgets, #UpdatedAttachments check that the
relevant entries in the Widget and Attachment tables are consistent.
DELETE FROM #UpdatedWidgets
DELETE FROM #UpdatedAttachments
If yes,
COMMIT
RETURN 0
Else,
RAISERROR ('Consistency violation....',18,-1)
ROLLBACK
RETURN 1
As I said, I've never tried this, but it should work, so long as your
code always calls sp_COMMIT rather than COMMIT directly...
But I think it won't work if you are using distributed transactions
though (which is why I didn't use it in my case, because I was using
distributed transactions...)
Cheers
Simon Kissane