日期:2014-05-20  浏览次数:20443 次

请大家帮忙看看.明天中午前揭贴
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表中去.




------解决方案--------------------
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)即可自动判断并插入。当然如果需要一次性全部更新,可以使用游标来完成。