Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SqlDataAdapter.Update hitting SQL Server trigger UPDATE() check

$
0
0

Hello,

Here's my situation...I have a .NET v3.5 windows console application that uses SqlDataAdapters to add new records to a disconnected dataset and then calls .Update() on the adapater to insert all new records into a SQL Server 2008 R2 table. This currently works and has been working fine for years.

A new business rule has come up and 2 of the table columns should no longer be used...the old data should be kept for historical/reporting purposes but moving forward no one should insert/update either of these 2 columns.

I decided to use a trigger to kick back a message, like this:

CREATE TRIGGER [dbo].[TRIGGER_NAME]
ON [dbo].[TABLE_NAME]
AFTER UPDATE, INSERT
AS

	IF (UPDATE(COLUMN_1) OR UPDATE(COLUMN_2))
	BEGIN
		RAISERROR('The XXXX and YYYY columns cannot be updated or inserted into - they are deprecated.', 16, 1)	;
		ROLLBACK;
	END;

GO

I removed the 2 columns from the code that creates the DataSet and Adapter so that they aren't in the DataTable. When I make a new Row for the DataTable, neither of those 2 columns are represented. I assumed, the Adapter would insert NULLs into the fields but it causes the trigger error to fire and roll back the insert.

I tried adding the 2 columns back into the DataSet/DataTable code and setting the new row values to System.DBNull.Value but that also kicks off the trigger.

Finally I rewrote the trigger like this:

CREATE TRIGGER TRIGGER_NAME
ON dbo.TABLE_NAME
AFTER UPDATE, INSERT
AS
	IF EXISTS(SELECT COLUMN_1
	FROM TABLE_1 T
	JOIN inserted I
	ON T.PK = i.PK
	WHERE I.COLUMN_2 IS NOT NULL
	OR I.COLUMN_3 IS NOT NULL)
	BEGIN
		RAISERROR('The COLUMN_2 and COLUMN_3 columns cannot be updated or inserted into - they are deprecated.', 16, 1)	;
		ROLLBACK;
	END;
GO
But it STILL KICKS OFF THE TRIGGER!!!

What is the deal with the SqlDataAdapter?? Is there any way to have it not affect the 2 columns?

Thanks, George


George H.


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>