加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

声明一个变量并在Oracle中从SELECT查询中设置其值

发布时间:2020-12-12 14:04:49 所属栏目:百科 来源:网络整理
导读:在SQL Server中,我们可以使用: DECLARE @variable INT;SELECT @variable= mycolumn from myTable; 在Oracle中如何做同样的操作?我正在执行以下操作: DECLARE COMPID VARCHAR2(20);SELECT companyid INTO COMPID from app where appid='90' and rownum=1;
在SQL Server中,我们可以使用:
DECLARE @variable INT;
SELECT @variable= mycolumn from myTable;

在Oracle中如何做同样的操作?我正在执行以下操作:

DECLARE COMPID VARCHAR2(20);
SELECT companyid INTO COMPID from app where appid='90' and rownum=1;

为什么这不工作?

SELECT INTO
DECLARE
   the_variable NUMBER;

BEGIN
   SELECT my_column INTO the_variable FROM my_table;
END;

确保查询只返回一行:

By default,a SELECT INTO statement must return only one row. Otherwise,PL/SQL raises the predefined exception TOO_MANY_ROWS and the values of the variables in the INTO clause are undefined. Make sure your WHERE clause is specific enough to only match one row

If no rows are returned,PL/SQL raises NO_DATA_FOUND. You can guard against this exception by selecting the result of an aggregate function,such as COUNT(*) or AVG(),where practical. These functions are guaranteed to return a single value,even if no rows match the condition.

A SELECT … BULK COLLECT INTO statement can return multiple rows. You must set up collection variables to hold the results. You can declare associative arrays or nested tables that grow as needed to hold the entire result set.

The implicit cursor SQL and its attributes %NOTFOUND,%FOUND,%ROWCOUNT,and %ISOPEN provide information about the execution of a SELECT INTO statement.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读