首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > IBM DB2 >

存储过程里更新符合条件的表有关问题

2012-03-27 
存储过程里更新符合条件的表问题表table1(id1,date),table2(id1,id2,fd2)当给的条件日期tmpdate在table1能

存储过程里更新符合条件的表问题
表table1(id1,date),table2(id1,id2,fd2)
当给的条件日期tmpdate在table1能找到时(即select count() from table1 where date=tmpdate不为空记录),更新和table1里对应的id1字段对应的table2里的fd2字段值为‘aaa’,如果没有找到时,更新为‘bbb’。
怎样写语句,求解!!!

[解决办法]

SQL code
declare row_count int default 0;select count(*) into row_count from(  select * from table1 where date = tmpdate fetch first 1 row only);if row_count > 0 then    update table2 set fd2 = 'aaa';else    update table2 set fd2 = 'bbb';end if;
[解决办法]
SQL code
update table2 set fd2 = 'aaa'where exists(  select id1 from table1  where table1.id1 = table2.id1    and table1.date = tmpdate);update table2 set fd2 = 'bbb'where not exists(  select id1 from table1  where table1.id1 = table2.id1    and table1.date = tmpdate); 

热点排行