MSSQL SQL查询的的问题2?如数据库表为
id name age
1  1    1
2  2    2现在相根据数据库的记录插入一条记录
如插入3,1,3
如name重复则不插入用SQL怎么实现
要求:不能用存诸过程,参数,事务...只能用SQL语句
能实现吗?

解决方案 »

  1.   


    if not exists(select 1 from tableA where [name] = '3' )
    insert into tableA (id,name,age) select '3','3','1'
      

  2.   


    insert into tb
    select top 1 3,'1',3 from tb t where not exists(
    select * from tb where name='1')
      

  3.   

    insert tb select top 1 3,1,3 from tb where name<>1
      

  4.   

    insert into tb
    select top 1 3,'1',3 from tb t where not exists(
    select * from tb where name='1')
      

  5.   


    insert tb select top 1 3,1,3 from tb where not exists(select 1 from tb where name=1)
      

  6.   

    declare @t table(id int, name int, age int) 
    insert @t select 1,  1,    1 
    insert @t select 2,  2,    2 
    declare @i int ,  @name int,@age int
    set @i=3 set @name=1 set @age=3
    insert @t select @i,@name,@age where @name not in(select name from @t) 
    select * from @t
    /*id          name        age         
    ----------- ----------- ----------- 
    1           1           1
    2           2           2(所影响的行数为 2 行)*/
      

  7.   

     
    table:是你的表
    TableA: 数据来源表。insert into table(id,name,age)
    select id,name,age
    from TableA
    where name
      not in(select name from table)