3

まあ、私はまだオラクルに慣れていないので、サブクエリでテーブルをクエリしようとしています..次のようになります

select id_user, count(*) as jumlah from (select * from users where username = 'usr' and pass = 'pwd' and company_id = 'PAN' and status = 1) 
group by id_user;

上記のコードは機能します。しかし、ストアドプロシージャの中に入れようとすると、このようなエラーが発生しました

ここにストアドプロシージャがあります

create type login_obj is object(jumlah integer);
create type login_table is table of login_obj;
create or replace function startLogin(u varchar, p varchar, cid varchar)
return login_table
is
  tabel login_table := login_table();
  the_count integer;
  the_sql varchar(200);
begin
  the_sql := 'select id_user, count(*) as jumlah from (select * from users where username = ''' || u || ''' and pass = ''' || p || ''' and company_id = ''' || cid || ''' and status = 1) GROUP BY id_user';
  execute immediate the_sql into the_count;

  if the_count IS NOT NULL
  then
  begin
    tabel.extend;
    tabel(1) := login_obj(the_count);
  end;
  end if;
  return tabel;
end;

次に、それを実行します

select * from table (startLogin('usr','pwd','PAN'));

ここにエラーがあります

SQL Error: ORA-00932: inconsistent datatypes: expected - got -
ORA-06512: at "LUKI.STARTLOGIN", line 14
00932. 00000 -  "inconsistent datatypes: expected %s got %s"

何か案が?

4

3 に答える 3

3

行の下にもう 1 つの変数を追加します

the_sql varchar(200);

なので

yid_user users.id_user%TYPE;

すぐに実行を変更します

execute immediate the_sql into yid_user, the_count;

また、Oracle で変数型を使用するためのいくつかのヒント:

1. VARCHAR is obsolete, use VARCHAR2 instead.
2. Instead of using INTEGER type, use NUMBER.
于 2015-05-10T16:38:24.850 に答える
1

私はすでにそれを理解しました.. user4884704に感謝します(私はすでに彼の答えをマークしました)

ここに作業コードがあります..結果を別の変数に入れます

create type login_obj is object(id_user integer, jumlah integer);
create type login_table is table of login_obj;
create or replace function startLogin(u varchar, p varchar, cid varchar)
return login_table
is
  tabel login_table := login_table();
  id_user integer;
  the_count integer;
  the_sql varchar(200);
begin
  the_sql := 'select id_user, count(*) as jumlah from (select * from users where username = ''' || u || ''' and pass = ''' || p || ''' and company_id = ''' || cid || ''' and status = 1) GROUP BY id_user';
  execute immediate the_sql into id_user, the_count;

  if the_count IS NOT NULL
  then
  begin
    tabel.extend;
    tabel(1) := login_obj(id_user, the_count);
  end;
  end if;
  return tabel;
end;

それから私はそれを次のように実行します

select * from table (startLogin('usr','pwd','PAN')); 
于 2015-05-10T16:46:05.143 に答える
1

クエリは 2 つの列を返しますが、INTO で定義されている列は 1 つだけです。

于 2015-05-10T16:20:20.700 に答える