Results 1 to 2 of 2

Thread: Trigger Help

  1. #1
    Join Date
    Feb 2015
    Posts
    1

    Unanswered: Trigger Help

    I am hoping I can get some help since sybase syntax is sensitive. I have a table, table A that gets data from an excel file, this is no problem. The fie is uploaded and the table, table A is populated. However after all the inserts are done, I wish to check one of the columns in table A, username for example and verify that the username just inserted into table A exists in table X. If it does not exist then run an insert statement for X to get all those usernames in Table A that were not in X. This trigger will be for Table A, after an insert is executed.

  2. #2
    Join Date
    Sep 2002
    Location
    Hong Kong
    Posts
    159

    With apologies to Hanna-Barbera

    Hi,

    I created the following to illustrate how. The use of an outer-join in the trigger is more efficient than a not-in.


    create table A ( username varchar(40) not null)
    go

    create index idx1 on A ( username )
    go

    create table X ( username varchar(40) not null)
    go

    create index idx1 on X ( username )
    go

    insert into X (username) values ('fred')
    insert into X (username) values ('wilma')
    insert into X (username) values ('barny')
    insert into X (username) values ('betty')
    insert into X (username) values ('dino')
    insert into X (username) values ('bambam')
    go

    create trigger A_ins on A for insert, update as

    begin

    insert into X (username)
    select
    I.username
    from
    inserted I
    left outer join X on
    I.username = X.username
    where
    X.username is null

    end
    go

    select username as Xusername from X
    go

    select username as A_username from A
    go

    insert into A (username) values ('fred')
    go
    insert into A (username) values ('MR Stone')
    go

    select username as X_username from X
    go

    select username as A_username from A
    go

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •