關於資料庫物件版本比較的指令碼

2021-09-22 04:54:30 字數 3388 閱讀 8423

專案原因導致出現兩個開發環境,主資料庫環境因需求變更每天都需要進行指令碼的修改,而報表伺服器的指令碼也需要同步更新,需求變更很少會同步提醒的;人工判斷太過於麻煩,我又是乙個懶人;只好寫個指令碼自動進行識別並加以執行了。

這只是乙個簡單的版本比較工具,事實上變更是很難判斷的,例如表中新增了乙個字段,導致順序發生變更,這個時候你很難判斷是新增還是修改的;諸如字段型別的修改.因此只能去判斷表是否存在,字段是否存在,進而執行判斷指令碼,產生相應的表指令碼和字段教本。

事實上oracle有個dbms_metadata資料報提供了dll指令碼,不過包含了很多儲存引數,對版本比較和物件生成沒有什麼意義。

--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%'

) dwhere 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

mysql資料庫中關於時間的比較

問題背景 1 影片開始時間大於當前時間不足15分鐘時為暫停購票 超過15分鐘即可購票 解決方案 starttime為varchar型別 str to date starttime,y m d h i s 轉變為date型別 mysql查詢語句 select unix timestamp str to...

查詢資料庫物件建立指令碼的SQL

select text from syscomments where id select id from sysobjects where name vw orderitem vw orderitem為檢視的名稱 可以查詢表名,檢視名,儲存過程名等 select from sysobjects wh...

資料庫指令碼

資料庫的建立 create database student 資料庫名 containment none onprimary name n student 主資料檔案的邏輯名稱 filename n c datalibrary student.mdf 主資料檔案的物理名稱 size 5120kb 主...