如果select返回某些内容,则将boolean设置为true

时间:2014-08-07 10:47:00

标签: oracle select boolean

如果boolean查询返回某些内容,如何将true设置为select

就像那样:

declare
    isExist boolean;

begin
    select count(1) into isExist from MYTABLE where ID = 1;
end;

2 个答案:

答案 0 :(得分:2)

Oracle的SQL(仍然)不支持布尔数据类型,只有PL / SQL才能这样做 - 但PL / SQL中嵌入的SQL语句必须符合Oracle的规则SQL;这有时会引起一些混乱。

当然,有许多可行的解决方法,例如:

declare
    n number;
    isExist boolean;    
begin
    select count(1) into n from MYTABLE where ID = 1;

    isExist := (n>0);
end;

答案 1 :(得分:2)

有很多方法可以做到:

DECLARE
  l_temp NUMBER(1);
  l_exists boolean;
BEGIN
  SELECT count(*)
    INTO l_temp
    FROM dual
   WHERE EXISTS (SELECT NULL FROM YOUR_TABLE WHERE id = 1)
  ;
  -- l_temp is 1 if there are something in your table and 0 if there is nothing in your table.
  l_exists := l_temp = 1;
END;

您也可以尝试使用光标:

DECLARE
  l_cursor SYS_REFCURSOR;
  l_temp YOUR_TABLE.id%TYPE;
  l_exists boolean;
BEGIN
  open l_cursor for SELECT id FROM YOUR_TABLE WHERE id = 1;
  fetch l_cursor into l_temp;
  l_exists := l_cursor%FOUND;
  close l_cursor;
END;