一、语法对比
Oracle | Sql Server | |
变量声明 | (只能在is或者as后面begin前) V_1 number | (Begin 后都可以) Declare @V_1 bigint |
变量赋值 | V_1 := 1; | select @v1=1 |
打印信息 | Dbms_output.putline(‘11’); | Print ‘111’ |
语句结束 | 以分号结束,必须填写 | 可以不写分号 |
临时表 | 建表的时候,持续时间那边勾选临时 | #开头的表 |
日期表示 | 以下几种形式均可表示日期 Sysdate、date ‘2022-08-17’、 To_date(‘2022-08-17’,’yyyy-mm-dd’) | ‘2022-08-17’ |
当前时间 | Sysdate | GETDATE() |
查询常量 | 使用dual 辅助 Select sysdate from dual | Select GETDATE() |
字符串拼接 | ‘a’||’b’ | ‘a’+’b’ |
dual | oracle独有 |
二、函数对比
Oracle | Sql Server | |
空值转换函数 | nvl(value1,value2) | isnull(value1,value2) |
格式转换 | to_number To_char To_date(时间,’格式’) | CONVERT(数据类型,待转换的变量) |
打印信息 | Dbms_output.putline(‘11’); | Print ‘111’ |
行转列 | wm_concat()函数 -- 独有 | |
日期加一个月 | add_months(date,int) --例:当前时间加一个月 add_months( sysdate,1) | 内置函数 DATEADD(interval,increment int,expression smalldatetime) returns smalldatetime --当前时间加一个月 DATEADD(MONTH,1,GETDATE()) --修改第一个参数 变为当前时间加一天 DATEADD(day,1,GETDATE()) |
三、其他
1.游标
oracle
declare
--在begin前 声明游标
cursor cur_test is ( select sysdate as today from dual );
begin
--begin之后 读取游标
-- 游标遍历
for rec_ in cur_test loop
--逻辑处理
dbms_output.put_line(rec_.today);
end loop;
end ;
sql Server
--声明游标
declare item_cursor cursor for (
select GETDATE() as today --这里写语句
)
--打开游标
open item_cursor
--声明变量
declare @today datetime
--读取游标中的数据 next为:读取游标当前行下一行数据
fetch next from item_cursor
into @today
while @@FETCH_STATUS = 0 --游标读取成功
BEGIN
--逻辑处理
print '这是游标里的数据:' + convert(varchar(20),@today)
FETCH NEXT FROM item_cursor into @today --将游标向下移1行
END
close item_cursor --关闭游标
deallocate item_cursor --释放游标
2.异常捕获
oracle
declare
begin
--逻辑处理
exception
when others then
rollback;--异常回滚
end
sql server
begin try
--逻辑处理
end try
begin catch
rollback tran --回滚
end carch