Cannot use text, ntext, or image columns in the 'inserted' and 'deleted' tables.
What should be the workaround in this case? :(
In DML triggers, the inserted and deleted tables are primarily used to perform the following: Extend referential integrity between tables. Insert or update data in base tables underlying a view. Test for errors and take action based on the error.
An inserted table is a pseudo-table containing rows with the inserted values of an insert statement, and a deleted table is a pseudo-table containing rows with the updated values (after image) of an update statement.
Inserted and deleted are the magic tables in the SQL Server trigger that used to manage pre-updated and post updated row.
Solution. In addition to the LEN() function, SQL Server also has a DATALENGTH() function. This function can be used on all data types in your table. That's all there is to it.
As of SQL Server 2005, TEXT/NTEXT/IMAGE
are deprecated - you should use the (N)VARCHAR(MAX)
and VARBINARY(MAX)
data types instead.
(N)VARCHAR(MAX) (see MSDN docs here) and VARBINARY(MAX) allow up to 2 GByte of data
From the MSDN docs:
nvarchar [ ( n | max ) ]
Variable-length Unicode character data. n can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes. (= 2 GB)
The (N)VARCHAR(MAX) types also allow all the usual T-SQL string function to work on them - something that wasn't the case with (N)TEXT at all.
As this MSDN article shows, the replacement types are supported in triggers, too:
SQL Server 2008 does not allow for text, ntext, or image column references in the inserted and deleted tables for AFTER triggers. However, these data types are included for backward compatibility purposes only. The preferred storage for large data is to use the varchar(max), nvarchar(max), and varbinary(max) data types. Both AFTER and INSTEAD OF triggers support varchar(max), nvarchar(max), and varbinary(max) data in the inserted and deleted tables.
This is the workaround:
SQL Trigger cannot do INSTEAD OF DELETE but is required for ntext, image columns
Essentially you need to join the inserted
and/or deleted
pseudo-tables onto the underlying table and read the NTEXT
, TEXT
or IMAGE
data from the underlying tables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With