我在C#里写了一条插入语句,想加上where Stationname=1391111111。如何加呢。必须要满足这个条件后才能插入。
sql += "insert into PlanMssion_ShangChuan(Station,JHour,JMinite,MissionParm1,MissionParm2,PressValue,YulvValue) values('" + ws.Dtuid + "','" + ws.Shi + "','" + ws.Fen + "','" + ws.BianPinSheZhi + "','" + ws.XiaoDuSheZhi + "','" + ws.YaLi + "','" + ws.YuLv + "');";

解决方案 »

  1.   

    先用ExecuteScalar来判断,如果>0就不加入
      

  2.   

    写个存储过程,先select where Stationname=1391111111 判断存在就插入
      

  3.   

    if exists(select 1 from table where Stationname=1391111111)
    begin
    insert into .........
    end
      

  4.   

    insert into ...select..from ....where  这种可以加条件
    insert into ....values()   这种不行 
      

  5.   

    没看出来你的 Where条件 和insert语句有什么关系 如果 Stationname 是 PlanMssion_ShangChuan 这张表的字段 
        那么 where Stationname=1391111111 完全用不着 
        因为要新增的数据还不在数据库中 就算你能where 也木有效果如果 Stationname 不是 PlanMssion_ShangChuan 这张表的字段 
        那 where Stationname=1391111111 跟Insert语句 就更加木有关系了
        直接在sql语句外面做判断就可以了注: insert语句是不能where的 where一般是查询用的
      

  6.   

    好像这样不行吧,加上WHERE好像是更新的吧
      

  7.   

    先 select  × where Stationname=1391111111 先查找出来,存在再执行 插入语句
      

  8.   

    declare @cc int
    select  @cc=count(*)  from 表 Stationname=1391111111
    if @cc>0
      begin
        insert xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
      end
    else
      begin
       --随便你
      end
      

  9.   


    insert into PlanMssion_ShangChuan(Station,JHour,JMinite,MissionParm1,MissionParm2,PressValue,YulvValue) select 字段名和insert字段一一对应 from 表 where Stationname=1391111111
      

  10.   

    先判断一下,不能直接拼SQL上