SqlServer与Oracle语法差异

来源:互联网 发布:网络砍价师 编辑:程序博客网 时间:2024/05/17 07:32

这不是总结文章,是我随遇随记的流水文,免得以后忘记。sqlserver是2008,oracle是11g。因为本小白同时接触两种数据库,不得已要在blog上开记流水账了大笑

1.  插入多个值到数据表

sqlserver:

insert into Tbl_COT(WaferID,VF1Avg,LOP1Avg) values('18DIV07P151224A38G',3.125,93.236),('18DIV08P151224A38G',3.115,92.236),('18DIV09P151224A38G',3.025,90.236)

oracle:   参考  http://software.cnw.com.cn/software-database/htm2014/20140220_291620.shtml

insert All into Tbl_COT(WaferID,VF1Avg,LOP1Avg) values('18DIV07P151224A38G',3.125,93.236) into Tbl_COT(WaferID,VF1Avg,LOP1Avg) values ('18DIV08P151224A38G',3.115,92.236) into Tbl_COT(WaferID,VF1Avg,LOP1Avg) values('18DIV09P151224A38G',3.025,90.236) 
select 1 from dual


似乎oracle麻烦了一些,要指定每个表的字段名,还要追加select 1 from dual这个无意思的尾巴。但是insert all 本来是为了将相同的数据同时插入多个(具有相同字段类型的)表使用的。

2. 更新多个字段

sqlserver:
update Tbl_COT set VF1Avg=3.10,LOP1Avg=90.203 where WaferID='18DIV08P151224A38G'
oracle:
update Tbl_COT set (VF1Avg,LOP1Avg)=(select 3.10,90.203 from dual) where WaferID='18DIV08P151224A38G' 
当字段较多时,Oracle会显得更简洁一些。嗯,算是oracle扳回一局吧

3. 对sql语句 select 4 from 数据表 where 5=6 执行结果不一样

sqlserver:
select 4 from MyDatabase.dbo.Test where 4=5

返回值为null
oracle:
select 4 from sajet.Test where 4=5

返回值为4

这个是我自己试出来的,算是发现的小bug吧

4. 对忽略唯一键/索引上插入的重复值的处理

有时我们会在一个创建了唯一键/索引的表中插入大量数值,游标方式检查每个记录是否符合唯一约束显然是费力不讨好的。数据库中各有办法实现“忽略重复值并插入不重复数值”的目的:

sqlserver:创建表的索引时将索引变更
create unique index Ind_MachineNo on tbl_MachineAdd(MachineNo) with ignore_dup_key 

在表tbl_MachineAdd的列MachineNo上创建的唯一索引,具备with ignore_dup_key = on 的性质。所有插入多条数据的语句,比如 insert into tbl_MachineAdd(MachineNo) select 子句 或者 insert into tbl_MachineAdd(MachineNo)  values('M01'),('M02'),('M01') 这样的sql语句,以后者为例,结果会将'M01','M02'两条记录插入表中。
oracle:做insert操作时使用hint忽略重复的值

创建唯一索引时没有什么特别的:

create unique index Ind_MachineNo on tbl_MachineAdd(MachineNo)

但是在使用insert语句时,可以使用hint【注意/*和+之间是没有空格的,否则就成了注释,不会被Oracle认为是hint。语法错误的Hint会被Oracle自动理解为注释】

 

insert /*+ Ignore_row_on_dupkey_Index(tbl_MachineAdd(MachineNo)) */ all into tbl_MachineAdd(MachineNo) values('M01') into tbl_MachineAdd(MachineNo) values('M02') into tbl_MachineAdd(MachineNo) values('M01') 


这个会报错:ORA-38917: 此操作不允许 IGNORE_ROW_ON_DUPKEY_INDEX 提示。但是使用select子句可以实现我们的功能。假设在另一张表

create table Dup(plane varchar2(20)); 中已经插入了数值'M01','M02','M01',

 

insert /*+ Ignore_row_on_dupkey_Index(tbl_MachineAdd(MachineNo)) */ into tbl_MachineAdd(MachineNo) select * from Dup;

 

则可以成功插入两条记录。

0 0
原创粉丝点击