您的位置:首页 > 数据库 > Oracle

oracle存储过程转化成sqlserver

2014-04-13 14:59 162 查看
oracle存储过程转化成sqlserver

1. 变量的定义 ,sqlserver变量名前加@. eg: a varchar2(4) --> @a varchar(4).

2. 类型转化,将varchar2转化成varchar. eg: 参上.

3. 变量的赋值,将 :=替换成set. eg: a :='abc' --> set @a = 'abc'.

4. 字符串的拼接,将||替换成+.eg: a = 'ab'||'c' --> a='ab'+'c'.

5. 语句结构,去掉所有;.

6. if结构的改变. eg: if ... then end if; --> if ..begin ..end end.

7. 返回结果集.oracle可以定义游标类型返回.

java中采用注册输出参数,然后获取结果集.

sqlserver中不返回结果集,改成exec(@querySql)

java中采用executeQuery获取结果集.

8. 注意拼接sql语句是,如果需要用单引号,需要用两个单引号进行转义.

1个引号时,而且是前置时,就是表示所辖内容为字符串.

2个引号时,而且是在语句中,表示一个'.第一个为转义

3个引号时,而且是前置时,第一个表示字符串前引号,第二个是转义字符,第三个表示引号(字符内容)

eg:a = ''' 相当于一个'

set @retJSON = '{msg:''获取页面准备数据出错:' + SUBSTRING(ERROR_MESSAGE(), 1, 200) + ''''

/**

* 通用结构

*/

-- 存在则删除

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[XXX]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)

drop procedure [dbo].[XXX]

GO

create procedure XXX(@x1 varchar,...,@put varchar out)

as

begin

-- 定义变量

declare @a datetime;

declare @b varchar(20);

declare @c date;

...;

-- 定义函数体

begin try

begin transaction

-- 变量赋值

set @b = convert(datetime,@x1,120)

select @b=name from t

-- with (xlock holdlock) 表锁直至事务释放

where id =@a

set @a = @a + 'b' + @b

-- 使用if结构

if @b is not null or @b = 'b'

begin

delete from call_list

where convert(datetime, substring(call_list.id, 0, 8), 112) <= @queryTime

end

-- 使用游标

declare v_cur cursor for select 1 from a;

open v_cur

fetch next from v_cur into @a

while @@fetch_status=0

begin

...

end

close v_cur

deallocate v_cur -- 删除对游标命名变量的引用

print @a -- 打印语句

--创建call_list索引wsj-begin

set @v_SQL=' create index
PKL_TASKID_'+@i_tenantId+' on CALL_LIST_' + @i_tenantId +' (TASK_FK)'

Exec (@v_SQL)

exec (@querySql) -- 返回结果集,无需输出参数java可以直接调用获取

commit

end try

begin catch

rollback

set @retCode = '-1'

set @retJSON = '{msg:''获取页面准备数据出错:' + SUBSTRING(ERROR_MESSAGE(), 1, 200) + ''''

end catch

end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: