日期:2014-05-17  浏览次数:21252 次

PL/SQL创建表问题
我需要批量生成一批表,需求是:创建表时表名是动态生成的;代码如下:

DECLARE

  --定义VARCHAR2的数组以及大小
  TYPE ARRSYVAR IS VARRAY(36) OF VARCHAR2(30); 
  ARRAYPARA ARRSYVAR; --数组变量
 BEGIN
  -- Test statements here
  ARRAYPARA := ARRSYVAR('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9'); --数组初始化
  FOR K IN 1 .. 36 LOOP
  FOR J IN 1 .. 36 LOOP
  -- calculate pi with 100 terms
  TABLENAME := 'IC_'+ARRAYPARA(K)+ARRAYPARA(J) -- 表名
  create table TABLENAME
(
  STOCKID NUMBER not null,
  USERID NUMBER,
  MODEL VARCHAR2(100),
  MAKER VARCHAR2(100),
  AMOUNT NUMBER default 0,
  LOTNUMBER VARCHAR2(100),
  UPTRANSDATE DATE default SYSDATE,
  PRICE NUMBER(8,2) default 0,
  STATE NUMBER default 0,
  ENCAPSULATION VARCHAR2(50),
  DESCRIBE VARCHAR2(1000),
  VOUCH NUMBER default 1,
  SPOTGOODS NUMBER default 0
)
tablespace SYSTEM
  pctfree 10
  pctused 40
  initrans 1
  maxtrans 255
  storage
  (
  initial 16
  minextents 1
  maxextents unlimited
  );
-- Create/Recreate primary, unique and foreign key constraints 
alter table TABLENAME
  add constraint PK_SYSICSTOCK primary key (STOCKID)
  using index 
  tablespace SYSTEM
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
  initial 16K
  minextents 1
  maxextents unlimited
  );
alter table TABLENAME
  add constraint FK_SYSICSTO_IC foreign key (USERID)
  references SYSMEMBER (USERID);
-- Create/Recreate indexes 
create index INDEX_MODEL on TABLENAME (MODEL)
  tablespace SYSTEM
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
  initial 64K
  minextents 1
  maxextents unlimited
  );
  END LOOP;
  END LOOP;
 END;
 不知道这样子行不行?




------解决方案--------------------
使用动态SQL语句
SQL code

declare
    v_SQL varchar2(1024);
begin
    v_SQL := 'create table .....';
    execute immediate v_SQL;
end;