利用ODBC從SQLServer向Oracle中導資料

來源:互聯網
上載者:User

標籤:blog   http   io   for   ar   資料   2014   代碼   log   

1、首先要在Oracle資料庫中建對應的表,Oracle資料庫中的欄位類型和Sql Server 有所不同,Oracle中常用的有varchar2、integer、nchar、date,Sql Server 的欄位類型相對多一些,uniqueidentifier類型的長度是36。

2、開啟管理工具——資料來源(ODBC),配置SqlServer的資料來源。

3、開啟Oracle資料庫——工具——ODBC匯入

4、選擇伺服器,輸入資料庫帳號和密碼

5、選擇使用者和表,然後點擊左下角的匯入按鈕即可

6、附上由SqlServer表自動產生Oracle的代碼:

/********1.定義變數*********/ DECLARE @table_id int, @table_name varchar(100), @column_id int, @c_name sysname, @c_type varchar(50), @max_length smallint, @precision tinyint, @scale tinyint, @is_nullable bit, @cons_sql varchar(5000)

/********2.變數初始化*********/ set @table_name=‘mio_log‘ set @cons_sql=‘create table ‘[email protected]_name+‘(‘

/********3.取表的id*********/ DECLARE tbl_cursor CURSOR FOR select a.id from sys.sysobjects  a where a.id = object_id(N‘[dbo].[‘[email protected]_name+‘]‘) and OBJECTPROPERTY(id, N‘IsUserTable‘) = 1

OPEN tbl_cursor FETCH NEXT FROM tbl_cursor INTO @table_id

/********4.構建表的結構*********/ DECLARE columns_cursor CURSOR FOR SELECT b.column_id,b.name,c.name,b.max_length, b.precision,b.scale,b.is_nullable from sys.columns b,sys.systypes c where [email protected]_id and b.system_type_id=c.xtype order by b.column_id asc

OPEN columns_cursor

FETCH NEXT FROM columns_cursor INTO @column_id, @c_name, @c_type, @max_length, @precision, @scale, @is_nullable

WHILE @@FETCH_STATUS = 0 BEGIN set @[email protected][email protected]_name+‘ ‘+ (case when @c_type in(‘smallint‘,‘tinyint‘) then ‘int‘ when @c_type=‘int‘ then ‘number‘ when @c_type =‘varchar‘ then ‘varchar2(‘+convert(varchar(2),@max_length)+‘)‘ when @c_type in(‘datetime‘,‘smalldatetime‘) then ‘date‘ when @c_type=‘decimal‘ then ‘number‘+‘(‘+convert(varchar(1),@precision)+‘,‘+convert(varchar(1),@scale)+‘)‘ when @c_type=‘char‘ then ‘char(‘+convert(varchar(2),@max_length)+‘)‘ when @c_type=‘text‘ then ‘long‘ --此處有待於進一步資料類型的映射 else ‘##‘ end)+ (case when @is_nullable=1 then ‘ null‘ else ‘ not null‘ end)+‘,‘ FETCH NEXT FROM columns_cursor INTO @column_id, @c_name, @c_type, @max_length, @precision, @scale, @is_nullable END

/********5.輸出建表語句*********/ select left(@cons_sql,Len(@cons_sql)-1)+‘);‘

/********6.釋放遊標*********/ DEALLOCATE tbl_cursor DEALLOCATE columns_cursor

 

利用ODBC從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.