關於多使用者建立相同表實現shell

shawnloong發表於2015-07-17
關於多使用者批次建立同樣表shell解決方法
開發給的指令碼事例
[oracle@OLEASM1 scripts]$ cat createaalltable.sql
--create table all
create table showlist
(id int primary key,name varchar2(50),url varchar2(50));
create table userlist
(id int primary key,uid varchar2(20),user_name varchar(50));
create table sales
(id int primary key,unit_price float not null,amount float not null,amounts float);

user.list 檔案我取dba_user幾個使用者
SQL> select * from (select username from dba_users order by created desc) m where rownum<6;

USERNAME
------------------------------
ZNJKTEST
ZNJKTEST01
ZNJK
TEST1
TEST

[oracle@OLEASM1 scripts]$ cat user.list
ZNJKTEST
ZNJKTEST01
ZNJK
TEST1
TEST


[oracle@OLEASM1 scripts]$ for user in `cat user.list`; do cat createaalltable.sql|sed 's/table /&'$user'./'; done;
--create table ZNJKTEST.all
create table ZNJKTEST.showlist
(id int primary key,name varchar2(50),url varchar2(50));
create table ZNJKTEST.userlist
(id int primary key,uid varchar2(20),user_name varchar(50));
create table ZNJKTEST.sales
(id int primary key,unit_price float not null,amount float not null,amounts float);
--create table ZNJKTEST01.all
create table ZNJKTEST01.showlist
(id int primary key,name varchar2(50),url varchar2(50));
create table ZNJKTEST01.userlist
(id int primary key,uid varchar2(20),user_name varchar(50));
create table ZNJKTEST01.sales
(id int primary key,unit_price float not null,amount float not null,amounts float);
--create table ZNJK.all
create table ZNJK.showlist
(id int primary key,name varchar2(50),url varchar2(50));
create table ZNJK.userlist
(id int primary key,uid varchar2(20),user_name varchar(50));
create table ZNJK.sales
(id int primary key,unit_price float not null,amount float not null,amounts float);
--create table TEST1.all
create table TEST1.showlist
(id int primary key,name varchar2(50),url varchar2(50));
create table TEST1.userlist
(id int primary key,uids varchar2(20),user_name varchar(50));
create table TEST1.sales
(id int primary key,unit_price float not null,amount float not null,amounts float);
--create table TEST.all
create table TEST.showlist
(id int primary key,name varchar2(50),url varchar2(50));
create table TEST.userlist
(id int primary key,uids varchar2(20),user_name varchar(50));
create table TEST.sales
(id int primary key,unit_price float not null,amount float not null,amounts float);

這樣就生成了我們要的語句了可以將這些語句儲存到一個sql檔案
for user in `cat user.list`; do cat createaalltable.sql|sed 's/table /&'$user'./'>>createtable.sql; done;

其實還可以用awk實現

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24486203/viewspace-1736783/,如需轉載,請註明出處,否則將追究法律責任。

相關文章