trigger syntax

I am new to the trigger function in sql server, so can I have some help with  the syntax in my trigger code  below (I am trying to create a trigger that finds the first 3 letters in th field country and it has a number in it (like 'CF3'?) move the to the postcode field and set the country field as null)

CREATE TRIGGER [testtrigger] ON [dbo].[Business]
FOR  UPDATE
AS

BEGIN
DECLARE @i int
DECLARE @c char(1)


SET @i=1
WHILE @i<=3
      BEGIN
            SET @c=SUBSTRING(b.Country,@i,1)
            IF @c LIKE '[0-9]'
                  update Business set postcode = a.country
                        update Business set country = null
            SET @i=@i+1
      END
from Business    a
                join inserted                     b on (b.BusinessNo = b.BusinessNo)

END
JakeyCakesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
CREATE TRIGGER [testtrigger]
ON [dbo].[Business]
FOR  UPDATE
AS
IF @@ROWCOUNT = 0
      RETURN
UPDATE Business
SET postCode = i.country,
    country = NULL
FROM Business b
INNER JOIN inserted i ON i.businessNo = b.businessNo
WHERE PATINDEX('%[0-9]%', i.Country) <= 3
0
JakeyCakesAuthor Commented:
Scott , the trigger still isn't doing what I want e.g. 'CF2 5PH' in Country is not moved to postcode and the country for that record not set to null
0
Scott PletcherSenior DBACommented:
Hmm, odd, looks good to me.  Are you sure?  Or are there other triggers that might be "undoing" this?

Oh, also, you might want to change only those rows where the country has changed as part of the UPDATE:


CREATE TRIGGER [testtrigger]
ON [dbo].[Business]
FOR  UPDATE
AS
IF @@ROWCOUNT = 0
     RETURN
IF UPDATE(country)
BEGIN
    UPDATE Business
    SET postCode = i.country,
        country = NULL
    FROM Business b
    INNER JOIN inserted i ON i.businessNo = b.businessNo
    INNER JOIN deleted d ON i.businessNo = d.businessNo
    WHERE PATINDEX('%[0-9]%', i.Country) <= 3 AND i.Country <> d.Country
END --IF
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:
D'OH, SORRY, forgot about "0" :-( :


WHERE PATINDEX('%[0-9]%', i.Country) BETWEEN 1 AND 3


0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.