1

以下の列を持つテーブル attribute_config があります。

table_name column_name キー

2行の下にあるとしましょう

アカウント accountphone accountnum

顧客 顧客番号 顧客ID

キーは、accountnum または customerid のみにすることができます。

(i_accountnum,i_customerid) を受け入れるコードを作成する必要があります。

where条件のキーを使用して、table_nameで言及されたテーブルのcolumn_nameで言及された列からそれぞれの値をフェッチします。

例: accountnum = i_accountnum のアカウントから accountphone を選択し、customerid = i_customerid の顧客から customernumber を選択します。

完全なクエリは動的に形成する必要があります。クエリで i_accountnum または i_customerid を渡すかどうかも動的に決定する必要があります。key - accountnum の場合、i_accountnum が where 条件に渡されます。

私はこれまでこれらの行を試してきましたが、これは機能していません。間違っていることはわかっています。

declare
v_accountnum varchar2(20);
v_customerid varchar2(20);
v_attribute_value varchar2(20);
v_stmt varchar2(255);
begin
Account_Num := 'TestCustomer';  -- input to the function
v_customer_ref := 'TestAccount'; -- input to the function
for i in (Select * from attribute_config) loop
v_stmt := 'select ' || i.column_name || ' from ' ||  i.table_name ||' where ' || i.key|| ' = v_' || i.key;
execute immediate v_Stmt into v_attribute_value;
end loop;
end;
4

1 に答える 1

0

これでコードは修正されますが、コードが2つのパラメーター(i_accountnum、i_customerid)を受け入れる必要がある場合、動的クエリを使用する利点はありません。これはすでに静的な状況であり、おそらく学習目的でのみ関連する値をフェッチします。

declare
   procedure fecth_values(i_accountnum account.accountnum%type,
                          i_customerid customer.customerid%type) return varchar2 is
      v_attribute_value varchar2(20);
   begin
      for i in (select * from attribute_config) loop
         execute immediate 'select ' || i.column_name || ' from ' ||
                           i.table_name || ' where ' || i.key || ' = ' || case when i.key = 'accountnum' then i_accountnum when i.key = 'customerid' then i_customerid end;
         into v_attribute_value;
         dbms_output.put_line(v_attribute_value);
      end loop;
      return null;
   end;
begin
   fecth_values(1, 1);
end;

where句が間違っていたので、stmtを実行したときに宣言されていi.keyない ではなく、入力された値と比較する必要があります。'v_' || i.key

于 2014-11-27T12:50:26.877 に答える