INSERT UPDATE Trigger Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • imani_technology@yahoo.com

    INSERT UPDATE Trigger Question

    How would I write a trigger that updates the values of a Description
    column to upper case for even IDs and to lower case for odd IDs? I
    need this trigger to fire for INSERT and UPDATE events.
  • John Bell

    #2
    Re: INSERT UPDATE Trigger Question

    Hi

    Try something like:

    CREATE TABLE Test ( id int not null identity (1,1), Description char(10) )

    CREATE TRIGGER Test_Insert ON Test FOR INSERT AS
    UPDATE TEST SET Description = CASE Id%2 WHEN 0 THEN UPPER(Descripti on) ELSE
    LOWER (Description) END

    INSERT INTO TEST ( Description ) VALUES ('One')
    INSERT INTO TEST ( Description ) VALUES ('Two')
    INSERT INTO TEST ( Description ) VALUES ('Three')
    INSERT INTO TEST ( Description ) VALUES ('four')
    INSERT INTO TEST ( Description ) VALUES ('five')
    INSERT INTO TEST ( Description ) VALUES ('SIX')
    INSERT INTO TEST ( Description ) VALUES ('SEVEN')

    SELECT * from Test


    John

    <imani_technolo gy@yahoo.com> wrote in message
    news:f9208446.0 309011615.5f269 625@posting.goo gle.com...[color=blue]
    > How would I write a trigger that updates the values of a Description
    > column to upper case for even IDs and to lower case for odd IDs? I
    > need this trigger to fire for INSERT and UPDATE events.[/color]


    Comment

    • Simon Hayes

      #3
      Re: INSERT UPDATE Trigger Question

      imani_technolog y@yahoo.com wrote in message news:<f9208446. 0309011615.5f26 9625@posting.go ogle.com>...[color=blue]
      > How would I write a trigger that updates the values of a Description
      > column to upper case for even IDs and to lower case for odd IDs? I
      > need this trigger to fire for INSERT and UPDATE events.[/color]

      You might want to consider formatting the text on the client, when you
      retrieve it from the database - presentation tasks don't really belong
      in a database. But if you want to do it using a trigger, something
      like this should work (assuming your 'ID' is an integer key column):

      create trigger ATR_UI_MyTable
      on dbo.MyTable after insert, update
      as
      update dbo.MyTable
      set DescriptionColu mn =
      case i.IDColumn % 2
      when 1 then lower(i.Descrip tionColumn)
      when 0 then upper(i.Descrip tionColumn)
      end
      from dbo.MyTable t
      join inserted i
      on t.IDColumn = i.IDColumn

      Simon

      Comment

      Working...