Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello All
I have trigger that does not work if i have "text" as datatype.
create TRIGGER [dbo].[UpdateAssetDeleteLog]
   ON  [dbo].[asset_device] 
   AFTER DELETE
AS 
BEGIN
		DECLARE @device_name varchar(500)
		DECLARE @device_ip_address varchar(100)
		DECLARE @device_type varchar(100)
		DECLARE @device_desc varchar(8000)
		DECLARE @first_discovered datetime
		DECLARE @last_discovered datetime
		DECLARE @asset_profile_id int
		DECLARE @deleted_by_user int

		DECLARE  CUR_DELETED 
		CURSOR FOR 
		SELECT asset_device_name,
		asset_device_ip_address, 
		asset_device_type [Device Type],
		asset_device_desc,
		asset_device_first_discovery,		
		asset_device_last_discovery AS [Last Discovered],
		asset_profile_id
		
		
		FROM deleted
		
        OPEN    CUR_DELETED
		FETCH FROM CUR_DELETED INTO @device_name,@device_ip_address,@device_type,@device_desc,@first_discovered,@last_discovered,@asset_profile_id
		
		WHILE (@@FETCH_STATUS = 0)
        BEGIN
		INSERT INTO dbo.asset_deleted_device_detail
		(
			[device_name],
			[device_ip_address],
			[device_type],
			[device_desc],
			[first_discovered],
			[last_discovered],
			[asset_profile_id],
			[deleted_by_user],
			[deleted_date]
		)
		VALUES
		(	
			
			@device_name,
			@device_ip_address,
			@device_type,
			@device_desc,
			@first_discovered,
			@last_discovered,
			@asset_profile_id,
			1,
			getdate()
		 )

		FETCH FROM CUR_DELETED INTO @device_name,@device_ip_address,@device_type,@device_desc,@first_discovered,@last_discovered,@asset_profile_id
			
        END
        CLOSE CUR_DELETED
		DEALLOCATE CUR_DELETED

END

The error message is like this.
Cannot use text, ntext, or image columns in the 'inserted' and 'deleted' tables

Can anyone help me?

Regards
Jesu
Posted
Updated 15-Dec-10 6:33am
v2
Comments
Abhinav S 15-Dec-10 12:33pm    
Code block added.

Easy to find if you just Google the error message and read a little bit:

SQL Server 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. For more information, see CREATE TRIGGER (Transact-SQL).
 
Share this answer
 
hi

A nice solution has been found:

SQL
SELECT FROM INSERTED just id column (it's not ntext or image and query is being executed).
SELECT from original table * with the same ids.
If required, use UPDATED() on INSERTED to be aware, what columns have been changed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900