Tuesday, June 15, 2010

Avoid trigger error for text,ntext,image data types in SQL server

SQL server triggers do not work with text,ntext and image data types. So avoid these like this...

Select COLUMN_NAME

FROM INFORMATION_SCHEMA.columns

WHERE TABLE_NAME = 'tbl_Name'

AND CAST(INFORMATION_SCHEMA.columns.DATA_TYPE AS nVARCHAR(100)) not IN (CAST('text' AS nVARCHAR(100)),CAST('ntext' AS nVARCHAR(100)),CAST('image' AS nVARCHAR(100)))

AND COLUMN_NAME NOT in

(select c.COLUMN_NAME

from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,

INFORMATION_SCHEMA.KEY_COLUMN_USAGE c

where pk.TABLE_NAME = 'tbl_Name'

and CONSTRAINT_TYPE = 'PRIMARY KEY'

and c.TABLE_NAME = pk.TABLE_NAME

and c.CONSTRAINT_NAME = pk.CONSTRAINT_NAME)

No comments:

Post a Comment