water 表custom_id  status  user_id    f_205   cur_flow
int          int    int      varchar     int 
note 表file_id    course_name   maker   status   flow_id   x  y
其中 custom_id对应 file_id  user_id对应 maker  f_205对应 course_name
cur_flow 对应 flow_id
先判断water表中的记录,在note中有没有相同的记录(根据water表中的custom_id与note表中的file_id俩个字段判断)。如果没有,就将water表中的新记录插入到note表中。
如果有的话就将water表中的该条记录的值更新到note表中去.

解决方案 »

  1.   

    declare @f_205 varchar(255)
    declare @user_id varchar(255)
    declare @status varchar(255)
    declare @cur_flow varchar(255)if not exists select 1 from note where file_id = @custom_id
    begin
    select @f_205=f_205,@user_id=user_id,@status=status,@cur_flow=cur_flow where file_id = @custom_id
    insert into note (file_id, course_name,maker, status,flow_id) values (@custom_id, @f_205,@user_id,@status,@cur_flow)
    end以上sql语句可以封装为一个存储过程,只需要传入要添加的的记录id(custom_id)即可自动判断并插入。当然如果需要一次性全部更新,可以使用游标来完成。
      

  2.   

    可以的,string sql = "上面我贴的代码";
    SqlCommand cmd = new Sqlcommand(sql, conn);
    cmd.Parameters.Add("@custom_id", id);//这里id是指定的一个记录id
    conn.open();
    cmd.exenonequery();
    conn.close();下面是循环的方法:
    SqlCommand cmd = new Sqlcommand(sql, conn);
    cmd.Parameters.Add("@custom_id", 0);
    conn.open();
    foreach(DataRow dr in 记录id的表的行集合)
    {
    cmd.Parameters[0]=dr["id"];
    cmd.exenonequery();
    }
    conn.close();