ITPub博客

首页 > 数据库 > Oracle > 关于数据库对象版本比较的脚本

关于数据库对象版本比较的脚本

原创 Oracle 作者:PercyWang 时间:2007-11-15 23:50:32 0 删除 编辑

项目原因导致出现两个开发环境,主数据库环境因需求变更每天都需要进行脚本的修改,而报表服务器的脚本也需要同步更新,需求变更很少会同步提醒的;人工判断太过于麻烦,我又是一个懒人;只好写个脚本自动进行识别并加以运行了。

这只是一个简单的版本比较工具,事实上变更是很难判断的,例如表中新增了一个字段,导致顺序发生变更,这个时候你很难判断是新增还是修改的;诸如字段类型的修改.因此只能去判断表是否存在,字段是否存在,进而执行判断脚本,产生相应的表脚本和字段教本。

事实上Oracle有个DBMS_METADATA数据包提供了DLL脚本,不过包含了很多存储参数,对版本比较和对象生成没有什么意义。

[@more@]

--CREATE THE CREATE_TABLE SCRIPT, THOUGH WE CAN USE

--LIKE select dbms_metadata.get_ddl('TABLE','TABLENAME','USERNAME') from dual; GET THE SQL SCRIPT

--BUT IT IS NOT HELPFUL TO COMPARE THE DIFFERENT VERSION

SELECT SQLTEXT FROM
(
SELECT 'CREATE TABLE '||TABLE_NAME AS SQLTEXT,-1 AS COLUMN_ID,TABLE_NAME FROM USER_TABLES@REMOTEKGK
UNION
SELECT '(' AS SQLTEXT,0 AS COLUMN_ID,TABLE_NAME FROM USER_TABLES@REMOTEKGK
UNION
SELECT ');' AS SQLTEXT,100 AS COLUMN_ID,TABLE_NAME FROM USER_TABLES@REMOTEKGK
UNION
SELECT
CASE WHEN DATA_TYPE='NUMBER'
THEN
CASE WHEN DATA_PRECISION IS NULL
THEN COLUMN_NAME||' INTEGER,'
ELSE COLUMN_NAME||' '||DATA_TYPE||'('||TO_CHAR(DATA_PRECISION)||','||TO_CHAR(DATA_SCALE)||')'||DECODE(COLUMN_ID,(SELECT MAX

(B.COLUMN_ID) FROM USER_TAB_COLUMNS@REMOTEKGK B WHERE A.TABLE_NAME=B.TABLE_NAME),' ',',')
END
WHEN DATA_TYPE='NVARCHAR2'
THEN COLUMN_NAME||' '||DATA_TYPE||'('||TO_CHAR(DATA_LENGTH/2)||')'||DECODE(COLUMN_ID,(SELECT MAX(B.COLUMN_ID) FROM

USER_TAB_COLUMNS@REMOTEKGK B WHERE A.TABLE_NAME=B.TABLE_NAME),' ',',')
WHEN DATA_TYPE IN ('CHAR','VARCHAR2')
THEN COLUMN_NAME||' '||DATA_TYPE||'('||TO_CHAR(DATA_LENGTH)||')'||DECODE(COLUMN_ID,(SELECT MAX(B.COLUMN_ID) FROM

USER_TAB_COLUMNS@REMOTEKGK B WHERE A.TABLE_NAME=B.TABLE_NAME),' ',',')
WHEN DATA_TYPE IN ('DATE','BLOB','CLOB','LONG','NCLOB')
THEN COLUMN_NAME||' '||DATA_TYPE||DECODE(COLUMN_ID,(SELECT MAX(B.COLUMN_ID) FROM USER_TAB_COLUMNS@REMOTEKGK B WHERE

A.TABLE_NAME=B.TABLE_NAME),' ',',')
END AS SQLTEXT,
COLUMN_ID,
A.TABLE_NAME
FROM USER_TAB_COLUMNS@REMOTEKGK A
Where A.table_name Like 'T%'
) d
Where d.table_name Not In
(
Select c.table_name From USER_TABLES c
Where c.table_name Like 'T%'
)
ORDER BY TABLE_NAME,COLUMN_ID,SQLTEXT

--ADD NEW COLUMN ACCORDING THE LATEST TABLE NAME

SELECT 'ALTER TABLE '||TABLE_NAME||' ADD "'||COLUMN_NAME||'" '||
(CASE WHEN DATA_TYPE='NUMBER'
THEN
CASE WHEN DATA_PRECISION IS NULL
THEN 'INTEGER'
ELSE DATA_TYPE||'('||TO_CHAR(DATA_PRECISION)||','||TO_CHAR(DATA_SCALE)||')'
END
WHEN DATA_TYPE='NVARCHAR2'
THEN DATA_TYPE||'('||TO_CHAR(DATA_LENGTH/2)||')'
WHEN DATA_TYPE IN ('CHAR','VARCHAR2')
THEN DATA_TYPE||'('||TO_CHAR(DATA_LENGTH)||')'
WHEN DATA_TYPE IN ('DATE','BLOB','CLOB','LONG','NCLOB')
THEN DATA_TYPE
END)||' ;' SQLTEXT
FROM USER_TAB_COLUMNS@REMOTEKGK A
Where (a.table_name,a.column_name) Not In
(
Select b.table_name,b.column_name From USER_TAB_COLS b
Where b.table_name Like 'T%'
)
And A.table_name Like 'T%'
ORDER BY COLUMN_ID

--BASED THE COLUMN_NAME AND COLUMN_ID,NEED TO MODIFY THE COLUMN NAME

--BUT IN FACT MOST SITUATION IT RESULT IN NEW COLUMN,SO IT IS NO USEFUL

....

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8128313/viewspace-982546/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    127
  • 访问量
    1756093