如何用一条sql实现当b表有某条记录时才在a表中加一条新记录?
谢谢

解决方案 »

  1.   

    if exists (select 1 from b表 where 某条记录)
    insert into a表 ....
      

  2.   

    insert into a表 ....
    where exists (select 1 from b表 where ...)
      

  3.   

    好象不行,我用的是sql2000
    报的错是
    服务器: 消息 156,级别 15,状态 1,行 1
    在关键字 'where' 附近有语法错误。
      

  4.   

    To:LZ  是否这样---创建测试数据
    declare @b table(id int,name varchar(20))
    declare @a table(name varchar(20))
      insert @b select 1,'张三'
      union all select 2,'李四'
      union all select 3,'王五'
    ---查看测试数据
    select * from @a
    select * from @b
    ---测试假如当@b表存在一个姓名为王五的就将其插入到@a中
    if exists(select 1 from @b where name='王五')
       insert @a select name from @b where name='王五'
    else
       print '@b表中不存在name为王五'
    ---查看结果
    select * from @a