oracle常用SQL语句和一些技巧


一、某个字段重复了,要将重复的删除(可以是ID,也可以是其它的某个字段)

delete from t_qingsuandan2
where t_qingsuandan2.chehao in (select   chehao from t_qingsuandan2 group by   chehao   having count(chehao) > 1)
and rowid not in (select min(rowid) from   t_qingsuandan2 group by chehao having count(chehao )>1);

 

 

二、存储过程中调用子存储过程

(1)主存储过程

create or replace procedure p_Table14Build_out(
p_yyyy number,
p_mm number ,
p_Tielu varchar2


)
--测试数据:2011,
as
 CUR_1 number;
 Cur_1_return number;
strSql1 varchar2(3000);
t_table14_id1 number;
t_table14_id2 number;
t_Day_Start Date;--本月的第一天
t_Day_End Date;--本月的最后一天

t_dd number;--日
t_CheShu number;--车数
t_ZhouShu number;--轴数
t_bianhao  varchar2(3000);--车交单编号
t_mmdd varchar(50);
begin
  --删除原有的信息(可能要考虑审核正确的不删除)

  delete t_table14_2 where t_table14_2.table14_id1
  in(select t_table14_1.table14_id1 from t_table14_1 where t_table14_1.yyyy=p_yyyy and mm=p_mm
              and t_table14_1.tielu=p_tielu and t_table14_1.inout='出口' AND t_table14_1.inputtype='自动生成');

 delete t_table14_1 where t_table14_1.yyyy=p_yyyy and mm=p_mm and t_table14_1.inout='出口'
              and t_table14_1.tielu=p_tielu AND t_table14_1.inputtype='自动生成';
              commit;


  --获取主表的ID号
  select St_Table14_1.Nextval into t_table14_id1 from dual;--获取主表的ID号
  --插入主表t_table14_1
   insert into t_table14_1
     (table14_id1, bianhao, qinhsundeptname, quduan, yyyy, mm, yyyymm,
tielu, hejizhou, hejizhoukm, chae, hejikuane,  inputtype,
language, km, moneypercent, quduan1, quduan2, inout)
   values
     (t_table14_id1, '', '', '', p_yyyy, p_mm, to_char(p_yyyy)||'-'||to_char(p_mm) ,
p_tielu, 0, 0, 0, 0, '自动生成',
'外方', 0,0, '', '', '出口');


--生成中方准轨车的14号表
t_Day_Start:=To_Date(to_char(p_yyyy)||'-'||to_char(p_mm)||'-1','yyyy-mm-dd');--起始日期,如:2011-05-01
select add_months(t_Day_Start,1) into  t_Day_End from dual;--获取终止日期,如:2011-06-01
t_dd:=1;
--循环检查每一天的数据
while t_Day_Start<t_Day_End loop

            --获取当天车交单的车数/轴数
            select count(t2.jiaojiedanid2),sum(t2.zhoushu)  into  t_CheShu,t_ZhouShu
          from t_jiaojiedan2 t2,t_jiaojiedan1 t1
          where t1.jiaojiedanid1=t2.jiaojiedanid1
          and t1.inout='出口' and t1.kz='Z'
          and t1.from_date=to_date(to_char(t_Day_Start,'yyyy-mm-dd'),'yyyy-mm-dd')
          and t1.language='外方' and KH='货车';

   t_mmdd:=to_char(p_mm)||'-'|| to_char(t_dd);--月-日
--调用子存储过程,返回车交单编号到t_bianhao变量中(t_bianhao为输出参数,其它参数为输入参数)
   p_Table14Build_GetJJDBianHao(to_char(t_Day_Start,'yyyy-mm-dd'),p_Tielu,'外方','出口','Z',t_bianhao);
   --插入记录到明细表t_Table14_2中
    select St_Table14_2.Nextval into t_table14_id2 from dual;
   insert into t_table14_2
     (table14_id1, table14_id2, riqi, yyyy, mm, mmdd, checi, cheshu, zhoushu,
     jiaojiedanhaoma,  tielu, quduan, km,
      inout, language, dd)
   values
   (t_table14_id1,t_table14_id2,t_Day_Start,p_yyyy,p_mm,t_mmdd,'',t_CheShu,t_ZhouShu,
   t_bianhao,p_Tielu,'',0,
   '出口','外方',t_dd);
    t_Day_Start:=t_Day_Start+1;--日期加一天
   t_dd:=t_dd+1;--天数加一天
 commit;

