SQLServer表結構轉換成Oracle表結構

2021-07-31 17:29:42 字數 4511 閱讀 4568

/******************** 

* function:sqlserver 表結構轉換成oralce 表結構,不支援索引以及自動增長

**********************/

--取消影響行數

set nocount on;

--建立表名游標

declare table_cursor cursor for

select convert(varchar(300),x.name) name,convert(varchar(500),y.value) value

from sys.tables x

left join (select major_id,value from sys.extended_properties where minor_id=0) y

on x.object_id=y.major_id

where x.name='t_statistics_wateroutputmonth'

order by x.name;

--宣告變數

declare

@sql varchar(max)='',

@primary varchar(300),

@tablename varchar(300), --表名稱

@tabledes varchar(500); --表名稱描述

--建立表結構臨時表

create table #table(colname varchar(300),

isprimary int,

typename varchar(50),

intlength int,

decimallength int,

nullflag int,

defaultval varchar(50),

commonts varchar(500) )

--開啟游標

open table_cursor;

fetch next from table_cursor into @tablename,@tabledes;

while @@fetch_status = 0

begin

truncate table #table;

insert into #table (colname,isprimary,typename,intlength,decimallength,nullflag,defaultval,commonts)

select convert(varchar(300),a.name) [欄位名],

(case when (select count(*)

from sysobjects

where (name in (select name

from sysindexes

where (id = a.id)

and (indid in (select indid

from sysindexkeys

where (id = a.id)

and (colid in (select colid

from syscolumns

where (id = a.id)

and (name = a.name)))))))

and (xtype = 'pk')) > 0 then 1

end) [主鍵],

b.name [型別],

columnproperty(a.id, a.name, 'precision') as [長度],

isnull(columnproperty(a.id, a.name, 'scale'), 0) as [小數字數],

(case when a.isnullable = 1 then 1 else 0 end) [允許空],

e.text [預設值],

convert(varchar(500),g.[value]) as [說明]

from syscolumns a

left join systypes b

on a.xtype = b.xusertype

inner join sysobjects d

on a.id = d.id

and d.xtype = 'u'

and d.name <> 'dtproperties'

left join syscomments e

on a.cdefault = e.id

left join sys.extended_properties g

on a.id = g.major_id

and a.colid = g.minor_id

left join sys.extended_properties f

on d.id = f.class

and f.minor_id = 0

where b.name is not null

and d.name=@tablename

order by a.id,a.colorder

--初始化變數

set @sql='';

--構建表結構

select @sql+='

'+case colname when 'comment' then 'comments' else colname end +' '

+case typename

when 'varchar' then 'varchar2('+convert(varchar(10),intlength)+') '

when 'int' then 'number(4) '

when 'decimal' then 'number('+convert(varchar(10),intlength)+ ( case when decimallength>0 then ','+ convert(varchar(10),decimallength) else '' end)+') '

when 'datetime' then 'date '

else typename

end

+ case when defaultval is not null and len(defaultval)>0 then 'default '+

(case when charindex('getdate',defaultval)>0 then 'sysdate '

when charindex('newid',defaultval)>0 then 'sys_guid() '

else (case when typename='int' or typename='decimal' then replace(replace(defaultval,'(',''),')','') else defaultval end )

end)

else '' end

+ case when nullflag=0 then ' not null,' else ',' end

from #table;

select * from #table

if @sql is not null and len(@sql)>0

begin

set @sql=left(@sql,len(@sql)-1);

--建立表結構

set @sql='create table '+ @tablename+'('+@sql+

');comment on table '+@tablename+' is '''+@tabledes+''';

'

--新增備註

select @sql+= case when commonts is not null and len(commonts)>0 then 'comment on column '+@tablename+'.'+colname+' is '''+commonts+''';

' else '' end

from #table;

--新增主鍵索引

if exists(select 1 from #table where isprimary=1 )

begin

set @primary=''

select @primary+= colname+','

from #table

where isprimary=1

set @primary=left(@primary,len(@primary)-1);

set @sql+='alter table '+@tablename+' add constraint pk_'+@tablename+' primary key ('+@primary+');'

endend

print @sql;

fetch next from table_cursor into @tablename,@tabledes;

endclose table_cursor;

deallocate table_cursor;

drop table #table;

Oracle轉換成SqlServer資料庫的步驟

1 背景 itm系統目前支援mysql oracle資料庫,由於現在需要和ca產品進行結合,用ca產品的採集功能進行資料採集,因此需要使用ca產品的sqlserver資料庫,為了使得系統支援sqlserver資料庫,首先需將oracle資料庫遷移到mysql中,然後修改系統中dao對應xml配置檔案...

小寫金額轉換成大寫 sql server

if exists select from dbo.sysobjects where id object id n dbo l2u andxtype in n fn n if n tf drop function dbo l2u go create function dbo.l2u n lowerm...

小寫金額轉換成大寫 sql server

if exists select from dbo.sysobjects where id object id n dbo l2u andxtype in n fn n if n tf drop function dbo l2u go create function dbo.l2u n lowerm...