---
title: "Oracle根据已有数据生成insert语句"
author: "wangrl"
date: "2011-11-23T12:51:53Z"
tags: []
categories: ["数据库"]
slug: "oracle-insert"
draft: false
lastmod: "2011-11-23T12:51:53Z"
id: 1274889
url: "https://wangrl.iteye.com/blog/1274889"
---

select 'insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (' || user\_id || ', ' ||  role\_id || ');' from (

  select distinct user\_id, role\_id from usermgr.ts\_user\_org\_position uop, usermgr.ts\_position p, usermgr.ts\_role r

  where uop.position\_id=p.position\_id and p.position\_code=r.role\_code

);

 

执行以上语句，会生成对应的insert语句：

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (239, 1176);

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (419, 724);

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (14627, 722);

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (14410, 721);

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (877, 721);

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (511, 721);

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (431, 721);

insert into USERMGR.TS\_USER\_ROLE(USER\_ID, ROLE\_ID) values (649, 720);