摘要:最近项目中用到了oracle存储过程,所以就自己尝试着写了下,下面我把我遇到的问题描述一下:就是在我处理解析clob字段中的xml字符串的时候,有个需求就是根据指定的主键参数,来判断该参数的是否已经存在数据表里面,如果存在我就根据这个参数执行upda
摘要:最近项目中用到了oracle存储过程,所以就自己尝试着写了下,下面我把我遇到的问题描述一下:就是在我处理解析clob字段中的xml字符串的时候,有个需求就是根据指定的主键参数,来判断该参数的值是否已经存在数据表里面,如果存在我就根据这个参数执行update操作,如果不存在我就执行save操作:
一:oracle存储过程中如何根据指定的参数判断该参数的值否存在数据表中,以及通过oracle存储过程解析clob字段中的xml字符串到指定的数据表里面:
二:下面是具体的方法,主要是红色部分,是解决如果根据指定的参数判断该参数的值是否已经存在的数据表中:
create or replace procedure mip.mip_parse (xmlstr in clob)is reno varchar2 (100); airline varchar2 (100); ffid varchar2 (100); ffid_a varchar2 (100); ffid_d varchar2 (100); abns varchar2 (100); acft varchar2 (100); chdt varchar2 (100); eibt varchar2 (100); fata varchar2 (100); feta varchar2 (100); fsta varchar2 (100); lmdt varchar2 (100); lmur varchar2 (100); pstm varchar2 (100); rway varchar2 (100); spot varchar2 (100); stnd varchar2 (100); a_tobt varchar2 (100); a_weather varchar2 (100); asat varchar2 (100); bctm varchar2 (100); botm varchar2 (100); bstm varchar2 (100); c_tobt varchar2 (100); cobt varchar2 (100); ctot varchar2 (100); dint varchar2 (100); dlab varchar2 (100); dout varchar2 (100); eddi varchar2 (100); eobt varchar2 (100); epgt varchar2 (100); epot varchar2 (100); fatd varchar2 (100); fstd varchar2 (100); oftm varchar2 (100); stdi varchar2 (100); tsat varchar2 (100); --新增字段 flightnumber varchar2 (100); flightmark varchar2 (100); --定义出港信息表要格式的时间字段 a_tobt_d varchar2 (100); asat_d varchar2 (100); bctm_d varchar2 (100); botm_d varchar2 (100); bstm_d varchar2 (100); c_tobt_d varchar2 (100); cobt_d varchar2 (100); ctot_d varchar2 (100); dint_d varchar2 (100); dout_d varchar2 (100); eddi_d varchar2 (100); eobt_d varchar2 (100); epgt_d varchar2 (100); epot_d varchar2 (100); fatd_d varchar2 (100); fstd_d varchar2 (100); lmdt_d varchar2 (100); oftm_d varchar2 (100); stdi_d varchar2 (100); tsat_d varchar2 (100); --定义进港信息表要格式化的时间字段 eibt_a varchar2 (100); fata_a varchar2 (100); feta_a varchar2 (100); fsta_a varchar2 (100); lmdt_a varchar2 (100); pstm_a varchar2 (100); spot_a varchar2 (100); counts number(36); --出港信息要修改的除时间外的字段 stnd_d varchar2 (100); a_weather_d varchar2 (100); abns_d varchar2 (100); acft_d varchar2 (100); airline_d varchar2 (100); dlab_d varchar2 (100); lmur_d varchar2 (100); reno_d varchar2 (100); rway_d varchar2 (100); -- 进港信息要修改的除时间外的字段 abns_a varchar2 (100); acft_a varchar2 (100); airline_a varchar2 (100); chdt_a varchar2 (100); reno_a varchar2 (100); lmur_a varchar2 (100); rway_a varchar2 (100); stnd_a varchar2 (100); begin reno := getxmlnodevalue (xmlstr, 'reno'); airline := getxmlnodevalue (xmlstr, 'airline'); ffid := getxmlnodevalue (xmlstr, 'ffid'); abns := getxmlnodevalue (xmlstr, 'abns'); acft := getxmlnodevalue (xmlstr, 'acft'); chdt := getxmlnodevalue (xmlstr, 'chdt'); eibt := getxmlnodevalue (xmlstr, 'eibt'); fata := getxmlnodevalue (xmlstr, 'fata'); feta := getxmlnodevalue (xmlstr, 'feta'); ffid := getxmlnodevalue (xmlstr, 'ffid'); fsta := getxmlnodevalue (xmlstr, 'fsta'); lmdt := getxmlnodevalue (xmlstr, 'lmdt'); lmur := getxmlnodevalue (xmlstr, 'lmur'); pstm := getxmlnodevalue (xmlstr, 'pstm'); rway := getxmlnodevalue (xmlstr, 'rway'); spot := getxmlnodevalue (xmlstr, 'spot'); stnd := getxmlnodevalue (xmlstr, 'stnd'); a_tobt := getxmlnodevalue (xmlstr, 'a_tobt'); a_weather := getxmlnodevalue (xmlstr, 'a_weather'); asat := getxmlnodevalue (xmlstr, 'asat'); bctm := getxmlnodevalue (xmlstr, 'bctm'); botm := getxmlnodevalue (xmlstr, 'botm'); bstm := getxmlnodevalue (xmlstr, 'bstm'); c_tobt := getxmlnodevalue (xmlstr, 'c_tobt'); cobt := getxmlnodevalue (xmlstr, 'cobt'); ctot := getxmlnodevalue (xmlstr, 'ctot'); dint := getxmlnodevalue (xmlstr, 'dint'); dlab := getxmlnodevalue (xmlstr, 'dlab'); dout := getxmlnodevalue (xmlstr, 'dout'); eddi := getxmlnodevalue (xmlstr, 'eddi'); eobt := getxmlnodevalue (xmlstr, 'eobt'); epgt := getxmlnodevalue (xmlstr, 'epgt'); epot := getxmlnodevalue (xmlstr, 'epot'); fatd := getxmlnodevalue (xmlstr, 'fatd'); fstd := getxmlnodevalue (xmlstr, 'fstd'); oftm := getxmlnodevalue (xmlstr, 'oftm'); stdi := getxmlnodevalue (xmlstr, 'stdi'); tsat := getxmlnodevalue (xmlstr, 'tsat'); --出港信息表中时间字段的时间格式函数的用法 a_tobt_d := formatdatevalue (a_tobt, 'a_tobt_d'); asat_d := formatdatevalue (asat, 'asat_d'); bctm_d := formatdatevalue (bctm, 'bctm_d'); botm_d := formatdatevalue (botm, 'botm_d'); bstm_d := formatdatevalue (bstm, 'bstm_d'); c_tobt_d := formatdatevalue (c_tobt, 'c_tobt_d'); cobt_d := formatdatevalue (cobt, 'cobt_d'); ctot_d := formatdatevalue (ctot, 'ctot_d'); dint_d := formatdatevalue (dint, 'dint_d'); dout_d := formatdatevalue (dout, 'dout_d'); eddi_d := formatdatevalue (eddi, 'eddi_d'); eobt_d := formatdatevalue (eobt, 'eobt_d'); epgt_d := formatdatevalue (epgt, 'epgt_d'); epot_d := formatdatevalue (epot, 'epot_d'); fatd_d := formatdatevalue (fatd, 'fatd_d'); fstd_d := formatdatevalue (fstd, 'fstd_d'); lmdt_d := formatdatevalue (lmdt, 'lmdt_d'); oftm_d := formatdatevalue (oftm, 'oftm_d'); stdi_d := formatdatevalue (stdi, 'stdi_d'); tsat_d := formatdatevalue (tsat, 'tsat_d'); --进港信息表中时间字段的时间格式函数的用法 eibt_a := formatdatevalue (eibt, 'eibt_a'); fata_a := formatdatevalue (fata, 'fata_a'); feta_a := formatdatevalue (feta, 'feta_a'); fsta_a := formatdatevalue (fsta, 'fsta_a'); lmdt_a := formatdatevalue (lmdt, 'lmdt_a'); pstm_a := formatdatevalue (pstm, 'pstm_a'); spot_a := formatdatevalue (spot, 'spot_a'); --出港信息要修改的除时间外的字段 stnd_d := getxmlnodevalue (xmlstr, 'stnd'); a_weather_d := getxmlnodevalue (xmlstr, 'a_weather'); abns_d := getxmlnodevalue (xmlstr, 'abns'); acft_d := getxmlnodevalue (xmlstr, 'acft'); airline_d := getxmlnodevalue (xmlstr, 'airline'); dlab_d := getxmlnodevalue (xmlstr, 'dlab'); lmur_d := getxmlnodevalue (xmlstr, 'lmur'); reno_d := getxmlnodevalue (xmlstr, 'reno'); rway_d := getxmlnodevalue (xmlstr, 'rway'); --进港信息要修改的除时间外的字段 abns_a := getxmlnodevalue (xmlstr, 'abns'); acft_a := getxmlnodevalue (xmlstr, 'acft'); airline_a := getxmlnodevalue (xmlstr, 'airline'); chdt_a := getxmlnodevalue (xmlstr, 'chdt'); reno_a := getxmlnodevalue (xmlstr, 'reno'); lmur_a := getxmlnodevalue (xmlstr, 'lmur'); rway_a := getxmlnodevalue (xmlstr, 'rway'); stnd_a := getxmlnodevalue (xmlstr, 'stnd'); if instr(ffid,'-d-') > 0 then ffid_d := ffid; --截取航班号 flightnumber := substr(ffid_d,instr(ffid_d,'-',1)+1,instr(ffid_d,'-',instr(ffid_d,'-',1)+1)-instr(ffid_d,'-',1)-1); --截取出港标志 flightmark := substr(ffid_d,instr(ffid_d,'-',2,2)+1,instr(ffid_d,'-',2,3)-1-instr(ffid_d,'-',2,2)); --update之前要先根据ffid查询一下看看数据表中是否已经存在该条数据,如果存在就update,如果不存在就save select count(1) into counts from tb_cms_flgtinfo_d where ffid = ffid_d; if counts > 0 then update tb_cms_flgtinfo_d set a_tobt = a_tobt_d, a_weather = a_weather_d, abns = abns_d, acft = acft_d, airline = airline_d, asat = asat_d, bctm = bctm_d, botm = botm_d, bstm = bstm_d, c_tobt = c_tobt_d, cobt = cobt_d, ctot = ctot_d, dint = dint_d, dlab = dlab_d, dout = dout_d, eddi = eddi_d, eobt = eobt_d, epgt = epgt_d, epot = epot_d, fatd = fatd_d, fstd = fstd_d, lmdt = lmdt_d, lmur = lmur_d, oftm = oftm_d, reno = reno_d, rway = rway_d, stdi = stdi_d, stnd = stnd_d, tsat = tsat_d where ffid = ffid_d; else insert into tb_cms_flgtinfo_d (id,a_tobt,a_weather,abns,acft,airline,asat,bctm,botm,bstm,c_tobt,cobt,ctot,dint,dlab,dout,eddi,eobt,epgt,epot,fatd,ffid,flightnumber, flightmark,fstd,lmdt,lmur,oftm,reno,rway,stdi,stnd,tsat) values (flgtinfo_d_seq.nextval, a_tobt_d, a_weather, abns, acft, airline, asat_d, bctm_d, botm_d, bstm_d, c_tobt_d, cobt_d, ctot_d, dint_d, dlab, dout_d, eddi_d, eobt_d, epgt_d, epot_d, fatd_d, ffid_d, flightnumber, flightmark, fstd_d, lmdt_d, lmur, oftm_d, reno, rway, stdi_d, stnd, tsat_d); end if; else ffid_a := ffid; --截取航班号 flightnumber := substr(ffid_a,instr(ffid_a,'-',1)+1,instr(ffid_a,'-',instr(ffid_a,'-',1)+1)-instr(ffid_a,'-',1)-1); --截取出港标志 flightmark := substr(ffid_a,instr(ffid_a,'-',2,2)+1,instr(ffid_a,'-',2,3)-1-instr(ffid_a,'-',2,2)); --update之前要先根据ffid查询一下看看数据表中是否已经存在该条数据,如果存在就update,如果不存在就save select count(1) into counts from tb_cms_flgtinfo_a where ffid = ffid_a; if counts > 0 then update tb_cms_flgtinfo_a set abns = abns_a, acft = acft_a, airline = airline_a, chdt = chdt_a, reno = reno_a, eibt = eibt_a, fata = fata_a, feta = feta_a, fsta = fsta_a, lmdt = lmdt_a, lmur = lmur_a, pstm = pstm_a, rway = rway_a, spot = spot_a, stnd = stnd_a where ffid = ffid_a; else insert into tb_cms_flgtinfo_a (id,abns,acft,airline,chdt,ffid,flightnumber,flightmark,reno,eibt,fata,feta,fsta,lmdt,lmur,pstm,rway,spot,stnd) values (flgtinfo_a_seq.nextval, abns, acft, airline, chdt, ffid_a, flightnumber, flightmark, reno, eibt_a, fata_a, feta_a, fsta_a, lmdt_a, lmur, pstm_a, rway, spot_a, stnd); end if; end if; commit; exception when others then dbms_output.put_line (sqlerrm); end mip_parse;/
三:详情请看:http://www.zuidaima.com/question/2041312860310528.htm