2023年8月3日发(作者:)
db2sql之updateinsertdelete
1. insert的3中写法
(1)
insert into sysinfo values(1);
insert into sysinfo values(2);
insert into sysinfo values(3);
(2)
insert into sysinfo values(1),(2),(3);
(3)
insert into sysinfo select a from sysinfo_1;
推荐(2),(3)两种写法
2. update的2种写法
(1)
update h_mntdcha_test set tdcyno= row_number()
over(partition by tdacno order by tdeddt asc)
where tdacno='7331212';
(2)
update (select ,
row_number() over(partition by order by
asc) nm from h_mntdcha_test a where
='7331212')
set tdcyno=nm +1;
IBM官方给出的说法是:
(1)中的写法是用来准确的更新一条记录,也就是说如果where条件可以定位一条记录进行更新,那么建议使用它
(2)中的写法是用来更新一条或者多条记录,一般用于集合操作。 由于(1)中是搜索到一条记录,然后更新,再搜索,因此更加适合单挑记录的更新
而(2)中是先根据条件把所有的记录都选择出来然后再统一更新,这样可以不用总是不断地更新-->查询--->更新。可以提高效率
个人理解(1)中的类似于游标的操作,而(2)中则是集合操作。
综上所述,对于批量更新操作,建议使用(2)
的2种写法
delete from
(select row_number() over(partition by a order by a ) nm from
sysinfo)
where nm=1; ---删除了3条重复的记录。
delete from sysinfo where a=3; --删除1条记录
和udpate同理,建议在平时使用 delete from (select ....),可以提高效率。
2023年8月3日发(作者:)
db2sql之updateinsertdelete
1. insert的3中写法
(1)
insert into sysinfo values(1);
insert into sysinfo values(2);
insert into sysinfo values(3);
(2)
insert into sysinfo values(1),(2),(3);
(3)
insert into sysinfo select a from sysinfo_1;
推荐(2),(3)两种写法
2. update的2种写法
(1)
update h_mntdcha_test set tdcyno= row_number()
over(partition by tdacno order by tdeddt asc)
where tdacno='7331212';
(2)
update (select ,
row_number() over(partition by order by
asc) nm from h_mntdcha_test a where
='7331212')
set tdcyno=nm +1;
IBM官方给出的说法是:
(1)中的写法是用来准确的更新一条记录,也就是说如果where条件可以定位一条记录进行更新,那么建议使用它
(2)中的写法是用来更新一条或者多条记录,一般用于集合操作。 由于(1)中是搜索到一条记录,然后更新,再搜索,因此更加适合单挑记录的更新
而(2)中是先根据条件把所有的记录都选择出来然后再统一更新,这样可以不用总是不断地更新-->查询--->更新。可以提高效率
个人理解(1)中的类似于游标的操作,而(2)中则是集合操作。
综上所述,对于批量更新操作,建议使用(2)
的2种写法
delete from
(select row_number() over(partition by a order by a ) nm from
sysinfo)
where nm=1; ---删除了3条重复的记录。
delete from sysinfo where a=3; --删除1条记录
和udpate同理,建议在平时使用 delete from (select ....),可以提高效率。
发布评论