end loop;

end p_Table14Build_out;

(2)子存储过程(有输入参数,也有输出参数)

create or replace procedure p_Table14Build_GetJJDBianHao(
p_date varchar2,--日期
p_Tielu varchar2,
p_lgg varchar2,--中方外方
p_inout varchar2,--进出口
p_KZ varchar2,--宽轨与准轨

p_BianHao out  varchar2--返回和车交单编号
)
as
 CUR_1 number;
 Cur_1_return number;
strSql1 varchar2(3000);
t_bianhao  varchar2(3000);
begin
  p_BianHao:='';
  strSql1:='select distinct t1.bianhao from t_jiaojiedan2 t2,t_jiaojiedan1 t1';
  strSql1:=strSql1||' where t1.jiaojiedanid1=t2.jiaojiedanid1 and t1.inout=:p_inout ';
  strSql1:=strSql1||' and t1.from_date=to_date(:p_date,''yyyy-mm-dd'') and t1.language=:p_lgg and KH=''货车'' and t1.kz=:p_KZ';
  CUR_1 := dbms_sql.open_cursor; --打开游标;
  dbms_sql.parse(CUR_1, strSql1, dbms_sql.native); --解析动态SQL语句;
  dbms_sql.bind_variable(CUR_1, ':p_date', p_date); --绑定输入参数
  dbms_sql.bind_variable(CUR_1, ':p_lgg', p_lgg);
  dbms_sql.bind_variable(CUR_1, ':p_inout', p_inout);
  dbms_sql.bind_variable(CUR_1, ':p_KZ', p_KZ);

  dbms_sql.define_column(CUR_1, 1, t_bianhao,1000); --定义输出的列(string类型必须指定长度)--车号
    Cur_1_return:=dbms_sql.execute(CUR_1); --执行动态SQL语句。
  LOOP
  EXIT WHEN dbms_sql.fetch_rows(CUR_1)<=0;


      dbms_sql.column_value(CUR_1, 1, t_bianhao); ----将当前行的查询结果写入上面定义的列中。
      if t_bianhao is not null  then
        if p_BianHao is null then
          p_BianHao:=t_bianhao;
          else
      p_BianHao:=p_BianHao||','|| t_bianhao;
      end if;
        end if;

  END LOOP;
  dbms_sql.close_cursor(CUR_1);
--returning p_BianHao;   --返回值

end p_Table14Build_GetJJDBianHao;

 

三、动态游标的使用


create or replace procedure p_Table8Check_in(
p_userid in  number,
p_yyyy in number,
p_mm in number,
p_tielu1 in varchar2, --要审核的铁路国别名称,如:哈铁,
p_RESULTID in number--本次审核的结果的ID号

 )
 as

strSql1 varchar2(3000);
CUR_1 number;
Cur_1_return number;
t1_TABLE8_ID1 NUMBER;--源数据的ID1和ID2
t1_TABLE8_ID2  NUMBER;
t_tempcount number;--记录数量的临时变量

t2_TABLE8_ID1 NUMBER;--匹配数据的ID1和ID2
t2_TABLE8_ID2  NUMBER;


