SQLSERVER和ORACLE批量處理表名和欄位名大寫

來源:互聯網
上載者:User

標籤:

在sql移植到oracle過程中,都會遇到表名和欄位名大寫的問題,因為在oracle中預設表名和欄位名都是大寫的,雖然可以通過使用雙引號的方式進行操作,如select * from "testtable"但是很多情況下,這樣做的代價很大,因為很多程式的代碼是早已經寫好的,而要修改oracle系統配置又不安全,一個比較穩妥的辦法是批量修改表名和檔案名稱。

 

先說在SQL中修改表名和欄位名的辦法:雖然在sql中預設是不區分表名和欄位名的大小寫,但是通過sql的DTS進行資料匯出匯入的時候,如果表名和欄位名是小寫,那產生的程式碼中對應也是小寫,反之亦然。因此通過此辦法產生oracle資料表的時候就需要首先批量把sql中的表名和欄位名都修改為大寫,下面給出的是對應的代碼。

批量修改表名:

declare @sql varchar(300)--,@rowcount varchar(10),@dyncnum int    declare @tablename varchar(100)    declare cursor1 cursor for             select name  from sysobjects  where xtype = ‘u‘  order by name                   open cursor1                           fetch next from cursor1 into @tablename    while @@fetch_status=0               begin        set @sql=‘sp_rename ‘‘‘[email protected]+‘‘‘,‘‘‘+upper(@tablename)+‘‘‘‘        print @sql         --exec(@sql)                     fetch next from cursor1 into @tablename    end    close cursor1                       deallocate cursor1

批量修改欄位名代碼:

declare @sql varchar(300)    declare @tablecolumnname varchar(100), @columnname varchar(100)    declare cursor1 cursor for             select b.name+‘.[‘+a.name+‘]‘,a.name from syscolumns a  ,sysobjects b where a.id = object_id(b.name) and b.xtype = ‘u‘ and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36                    open cursor1                           fetch next from cursor1 into @tablecolumnname,@columnname    while @@fetch_status=0               begin        set @sql=‘sp_rename ‘‘‘[email protected]+‘‘‘,‘‘‘+upper(@columnname)+‘‘‘,‘‘column‘‘‘        --print @sql         exec(@sql)                     fetch next from cursor1 into @tablecolumnname,@columnname    end    close cursor1                       deallocate cursor1

還有一種情況就是,表已經匯入到oracle中了,但是發現表名和欄位名都是小寫,需要在oracle中修改表名和欄位名。

這個時候,我們需要使用一種辦法來幫我們自動產生修改的語句,然後copy出來執行就可以了。 

修改表名:

1 select ‘alter table "‘||table_name||‘" rename to ‘||upper(table_name)||‘;‘ from user_tables where table_name<>upper(table_name);

修改欄位名:

1 select ‘alter table ‘||table_name||‘ rename column "‘|| column_name ||‘" to ‘||upper(column_name)||‘;‘2 from user_tab_columns where column_name<>upper(column_name);

這個時候,我們可以在sql plus或者toad等用戶端工具中產生對應的alter語句,然後copy出來執行就ok了。

 

引申一下,如果需要把oracle中的資料全部清空或者把表全部刪除,也可以採用如上的方式

--清除資料select ‘TURNCATE table ‘||table_name ||‘;‘ from user_tables; --刪除表select ‘drop table ‘||table_name ||‘;‘ from user_tables; 

原文連結:http://www.cnblogs.com/tippoint/archive/2012/11/07/2758855.html

 

SQLSERVER和ORACLE批量處理表名和欄位名大寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.