Tuesday, March 27, 2012

Create a trigger to update a row that's been inserted or updated

Hi

Apologies if this is a silly question

I have a basic table "Customer" which has

Id

Address1

Address2

Address3

Town

County

Postcode

SearchData

After I insert or Update a row in this table I need to update the SearchData column

with

UPPER(ADDRESS1) + UPPER(ADDRESS2) + UPPER(TOWN) + UPPER(POSTCODE)

only for that Id

I'm sure this is only a basic update but all the examples I can find are for inserting into other tables not the row itself.

Regards

David

Instead of using the trigger better you can go with Computed Columns...

here it is,

Code Snippet

Createtable Customer

(

IdintNOTNULL,

Address1varchar(100)NOTNULL,

Address2varchar(100)NOTNULL,

Address3varchar(100)NULL,

Townvarchar(100)NOTNULL,

Countyvarchar(100)NULL,

Postcodevarchar(100)NOTNULL,

SearchDataasUPPER(ADDRESS1)+UPPER(ADDRESS2)+UPPER(TOWN)+UPPER(POSTCODE)PERSISTED --Persisted only used on SQL Server 2005

)

|||I would not recommend using the 'PERSISTED' keyword unless you are attempting to solve a particular problem.|||

Hi

That was really helpful.

Thanks

David

No comments:

Post a Comment