Oracle 00932. 00000 - “不一致的数据类型:预期%s获得%s”

时间:2015-05-10 16:15:25

标签: sql oracle stored-procedures ora-06512

我在oracle中仍然是新手,我正在尝试使用子查询查询表..它看起来像这样

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"

任何想法?

3 个答案:

答案 0 :(得分:3)

在行

下添加一个变量
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.

答案 1 :(得分:1)

您的查询返回2列,但INTO中只定义了一列。

答案 2 :(得分:0)

我已经想通了..感谢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'));