扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
create table EmpTest ( empNo integer not null, lastName varchar2(30) not null, firstName varchar2(20) not null, job varchar2(9) ’ hireDate date ’ isActive number(1) constraint EmpTest_CK1 check (isActive in (0,1)) , salary number(9,2) , commision number(9,2) , deptNo number(2) , constraint EmpTest_PK primary key (empNo), constraint EmpTest_AK1 unique (lastName, firstName) ); create index EmpTest_HireDate_Salary on EmpTest ( salary, hireDate ); |
select index_name, index_type, uniqueness from user_indexes where table_name = ’EMPTEST’; |
索引名称 | 索引类型 | 唯一性 |
EMPTEST_AK1 | NORMAL | UNIQUE |
EMPTEST_HIREDATE_SALARY | NORMAL | NONUNIQUE |
EMPTEST_PK | NORMAL | UNIQUE |
约束名称 | 约束类型 | 索引名称 |
SYS_C002144065 | C | |
SYS_C002144066 | C | |
SYS_C002144067 | C | |
EMPTEST_CK1 | C | |
EMPTEST_PK | P | EMPTEST_PK |
EMPTEST_AK1 | U | EMPTEST_AK1 |
declare vClob clob; vLongString varchar2(32767); vOffSet pls_integer := 0; vLength pls_integer := 0; vTable varchar2(30) := ’EmpTest’; procedure Show (pVariable varchar2, pLineSize pls_integer := 80) is begin dbms_output.enable(1000000); if (length(pVariable) > pLineSize) then dbms_output.put_line(substr(pVariable, 1, pLineSize)); Show(substr(pVariable, pLineSize + 1), pLineSize); else dbms_output.put_line(pVariable); end if; end Show; begin -- 获取 DDL vClob := dbms_metadata.get_ddl(’TABLE’, upper(vTable)); -- 获取 CLOB 长度 vLength := dbms_lob.GetLength(vClob); dbms_output.put_line(’DDL length: ’ || to_char(vLength)); vOffSet := 1; dbms_lob.read(vClob, vLength, vOffSet, vLongString); -- 关闭 CLOB if (dbms_lob.isOpen(vClob) > 0) then dbms_lob.close(vClob); end if; Show(vLongString, 80); end; |
DDL length: 461 CREATE TABLE "BORIS"."EMPTEST" ( "EMPNO" NUMBER(*,0) NOT NULL ENABLE, "LASTNAME" VARCHAR2(30) NOT NULL ENABLE, "FIRSTNAME" VARCHAR2(20) NOT NULL ENABLE, "JOB" VARCHAR2(9), "HIREDATE" DATE, "ISACTIVE" NUMBER(1,0), "SALARY" NUMBER(9,2), "COMMISION" NUMBER(9,2), "DEPTNO" NUMBER(2,0), CONSTRAINT "EMPTEST_CK1" CHECK (isActive in (0,1)) ENABLE, CONSTRAINT "EMPTEST_PK" PRIMARY KEY ("EMPNO") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "TOOLS" ENABLE, CONSTRAINT "EMPTEST_AK1" UNIQUE ("LASTNAME", "FIRSTNAME") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "TOOLS" ENABLE) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "TOOLS" |
subtype tString is varchar2(30); subtype tDBString is varchar2(255); subtype tDBLongString is varchar2(4000); subtype tLongString is varchar2(32767); type tArrayLongString is table of tLongString index by pls_integer; type tMetaObject is record ( aName tString, aType tString, aLogging tString, aParallel tString, aStatus tString, aValidated tString, aRely tString, aDDLString tLongString ); |
type tArrayMetaObject is table of tMetaObject index by pls_integer; |
type tFullMetaObject is record ( aTable tMetaObject, aIndexes tArrayMetaObject, aConstraints tArrayMetaObject, aTriggers tArrayMetaObject ); |
cEnabled constant char(7) := ’ENABLED’; cDisabled constant char(8) := ’DISABLED’; cUsable constant char(6) := ’USABLE’; cUnusable constant char(8) := ’UNUSABLE’; cValid constant char(5) := ’VALID’; cInvalid constant char(7) := ’INVALID’; cTable constant char(5) := ’TABLE’; cView constant char(4) := ’VIEW’; cIndex constant char(5) := ’INDEX’; cConstraint constant char(10) := ’CONSTRAINT’; cTrigger constant char(7) := ’TRIGGER’; cLobType constant char(3) := ’LOB’; cClobType constant char(4) := ’CLOB’; cBlobType constant char(4) := ’BLOB’; cPackage constant char(7) := ’PACKAGE’; cPackageBody constant char(12) := ’PACKAGE BODY’; cProcedure constant char(9) := ’PROCEDURE’; cFunction constant char(8) := ’FUNCTION’; cSequence constant char(8) := ’SEQUENCE’; cSynonym constant char(7) := ’SYNONYM’; cType constant char(4) := ’TYPE’; cColumn constant char(6) := ’COLUMN’; cJavaSource constant char(11) := ’JAVA SOURCE’; cJavaClass constant char(10) := ’JAVA CLASS’; cYes constant char(3) := ’YES’; cNo constant char(2) := ’NO’; cPKConsType constant char(1) := ’P’; cUNConsType constant char(1) := ’U’; cFKConsType constant char(1) := ’R’; cCKConsType constant char(1) := ’C’; cDropStorage constant char(12) := ’DROP STORAGE’; cReuseStorage constant char(13) := ’REUSE STORAGE’; cCascade constant char(19) := ’CASCADE CONSTRAINTS’; cNoCascade constant char(10) := ’NO CASCADE’; cEnable constant char(6) := ’ENABLE’; cNovalidate constant char(10) := ’NOVALIDATE’; cRely constant char(4) := ’RELY’; cNoRely constant char(6) := ’NORELY’; cValidated constant char(9) := ’VALIDATED’; cNotValidated constant char(13) := ’NOT VALIDATED’; cLogging constant char(7) := ’LOGGING’; cNoLogging constant char(9) := ’NOLOGGING’; cParallel constant char(8) := ’PARALLEL’; cNoParallel constant char(10) := ’NOPARALLEL’; cNull constant char(4) := ’NULL’; cNotNull constant char(8) := ’NOT NULL’; cDefault constant char(7) := ’DEFAULT’; cSYSPrefix constant char(4) := ’SYS_’; cDoubleQuote constant char(1) := ’"’; subtype tString is varchar2(30); subtype tDBString is varchar2(255); subtype tDBLongString is varchar2(4000); subtype tLongString is varchar2(32767); type tArrayLongString is table of tLongString index by pls_integer; type tMetaObject is record ( aName tString, aType tString, aLogging tString, aParallel tString, aStatus tString, aValidated tString, aRely tString, aDDLString tLongString ); type tArrayMetaObject is table of tMetaObject index by pls_integer; type tFullMetaObject is record ( aTable tMetaObject, aIndexes tArrayMetaObject, aConstraints tArrayMetaObject, aTriggers tArrayMetaObject ); type tArrayFullMetaObjectByString is table of tFullMetaObject index by varchar2(30); procedure Load ( pTable in tString, pForce in boolean := false ); procedure Reset ( pTable in tString ); procedure Reset; function GetMeta ( pTable in tString, pForce in boolean := false ) return tFullMetaObject; function GetMeta return tArrayFullMetaObjectByString; procedure SetMeta ( pTable in tString, pFullMetaObject in tFullMetaObject ); procedure SetMeta ( pArrayFullMetaObjectByString in tArrayFullMetaObjectByString ); procedure Show ( pTable in tString ); procedure Show; end MetaDataPkg; |
vMetaData tArrayFullMetaObjectByString; procedure SetEnvironment is begin dbms_metadata.set_transform_param( dbms_metadata.session_transform, ’PRETTY’, false); dbms_metadata.SET_TRANSFORM_PARAM( dbms_metadata.session_transform, ’SEGMENT_ATTRIBUTES’, true); dbms_metadata.set_transform_param( dbms_metadata.session_transform, ’STORAGE’, true); dbms_metadata.set_transform_param( dbms_metadata.session_transform, ’TABLESPACE’, true); dbms_metadata.set_transform_param( dbms_metadata.session_transform, ’CONSTRAINTS’, false); dbms_metadata.set_transform_param( dbms_metadata.session_transform, ’REF_CONSTRAINTS’, false); dbms_metadata.set_transform_param( dbms_metadata.session_transform, ’CONSTRAINTS_AS_ALTER’, false); end SetEnvironment; procedure Print ( pString varchar2, pLineSize positive := 80 ) is vLineSize pls_integer := least(nvl(pLineSize, 80), 255); begin dbms_output.enable(1000000); if (length(pString) > vLineSize) then dbms_output.put_line(substr(pString, 1, vLineSize)); Print(substr(pString, pLineSize + 1), vLineSize); else dbms_output.put_line(pString); end if; end Print; procedure Show ( pMetaObject in tMetaObject ) is begin dbms_output.put_line(’***’); dbms_output.put_line(’Name: ’ || pMetaObject.aName); dbms_output.put_line(’Type: ’ || pMetaObject.aType); dbms_output.put_line(’Logging: ’ || pMetaObject.aLogging); dbms_output.put_line(’Parallel: ’ || to_char(pMetaObject.aParallel)); dbms_output.put_line(’Status: ’ || pMetaObject.aStatus); dbms_output.put_line(’Validated: ’ || pMetaObject.aValidated); dbms_output.put_line(’Rely: ’ || pMetaObject.aRely); print(’DDL String: ’ || pMetaObject.aDDLString, 255); dbms_output.put_line(’***’); end Show; function GetDDL ( pName in tString, pType in tString ) return tLongString is vClob clob; vLongStrings tArrayLongString; vFullLength pls_integer := 0; vOffSet pls_integer := 0; vLength pls_integer := 0; begin vClob := dbms_metadata.get_ddl(pType, upper(pName)); vFullLength := dbms_lob.GetLength(vClob); for nIndex in 1..ceil(vFullLength / 32767) loop vOffSet := vLength + 1; vLength := least(vFullLength - (nIndex - 1) * 32767, 32767); dbms_lob.read(vClob, vLength, vOffSet, vLongStrings(nIndex)); vLongStrings(nIndex) := replace(vLongStrings(nIndex), cDoubleQuote || user || cDoubleQuote || ’.’, ’’); vLongStrings(nIndex) :=ltrim(rtrim(replace(vLongStrings(nIndex), chr(10), ’’))); end loop; if (dbms_lob.isOpen(vClob) > 0) then dbms_lob.close(vClob); end if; return vLongStrings(1); end GetDDL; function ObjectExists ( pObjectName in tString, pObjectType in tString, pTableName in tString := null ) return boolean is vCount pls_integer := 0; vObjectName tString := ltrim(rtrim(pObjectName)); vObjectType tString := upper(ltrim(rtrim(pObjectType))); vTableName tString := upper(ltrim(rtrim(pTableName))); begin case when vObjectType = cColumn then select count(*) into vCount from Dual where exists (select ’1’ from user_tab_columns where column_name = upper(vObjectName) and table_name = vTableName); when vObjectType = cConstraint then select count(*) into vCount from Dual where exists (select ’1’ from user_constraints where constraint_name = upper(vObjectName) and table_name = vTableName); when vObjectType in (cJavaSource, cJavaClass) then select count(*) into vCount from Dual where exists (select ’1’ from user_objects where object_name = vObjectName and object_type = vObjectType); else select count(*) into vCount from Dual where exists (select ’1’ from user_objects where object_name = upper(pObjectName) and object_type = vObjectType); end case; return (vCount > 0); end ObjectExists; procedure Load ( pTable in tString, pForce in boolean := false ) is vFullMetaObject tFullMetaObject; vTable tString := upper(ltrim(rtrim(pTable))); vCount pls_integer := 0; begin if (not vMetaData.exists(vTable) or nvl(pForce, false)) then if not ObjectExists(pTable, cTable) then raise_application_error(-20500, ’Unable to load metadata for ’ || nvl(pTable, ’NULL’) || ’. ’ || ’Table does not exist.’ ); end if; for rec in (select table_name, logging, ltrim(rtrim(degree)) as degree, partitioned, backed_up from user_tables where table_name = vTable) loop vFullMetaObject.aTable.aName := rec.table_name; vFullMetaObject.aTable.aType := rec.partitioned; vFullMetaObject.aTable.aLogging := rec.logging; vFullMetaObject.aTable.aParallel := ltrim(rtrim(rec.degree)); vFullMetaObject.aTable.aStatus := rec.backed_up; vFullMetaObject.aTable.aDDLString := GetDDL(rec.table_name, cTable); end loop; for rec in (select index_name, uniqueness, logging, ltrim(rtrim(degree)) as degree, status from user_indexes where table_name = vTable and index_type != cLobType) loop vCount := vCount + 1; vFullMetaObject.aIndexes(vCount).aName := rec.index_name; vFullMetaObject.aIndexes(vCount).aType := rec.uniqueness; vFullMetaObject.aIndexes(vCount).aLogging := rec.logging; vFullMetaObject.aIndexes(vCount).aParallel := ltrim(rtrim(rec.degree)); vFullMetaObject.aIndexes(vCount).aStatus := rec.status; vFullMetaObject.aIndexes(vCount).aDDLString := GetDDL(rec.index_name, cIndex); end loop; vCount := 0; for rec in (select constraint_name,constraint_type,status,search_condition,validated,rely from user_constraints where table_name = vTable order by decode(constraint_type, cPKConsType, 10, cUNConsType, 20, cFKConsType, 30, cCKConsType, 40, 100), constraint_name) loop vCount := vCount + 1; vFullMetaObject.aConstraints(vCount).aName := rec.constraint_name; vFullMetaObject.aConstraints(vCount).aType := rec.constraint_type; vFullMetaObject.aConstraints(vCount).aLogging := null; vFullMetaObject.aConstraints(vCount).aParallel := null; vFullMetaObject.aConstraints(vCount).aStatus := rec.status; vFullMetaObject.aConstraints(vCount).aValidated := rec.validated; vFullMetaObject.aConstraints(vCount).aRely := rec.rely; if substr(rec.constraint_name, 1, length(cSYSPrefix)) = cSYSPrefix and upper(rec.search_condition) like ’%IS ’ || cNotNull || ’%’ then vFullMetaObject.aConstraints(vCount).aDDLString := ’ALTER TABLE ’ || cDoubleQuote || vFullMetaObject.aTable.aName || cDoubleQuote || ’ ’ || ’MODIFY ’ || replace(rec.search_condition, ’IS ’ || cNotNull, cNotNull) || (case when vFullMetaObject.aConstraints(vCount).aValidated = cNotValidated then ’ ’ || cNovalidate else ’’end); else vFullMetaObject.aConstraints(vCount).aDDLString := GetDDL(rec.constraint_name, cConstraint); end if; end loop; SetMeta(pTable, vFullMetaObject); end if; end Load; procedure Reset ( pTable in tString ) is begin vMetaData.delete(pTable); end Reset; procedure Reset is begin vMetaData.delete; end Reset; function GetMeta ( pTable in tString, pForce in boolean := false ) return tFullMetaObject is begin if (not vMetaData.exists(pTable) or nvl(pForce, false)) then Load(pTable, pForce); if not vMetaData.exists(pTable) then raise_application_error(-20501, ’Unable to find metadata for ’ || pTable || ’ in repository.’); end if; end if; return vMetaData(pTable); end GetMeta; function GetMeta return tArrayFullMetaObjectByString is begin return vMetaData; end GetMeta; procedure SetMeta ( pTable in tString, pFullMetaObject in tFullMetaObject ) is begin vMetaData(pTable) := pFullMetaObject; end SetMeta; procedure SetMeta ( pArrayFullMetaObjectByString in tArrayFullMetaObjectByString ) is begin vMetaData := pArrayFullMetaObjectByString; end SetMeta; procedure Show ( pTable in tString ) is vFullMetaObject tFullMetaObject; begin if (vMetaData.exists(pTable)) then dbms_output.enable(1000000); vFullMetaObject := vMetaData(pTable); dbms_output.put_line(’Start Full Object: ’ || pTable); dbms_output.put_line(’Start Table: ’ || pTable); Show(vFullMetaObject.aTable); dbms_output.put_line(’Finish Table: ’ || pTable); dbms_output.put_line(’Start Indexes: ’ || pTable); if (vFullMetaObject.aIndexes.count > 0) then for nIndex in vFullMetaObject.aIndexes.first..vFullMetaObject.aIndexes.last loop Show(vFullMetaObject.aIndexes(nIndex)); end loop; end if; dbms_output.put_line(’Finish Indexes: ’ || pTable); dbms_output.put_line(’Start Constraints: ’ || pTable); if (vFullMetaObject.aConstraints.count > 0) then for nIndex in vFullMetaObject.aConstraints.first..vFullMetaObject.aConstraints.last loop Show(vFullMetaObject.aConstraints(nIndex)); end loop; end if; dbms_output.put_line(’Finish Constraints: ’ || pTable); dbms_output.put_line(’Start Triggers: ’ || pTable); if (vFullMetaObject.aTriggers.count > 0) then for nIndex in vFullMetaObject.aTriggers.first..vFullMetaObject.aTriggers.last loop Show(vFullMetaObject.aTriggers(nIndex)); end loop; end if; dbms_output.put_line(’Finish Triggers: ’ || pTable); dbms_output.put_line(’Finish Full Object: ’ || pTable); end if; end Show; procedure Show is vTable tString; begin if vMetaData.count > 0 then dbms_output.put_line(’Total Meta Objects: ’ || to_char(vMetaData.count)); vTable := vMetaData.first; while (vTable is not null) loop Show(vTable); vTable := vMetaData.next(vTable); end loop; end if; end Show; begin SetEnvironment; end MetaDataPkg; |
dbms_metadata.set_transform_param(dbms_metadata.session_transform, ’PRETTY’, false); |
dbms_metadata.set_transform_param(dbms_metadata.session_transform, ’SEGMENT_ATTRIBUTES’, true); dbms_metadata.set_transform_param(dbms_metadata.session_transform, ’STORAGE’, true); dbms_metadata.set_transform_param(dbms_metadata.session_transform, ’TABLESPACE’, true); |
dbms_metadata.set_transform_param(dbms_metadata.session_transform, ’CONSTRAINTS’, false); dbms_metadata.set_transform_param(dbms_metadata.session_transform, ’REF_CONSTRAINTS’, false); dbms_metadata.set_transform_param(dbms_metadata.session_transform, ’CONSTRAINTS_AS_ALTER’, false); |
declare vTable MetaDataPkg.tString := ’EmpTest’; vRunStartTime number; begin vRunStartTime := dbms_utility.get_time; MetaDataPkg.Load(vTable, true); MetaDataPkg.Show(); dbms_output.put_line(’Time Elapsed: ’ || to_char((dbms_utility.get_time - vRunStartTime) / 100) || ’ sec.’); end; |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。