oracle - How to write pl/sql code with return type boolean for user authorization?
问题描述
I am trying to implement User Authentication with pl/sql function.
DECLARE
roleId RAW(16);
userRole User_Role%ROWTYPE;
BEGIN
select role_id
into roleId
from role
where role_name='guest';
BEGIN
select 1
into userRole
from user_role
where user_id = :APP_USER
and role_id = role_id;
return true;
exception when no_data_found
then return false;
END;
END;
With the above code I want to get 'role_id' of guest role and then try to find a record with that role id and app user in a 'user_role' junction table. But when I run this code the follow error occurs:
How can I solve this?
解决方案
You have defined a variable userrole
using the %rowtype
syntax. This means the variable is aligned with the projection of the USER_ROLE table. However, you are selecting a literal 1
instead of columns, so the projection of the query does not match the target variable.
Either change the target variable to be a simple number type, or change the query to select * from user_role
.
Incidentally, your WHERE clause is wrong: instead of role_id = role_id
you should have coded role_id = roleid
.
I think roleid
is a poor name for a local variable, because it is so similar to the column name that it's easy to make mistakes: the convention of using a prefix like l_
highlights local variables (or their absence).
推荐阅读
- admob - 来自应用程序的请求和展示次数之间的 Admob 差异
- grails - 无法在空对象上获取属性“mainContext”
- drupal-8 - Drupal 8“登录”表单不会出现
- netlogo - NeLogo:使用带有反向权重报告器的 rnd 扩展
- python - 在 TensorFlow 数据集管道中计算样本权重
- microcontroller - 当我写入内存映射寄存器时会发生什么?
- r - 根据条件删除多个列
- javascript - 如何将数据从 nodejs 发送到 js?
- html - 尝试使用 AngularJS 创建重复的水平 div 滚动
- text - 如何在 Vscode 中调整非文本编辑器字体大小