Conditional insert?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hoonew

    Conditional insert?

    I need to do in SQL the following psuedo-code, and I'm stuck:

    IF ((COUNT(*) FROM a WHERE column_a=1)>0)
    THEN
    INSERT INTO TABLE b
    ENDIF
  • Bill Karwin

    #2
    Re: Conditional insert?

    hoonew wrote:[color=blue]
    > I need to do in SQL the following psuedo-code, and I'm stuck:
    >
    > IF ((COUNT(*) FROM a WHERE column_a=1)>0)
    > THEN
    > INSERT INTO TABLE b
    > ENDIF[/color]

    create table a (column_a integer);
    create table b (foo varchar(10));
    insert into a (column_a) values (1), (1);

    insert into b (foo)
    select distinct 'foo' as foo
    from a where column_a = 1;
    ....one record inserted, because of distinct.

    insert into b (foo)
    select distinct 'foo' as foo
    from a where column_a = 2;
    ....zero records inserted, because no records in `a` match.

    Bill

    Comment

    Working...