日期:2014-05-17  浏览次数:20882 次

高分求助sql server上用的语句转换为oracle语句
原sql server查询语句
SQL code
Select cast(IsNULL(yhzh,replicate(' ',20)) as char(20))+cast(object_name as char(10))+cast(gh as char(10))+cast(1 as char(10))+cast(IsNULL(idcard_num,replicate(' ',20)) as char(20))+cast(sum(btje) as char(16))+'                    ' from t_sn_object as a Left Join t_sn_object_zhxx as b on a.id=b.btdx_uid inner Join czbt2.hnxc.czbt_btdj as c on a.id=c.btdx_uid inner Join objectgh as d on a.id=d.id where enable=1 and lxdm in ('0541','0542','0543','0546') and month(djrq)=4 group by yhzh,object_name,gh,idcard_num order by yhzh,gh



有这些东西需要转换
IsNull函数
replicate函数
Cast as函数
month函数
多表连接转换


------解决方案--------------------
SQL code

select nvl(yhzh,substr(rpad('a',21,' '),2,20))
||lpad(substr(object_name,1,10),10,' ')
||lpad(substr(gh,1,10),10,' ')
||lpad('1',10,' ')
||nvl(idcard_num,substr(rpad('a',21,' '),2,20))
||lpad(sum(btje),16,' ')
||substr(rpad('a',21,' '),2,20)
from t_sn_object a 
Left Join t_sn_object_zhxx b on a.id=b.btdx_uid 
inner Join czbt2.hnxc.czbt_btdj c on a.id=c.btdx_uid 
inner Join objectgh d on a.id=d.id 
    where enable=1 and lxdm in ('0541','0542','0543','0546')
        and substr(to_char(djrq,'mm'),2,1)=4 
group by yhzh,object_name,gh,idcard_num 
order by yhzh,gh;

------解决方案--------------------
先说说sql server ——oracle对应转换的函数
IsNull函数——nvl函数
replicate函数——lpad与rpad函数
Cast as函数——to_char函数
month函数——to_char(sysdate,'mm')函数
另外看到有个czbt2.hnxc.czbt_btdj 这个不知道是不是你的表名还是czbt2是个库名,hnxc是个用户名,czbt_btdj是表名,如果czbt2是个库名,转换成oracle的时候要加个dblink,引用为hnxc.czbt_btdj@czbt2;
具体的sql :
select rpad(to_char(nvl(yhzh,' ')),20,' ')||rpad(to_char(object_name),10,' ')||rpad(to_char(gh),10,' ')||rpad('1',10,' ')||rpad(to_char(nvl(idcard_num,' ')),20,' ')||rpad(to_char(sum(btje)),17,' ') from t_sn_object as a left join t_sn_object_zhxx as b on a.id=b.btdx_uid inner Join czbt2.hnxc.czbt_btdj as c on a.id=c.btdx_uid inner Join objectgh as d on a.id=d.id where enable=1 and lxdm in ('0541','0542','0543','0546') and to_char(djrq,'mm')='04' group by yhzh,object_name,gh,idcard_num order by yhzh,gh