Using IF ELSE statement based on Count to execute different Insert statements

后端 未结 8 910
灰色年华
灰色年华 2020-12-01 08:40

While I am searching through my database, I run an INSERT statement if I find that a particular item does not exist, and I run a different INSERT statement if I find one or

相关标签:
8条回答
  • 2020-12-01 09:31

    Simply use the following:

    IF((SELECT count(*) FROM table)=0)
    BEGIN
    
    ....
    
    END
    
    0 讨论(0)
  • 2020-12-01 09:41

    IF exists

    IF exists (select * from table_1 where col1 = 'value')
    BEGIN
        -- one or more
        insert into table_1 (col1) values ('valueB')
    END
    ELSE
        -- zero
        insert into table_1 (col1) values ('value') 
    
    0 讨论(0)
提交回复
热议问题