Oracle中的dbms_metadata如何从表模式文件中生成约束?

时间:2010-09-24 06:31:07

标签: database oracle ddl database-schema dbms-metadata

为什么dbms_metadata.set_transform_param(dbms_metadata.session_transform,'CONSTRAINTS_AS_ALTER',TRUE)不在单独的文件中生成约束?此时此pl / sql在表模式定义文件之后粘贴约束。我必须使用什么标志来实现这个单独的约束模式定义目录?

2 个答案:

答案 0 :(得分:1)

DBMS_METADATA.GET_DDL返回单个CLOB,而不是文件,绝对不是多个文件。

为每个约束获取单独的CLOB的唯一方法是为每个约束单独执行GET_DDL:

select dbms_metadata.get_ddl('CONSTRAINT',constraint_name) 
from user_constraints;

答案 1 :(得分:0)

extractObj() 
{ 
  APT_CMD=${APT_CMD}"exec dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'SEGMENT_ATTRIBUTES', FALSE);     APT_CMD=${APT_CMD}"exec dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'SEGMENT_ATTRIBUTES', FALSE); 
"   " 
  # Add FORCE to view statements     # Add FORCE to view statements 
  APT_CMD=${APT_CMD}"exec dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'FORCE', TRUE);     APT_CMD=${APT_CMD}"exec dbms_metadata.set_transform_param(dbms_metadata.session_transform, 'FORCE', TRUE); 
"   " 
  # Make ref integrity constraints alter statements +-   
   APT_CMD=${APT_CMD}"exec dbms_metadata.set_transform_param( DBMS_METADATA.SESSION_TRANSFORM, 'CONSTRAINTS_AS_ALTER', TRUE );      
"     

  # Remap the schema name to blank =   # Remap the schema name to blank 
  # This is done by processing the tmp file using sed     # This is done by processing the tmp file using sed 

  # Add a terminator to the end of the extracted SQL     # Add a terminator to the end of the extracted SQL 
  APT_CMD=${APT_CMD}"exec DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',TRUE); <> #  APT_CMD=${APT_CMD}"exec DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'SQLTERMINATOR',TRUE); 
"   #" 
  # Make the output look 'PRETTY'!! =   # Make the output look 'PRETTY'!! 
  APT_CMD=${APT_CMD}"execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',TRUE); <> #  APT_CMD=${APT_CMD}"execute DBMS_METADATA.SET_TRANSFORM_PARAM(DBMS_METADATA.SESSION_TRANSFORM,'PRETTY',TRUE); 
"   #" 
  if [[ ${OBJECT_TYPE} = 'DB_LINK' ]]     
  then     
    APT_CMD=${APT_CMD}"select substr(to_char(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'))     
             ,1     
             ,INSTR(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'),'\"',1,3)     
             )||     
       '<USER>'||     
       substr(to_char(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'))     
             ,INSTR(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'),'\"',1,4)     
             , INSTR(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'),'VALUES',1,1)-INSTR(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'),'\"',1,4)     
             )||     
       '''<PW>'''||     
       substr(to_char(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'))     
             ,INSTR(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'),'''',1,2)+1     
             ,INSTR(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'),'''',1,3)-INSTR(DBMS_METADATA.GET_DDL('DB_LINK','PSINTERFACE'),'''',1,2)     
             )||     
       '<DB>'';'     
    from   dual     
"     
  else     
    APT_CMD=${APT_CMD}" select DBMS_METADATA.GET_DDL('${OBJECT_TYPE}','${OBJECT_NAME}') from dual     APT_CMD=${APT_CMD}" select DBMS_METADATA.GET_DDL('${OBJECT_TYPE}','${OBJECT_NAME}')||'/' from dual 
" = " 
  fi +-   

  APT_CMD=${APT_CMD}" ; " =   APT_CMD=${APT_CMD}" ; " 

  if [[ -n ${APT_DEBUG}  ]] +-   
  then     
    logMsg ${LM_INFO} "${APT_CMD}"     
  fi     

  SPOOLFILE=${OBJDIR}/$(print ${OBJECT_NAME} | tr "[A-Z]" "[a-z]") =   SPOOLFILE=${OBJDIR}/$(print ${OBJECT_NAME} | tr "[A-Z]" "[a-z]") 
  CMDLINE=${OBJECT_DIR}/$(print ${OBJECT_NAME} | tr "[A-Z]" "[a-z]").sql     CMDLINE=${OBJECT_DIR}/$(print ${OBJECT_NAME} | tr "[A-Z]" "[a-z]").sql 
  print "  ...extracting ${OBJECT_TYPE} ${OBJECT_NAME}" <>   
  oraSql ${L_APT_USER} ${L_APT_PASSWD} ${L_APT_SID} "${APT_CMD}" ${SPOOLFILE}.tmp >/dev/null     oraSql ${L_APT_USER} ${L_APT_PASSWD} ${L_APT_SID} "${APT_CMD}" ${SPOOLFILE}.tmp 
  =   
  # Remove any references to the local schema - note other schema references used in triggers will remain!!!     # Remove any references to the local schema - note other schema references used in triggers will remain!!! 
  cat ${SPOOLFILE}.tmp | sed "s/\"${UPPER_APT_USER}\"\.//g" >${SPOOLFILE}.sql <>   cat ${SPOOLFILE}.tmp | sed "s/\"${L_APT_USER}\"\.//g" >${SPOOLFILE}.sql 
  rm ${SPOOLFILE}.tmp =   rm ${SPOOLFILE}.tmp 
  print "@${CMDLINE}" >>${BUILD_FILE}     print "@${CMDLINE}" >>${BUILD_FILE} 
  +-   
  # If the object type is table further parse it to strip out the constraints to constraint files     
  if [[ ${OBJECT_DIR} = tables ]]     
  then     
    # This splits the second part of the file (after the first semi-colon) to a temporary constraint file for further processing     
    mkDir ${BASE_DIR}/${L_APT_USER}/constraints     
    CONTMP=${BASE_DIR}/${L_APT_USER}/constraints/${OBJECT_NAME}.tmp     
    cat ${SPOOLFILE}.sql | awk "BEGIN { x=0 }     
                                  // { if (x == 1) { print; } }     
                                  /\;/  { x=1; }     
                                END   { }" | grep -v ^$ >${CONTMP}     

    # Split each alter statement into a seperate constraint file in the constraints directory     
    while read line     
    do     
      if  [[ $(print $line | grep -c 'ALTER TABLE') > 0 ]]     
      then     
        CONSTRAINT=$(print $line | tr "[A-Z]" "[a-z]" | cut -d\" -f4)     
        CONFILE=${BASE_DIR}/${L_APT_USER}/constraints/${CONSTRAINT}.sql     
        UPPERCONSTRAINT=$(print ${CONSTRAINT} | tr "[a-z]" "[A-Z]")     
        print "      .extracting CONSTRAINT ${UPPERCONSTRAINT}"     
      fi     
      print $line>>${CONFILE}     
    done<${CONTMP}     
    rm -f ${CONTMP}     
  fi     
}