views:

54

answers:

1

Guys, I have a table called tblNames and one of my fields in this table is called 'UpFileName'. Is it possible to create an insert trigger that would automatically replace all '%20' in the UpFileName field to underscores '_'?

I'm using SQL Server 2005.

+3  A: 

In general it is like this, replace ID with the PK of your table

CREATE TRIGGER  trTriggerName  ON  tblNames   
 AFTER INSERT  AS  
UPDATE tblNames 
   SET UpFileName = replace(UpFileName,'%20','_') 
FROM tblNames t
JOIN INSERTED i ON t.ID = i.ID

However if you don't want people to insert certain types of data then use check constraints. Right now you are doing extra work for every inserts because this trigger fires

SQLMenace