Creating a trigger that manipultes a field in SQL Server
Guys, I have a table called tblNames and one of my fi开发者_如何转开发elds 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.
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
精彩评论