Link to home
Start Free TrialLog in
Avatar of jayme9
jayme9

asked on

Using "Contains" in a trigger

I have a trigger that audits changes by posting to an audit table when changes to other tables are made.  It works great; however, now I must isolate this to happen only to those records where the travelrequest_no field contains "-U-" while ignoring the other records.
I put a
WHERE CONTAINS(travelrequest_no, '"*-U-*"') after the FROM line below.  The trigger is successfully saved, but does not execute when I insert new records.  What am I doing wrong?  Perhaps another way of doing this?
--------------------------------------------------code---------------------------------
CREATE TRIGGER AuditVisit ON dbo.tbl_travelrequests
FOR INSERT
AS
/* * INITIALLY SETS THE TRANSFER FLAG OFF AND INSERTS OVERALL LAST MODIFICATION DATE IN 'tbl_auditForTransfer' */

BEGIN
   INSERT INTO [dbo].[tbl_auditForTransfer] (travelrequest_no, LastModifiedDate, TransferFlag)
   SELECT travelrequest_no, getDate(), 'OFF'
   FROM inserted
 
END




Thank you,
Jayme
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

The wildcard character in SQL is % not *
oops...i was thinking of LIKE not CONTAINS
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The syntax for CONTAINS only supports prefix searches as in:
WHERE CONTAINS(travelrequest_no, '"-U-*"')
BriCrowe,

>>speaking of which why don't you use LIKE?<<
CONTAINS is indexed and should be a lot faster than a sequential search using LIKE.  However, in this case the questioner may not have much choice.