[英]Move rows to another table
我想將行從一個表移動到另一個表(以便將未使用的數據移動到歷史存儲中)。
如何以最聰明的方式做到這一點?
我找到了這樣的解決方案,但看起來它不適用於Oracle方言
INSERT dbo.CustomersInactive (
CustomerID,
FirstName,
LastName
) SELECT
CustomerID,
FirstName,
Lastname
FROM (
DELETE dbo.CustomersActive
OUTPUT
DELETED.CustomerID,
此解決方案似乎有效:
DECLARE
TYPE CustomerSet IS TABLE OF CustomersActive%ROWTYPE;
inactive CustomerSet;
BEGIN
delete from CustomersActive returning CustomerID,FirstName,Lastname bulk collect into inactive;
FOR i IN inactive.FIRST .. inactive.LAST LOOP
insert into CustomersInactive values (inactive(i).CustomerID,inactive(i).FirstName,inactive(i).Lastname);
END LOOP;
END;
我希望您是這種情況:
--init objects
create table active_cust
(cust_id integer,
name varchar2(100 char)
);
create table inactive_cust as
select *
from active_cust
where 1=2;
--init data
insert into active_cust values (1, 'Przemo');
insert into active_cust values (2,'Pan Miecio');
insert into active_cust values (3,'Pan Franio');
insert into inactive_cust values (3,'Pan Franio');
--merge active and inactive
merge into inactive_cust dest
using (select * from active_cust) srce
on (srce.cust_id = dest.cust_id)
when not matched then insert values
(srce.cust_id, srce.name )
--here specify conditions on which customer is being
--accounted as inactive
/*where srce.some_status_date < sysdate - 100 */
;--only two rows merged as we have >Pan Franio< already in a list of inactive customers!
--now as we have all inactive customers in inactive_cust table, delete from active_cust where id is present in inactive_cust
delete from active_cust ac
where ac.cust_id in (select cust_id
from inactive_cust);
drop table active_cust;
drop table inactive_cust;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.