begin
    --按接收日期返回没有审核和审核不正确的数据

    strSql1:='select t2.TABLE8_ID1,t2.TABLE8_ID2 from t_table8_2 t2,t_table8_1 t1 where t1.table8_id1=t2.table8_id1 and  (t2.checkresult is null or t2.checkresult!=''正确'')';
    strSql1:=strSql1||' and t1.yyyy=' || p_yyyy || ' and t1.mm='|| p_mm ||' and t1.inout=''进口''';
    strSql1:=strSql1||' and language=''中方'' and t1.tielu2='''|| p_tielu1||''''||' and (t1.inputtype=''手工录入'' or  t1.inputtype=''表格导入'')';

    CUR_1 := dbms_sql.open_cursor; --打开游标;
    dbms_sql.parse(CUR_1, strSql1, dbms_sql.native); --解析动态SQL语句;
    dbms_sql.define_column(CUR_1, 1, t1_TABLE8_ID1);--输出列
    dbms_sql.define_column(CUR_1, 2, t1_TABLE8_ID2);

    Cur_1_return:=dbms_sql.execute(CUR_1); --执行动态SQL语句。
    LOOP
    EXIT WHEN dbms_sql.fetch_rows(CUR_1)<=0;
                  dbms_sql.column_value(CUR_1, 1, t1_TABLE8_ID1);
                  dbms_sql.column_value(CUR_1, 2, t1_TABLE8_ID2);
                  --此处是否要将两个记录的记录全部都要清除呢?(只有完全正确的数据才会填写双方匹配的记录的ID号)
if t1_TABLE8_ID2=6872 then
  t1_TABLE8_ID2:=6872;
  end if;
                  update t_table8_2 set t_table8_2.checkresult=null,t_table8_2.matchtable8_id1=0,
                  t_table8_2.matchtable8_id2=0,t_table8_2.errcolmemo=null,
                  t_table8_2.errcolname=null,
                  t_table8_2.checkresultstate=null ,t_table8_2.checkdate=null

                  where t_table8_2.table8_id2=t1_TABLE8_ID2;
                  commit;
                  --通过记录的匹配查找日期、车号匹配的自动生成的记录(开始)
                  t_tempcount:=0;


 select count(a1.table8_id2) into t_tempcount
 from  t_table8_2 a1,t_table8_1 t1,
    (select t_table8_2.* from t_table8_2,t_table8_1 where t_table8_2.table8_id1=t_table8_1.table8_id1
    and  t_table8_1.tielu2=p_tielu1 and table8_id2=t1_TABLE8_ID2 and t_table8_1.inout='进口'
    and language='中方' and
   ( t_table8_1.inputtype='手工录入' or t_table8_1.inputtype='表格导入')
     ) a2
      where  a1.chehao=a2.chehao and a1.jieshouriqi=a2.jieshouriqi
                 and a1.jiaofuriqi=a2.jiaofuriqi and t1.language='中方'
                  and a1.table8_id2!=t1_TABLE8_ID2 and a1.table8_id1=t1.table8_id1 and t1.inputtype='自动生成'
                     and (a1.checkresult!='正确' or a1.checkresult is null);

 


                  if (t_tempcount=1) then
                    begin

                     select a1.table8_id2 ,a1.table8_id1 into t2_TABLE8_ID2,t2_TABLE8_ID1
                     from  t_table8_2 a1,t_table8_1 t1,
                        (select t_table8_2.* from t_table8_2,t_table8_1 where t_table8_2.table8_id1=t_table8_1.table8_id1
                        and  t_table8_1.tielu2=p_tielu1 and table8_id2=t1_TABLE8_ID2 and t_table8_1.inout='进口' and language='中方'
                        and
                       ( t_table8_1.inputtype='手工录入' or t_table8_1.inputtype='表格导入')) a2
                       where  a1.chehao=a2.chehao and a1.jieshouriqi=a2.jieshouriqi
                       and a1.jiaofuriqi=a2.jiaofuriqi and t1.language='中方'
                       and a1.table8_id2!=t1_TABLE8_ID2 and a1.table8_id1=t1.table8_id1 and t1.inputtype='自动生成'
                       and (a1.checkresult!='正确' or a1.checkresult is null);

 


                      update t_table8_2  set t_table8_2.RESULTID=p_RESULTID, t_table8_2.CHECKRESULTSTATE='日期车号匹配',
                      checkresultmemo='',IMGURL=null,t_table8_2.CHECKDATE=to_Date(to_Char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd'),t_table8_2.matchtable8_id1= t2_TABLE8_ID1,
                      t_table8_2.matchtable8_id2=t2_TABLE8_ID2
                      where t_table8_2.table8_id2=t1_TABLE8_ID2;
                      update t_table8_2 set t_table8_2.resultid=p_RESULTID,t_table8_2.matchtable8_id1= t1_TABLE8_ID1,
                      t_table8_2.matchtable8_id2=t1_TABLE8_ID2
                      where t_table8_2.table8_id2=t2_TABLE8_ID2;
                   commit;
                   end;
                   elsif  (t_tempcount=0) then
                     begin
                            update t_table8_2  set t_table8_2.RESULTID=p_RESULTID,t_table8_2.CHECKRESULTSTATE='日期车号不匹配',t_table8_2.checkresult='错误',IMGURL='../images/re_error.gif',t_table8_2.checkresultmemo='对方8号表中找不到接收日期和车号都匹配的记录。',
                            t_table8_2.CHECKDATE=to_Date(to_Char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd')
                            where t_table8_2.table8_id2=t1_TABLE8_ID2;
                            commit;
                            --获取车车日期匹配但国别不匹配的记录开始
                            select count(a1.table8_id2) into t_tempcount
                             from  t_table8_2 a1,t_table8_1 t1,
                                (select t_table8_2.* from t_table8_2,t_table8_1 where t_table8_2.table8_id1=t_table8_1.table8_id1
                               -- and  t_table8_1.tielu2=p_tielu1
                                and table8_id2=t1_TABLE8_ID2 and t_table8_1.inout='进口'
                                and language='中方' and
                               ( t_table8_1.inputtype='手工录入' or t_table8_1.inputtype='表格导入')
                                 ) a2
                                  where  a1.chehao=a2.chehao and a1.jieshouriqi=a2.jieshouriqi
                                             and a1.jiaofuriqi=a2.jiaofuriqi and t1.language='中方'
                                              and a1.table8_id2!=t1_TABLE8_ID2 and a1.table8_id1=t1.table8_id1 and t1.inputtype='自动生成'
                                                 and (a1.checkresult!='正确' or a1.checkresult is null);
                     if  (t_tempcount=1) then
                       begin
                           update t_table8_2  set t_table8_2.RESULTID=p_RESULTID, t_table8_2.CHECKRESULTSTATE='日期车号匹配但国别不匹配',
                      checkresultmemo='',IMGURL=null,t_table8_2.CHECKDATE=to_Date(to_Char(sysdate,'yyyy-mm-dd'),'yyyy-mm-dd'),t_table8_2.matchtable8_id1= t2_TABLE8_ID1,
                      t_table8_2.matchtable8_id2=t2_TABLE8_ID2
                      where t_table8_2.table8_id2=t1_TABLE8_ID2;
                      update t_table8_2 set t_table8_2.resultid=p_RESULTID,t_table8_2.matchtable8_id1= t1_TABLE8_ID1,
                      t_table8_2.matchtable8_id2=t1_TABLE8_ID2
                      where t_table8_2.table8_id2=t2_TABLE8_ID2;
                            commit;
                         end;

                         end if;
                            --获取车车日期匹配但国别不匹配的记录结束
                       end;
                       else
                         begin
                                 update t_table8_2  set t_table8_2.RESULTID=p_RESULTID,t_table8_2.CHECKRESULTSTATE='日期车号有多个匹配的记录',checkresultmemo='日期车号有多个匹配的记录',t_table8_2.checkresult='错误',IMGURL='../images/re_error.gif',t_table8_2.checkresultmemo='对方8号表中找不到接收日期和车号都匹配的记录。',t_table8_2.CHECKDATE=sysdate where t_table8_2.table8_id2=t1_TABLE8_ID2;
                            commit;

                           end;

                  end if;

                  --通过记录的匹配查找日期、车号匹配的另一国别的记录(结束)


    END LOOP;
    dbms_sql.close_cursor(CUR_1); --关闭游标。
--统计本次的审核结果的总数
   p_Table8CheckLog(p_userid,p_yyyy,p_mm,p_tielu1,p_RESULTID,'进口');
end p_Table8Check_in;

四、C#存储过程返回数据集

(1)存储过程

create or replace procedure p_test(
p_tielu2 in  varchar2,--如:哈铁
p_yyyy in number,
p_mm in number


 )
 as
 t_table11_id1 number;
 t_HuanLunFei number;
 begin
   select t_parameter.paravalue into t_HuanLunFei from t_parameter  where  t_parameter.paraname='换轮费';
delete T_TABLE11_2 where T_TABLE11_2.Tielu2=p_tielu2 and yyyy=p_yyyy and mm=p_mm;
commit;
select St_Table11_1.Nextval into t_table11_id1 from dual;--获取主表的ID号

declare
          type recordtype is record(chelianghaoma varchar2(100), haoma varchar2(100), jiaojiedanhaoma varchar2(100),
   feilv number(10,2),zhoushou number,  money1 number(10,2), money2  number(10,2),
   memo varchar2(400), yyyy number, mm number, tielu1 varchar2(100), tielu2 varchar2(100),
   jiaojiedanid2 number, jiaojiedanid1 number,language varchar2(100)

           );
           type rt_cur is ref cursor return t_Table11_2%rowtype;
           vrt_cur rt_cur;
           value_rt   recordtype ;
     begin
           open vrt_cur for  select a2.chehao,a1.bianhao,'',
                t_HuanLunFei,a2.zhoushu,a2.zhoushu*t_HuanLunFei,0,
                '',p_yyyy,p_mm,a2.tielumingcheng,a1.tielu1,a2.jiaojiedanid2,a2.jiaojiedanid1,a1.language
                from t_jiaojiedan2 a2,t_jiaojiedan1 a1

                where a1.jiaojiedanid1=a2.jiaojiedanid1
                and a1.to_dateyyyy=p_yyyy and a1.to_datemm=p_mm
                and a2.tielumingcheng=p_tielu2
                and a1.inout='进口'
                and a1.kz='准轨'
                and a2.zhongorkong='重'
                and a1.language='外方';
            loop
                   fetch vrt_cur into value_rt ;
                   exit when vrt_cur%notfound;
                   dbms_output.put_line('编号 '||value_rt.r_deptno||' 部门 '||value_rt.r_dname );
            end loop;
            close vrt_cur ;
      end;

 


end p_test;

(2)C#中的调用方法

public static DataTable  p_testGetDataSet(string p_tielu2, int p_yyyy, int p_mm,string p_inout)
        {
            OracleParameter[] parameters = new OracleParameter[]{ new OracleParameter( "p_tielu2",OracleType.Int32,9),
            new OracleParameter( "p_yyyy",OracleType.Int32,9),new OracleParameter( "p_mm",OracleType.Int32,9),
           new OracleParameter( "p_inout",OracleType.VarChar,49),new OracleParameter("V_CS", OracleType.Cursor)
          };
              parameters[0].Value = p_tielu2;
              parameters[1].Value = p_yyyy;
              parameters[2].Value = p_mm;
              parameters[3].Value = p_inout;

          parameters[4].Direction = ParameterDirection.Output;//设置为Output 
        

             DataTable dt   =  GJSH.DBUtility.DbHelperOra.RunProcedureGetDataSet("p_testGetDataSet", parameters).Tables[0];
            int tt=dt.Rows.Count;
            return dt;
        }

//RunProcedureGetDataSet方法如下:

/// <summary>
  /// 执行存储过程,返回数据集
  /// </summary>
  /// <param name="storedProcName">存储过程名</param>
  /// <param name="parameters">存储过程参数</param>
  /// <param name="tableName">DataSet结果中的表名</param>
  /// <returns>DataSet</returns>
        public static DataSet RunProcedureGetDataSet(string storedProcName, OracleParameter[] parameters)
  {
   using (OracleConnection connection = new OracleConnection(connectionString))
   {
    DataSet dataSet = new DataSet();
    connection.Open();
    OracleDataAdapter sqlDA = new OracleDataAdapter();
    sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters );
    sqlDA.Fill(dataSet, "dt");
    connection.Close();
    return dataSet;
   }
  }

BuildQueryCommand方法如下:

private static OracleCommand BuildIntCommand(OracleConnection connection,string storedProcName, IDataParameter[] parameters)
  {
   OracleCommand command = BuildQueryCommand(connection,storedProcName, parameters );
   command.Parameters.Add( new OracleParameter ( "ReturnValue",
                OracleType.Int32, 4, ParameterDirection.ReturnValue,
    false,0,0,string.Empty,DataRowVersion.Default,null ));
   return command;
  }

 

 

 

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告