如何使用txt文件输入自动化SQL查询?

时间:2016-03-16 17:10:18

标签: sql oracle

我正在尝试创建一个小脚本,以便自动删除表中的某些行。基本上我想要一个脚本从包含要删除的行的txt文件中读取信息,然后它们会自动添加到查询中。例如,文本文件将有两列,

0900209N    0910609S
0900210N    0910610S

然后将这些内容添加到以下代码and FLD_LOCATION_LABEL >= '0900210N' and FLD_LOCATION_LABEL <= '0910610S'

    select count(*),test_storage,TEST_PRIMARY
    from TEST_LOCATION
    where fld_location_num in (select fld_location_num
                       from test_location_coordinates                                         
                       where FLD_LOCATION_LABEL >= '0900209N' and FLD_LOCATION_LABEL <= '0910609S'
                          and FLD_LOCATION_LABEL >= '0900210N' and FLD_LOCATION_LABEL <= '0910610S')
    group by fld_storage_class_code,FLD_PRIMARY_STRG_CLASS_IND
    order by fld_storage_class_code;

    delete

因此,结果代码取决于文本文件中的行数。

1 个答案:

答案 0 :(得分:0)

希望这适合你

DECLARE
  fhandle UTL_FILE.FILE_TYPE;
  line    VARCHAR2(80);
  l_start varchar2(20);
  l_end varchar2(20);
  l_length number;
  l_cnt number;
  l_test_storage varchar2(20);
  l_test_primary varchar2(20);
BEGIN
  fhandle := UTL_FILE.FOPEN('TMP', 'myoutput', 'R');
  LOOP
    UTL_FILE.GET_LINE(fhandle, line);
    l_start := substr(line, 1, instr(line,' ')-1);
    l_end := substr(line,instr(line,' ')+1);
    select count(*),test_storage,TEST_PRIMARY into l_cnt,l_test_storage,l_test_primary
    from TEST_LOCATION
    where fld_location_num in (select fld_location_num
                       from test_location_coordinates                                         
                       where FLD_LOCATION_LABEL between l_start and l_end
                          )
    group by fld_storage_class_code,FLD_PRIMARY_STRG_CLASS_IND
    order by fld_storage_class_code;
    --delete statements if required  
  END LOOP;
EXCEPTION
  WHEN others THEN
     dbms_output.put_line('Exception occured');
END;