0

我写了下面的脚本,我在第 8 行得到 ORA-01722 错误。我该如何解决这个错误?提前致谢。

declare 
  type array_t is varray(2) of VARCHAR2(20);
  array array_t := array_t('5634', '5764');

 begin

  for i in 1..array.count loop   
      execute immediate 'select t.musteri_id from tms.isemri t where t.isemri_tarihi  = (select max(t.isemri_tarihi) from tms.isemri t where t.hizmet_no = '|| 
      array(i)||') and t.hizmet_no ='|| array(i);   

  end loop;
  end;
4

2 回答 2

1

您的连接 SQL 将如下所示:

select t.musteri_id from tms.isemri t where t.isemri_tarihi =
(select max(t.isemri_tarihi) from tms.isemri t where t.hizmet_no = 5634)
and t.hizmet_no = 5634

请注意,值 5634 是一个数字而不是字符串,这会导致 Oracle 将 hizmet_no 隐式转换为一个数字,这并不总是有效。

您应该使用绑定变量,这将解决此问题:

declare 
  type array_t is varray(2) of VARCHAR2(20);
  array array_t := array_t('5634', '5764');
  l_musteri_id tms.isemri.musteri_id%type
 begin

  for i in 1..array.count loop   
      execute immediate 'select t.musteri_id from tms.isemri t 
                         where t.isemri_tarihi  = (select max(t.isemri_tarihi)
                                                   from tms.isemri t
                                                   where t.hizmet_no = :val1)
                         and t.hizmet_no = :val2'
       into l_musteri_id
       using array(i), array(i);   

  end loop;
  end;
于 2012-03-20T14:52:55.797 回答
0

立即执行'select t.musteri_id from tms.isemri t where t.isemri_tarihi = (select max(t.isemri_tarihi) from tms.isemri t where t.hizmet_no = '''|| array(i)||''')和 t.hizmet_no ='''|| 数组(i)||'''';

于 2012-03-20T15:11:52.427 回答