Ibr*_*fiq 1 sql oracle plsql if-statement oracle-apex
我想实现一个只返回登录用户并仅显示记录的查询,我按照以下方式进行了操作:
SELECT * FROM EMPLOYEE
WHERE UPPER(username) = v('APP_USER')
Run Code Online (Sandbox Code Playgroud)
但是,我有另一个名为User_Type的列,用户可以是类型1,2或3.如果我的用户类型为1,我希望查询也返回所有表记录,因为用户类型1是管理员.
我想这样做:
BEGIN
SELECT * FROM Employee
WHERE upper(username) = v('APP_USER')
IF User_Type = 1
THEN SELECT * FROM Employee
END IF;
END;
/
Run Code Online (Sandbox Code Playgroud)
但它在APEX Oracle PLSQL中不起作用.
有什么建议?
根据我的理解,你需要尝试这个:
DECLARE
emp employee%ROWTYPE; -- Create a record type
tbl_emp IS TABLE OF emp;
-- ^^^ Create a table of that record type
v_user_type employee.user_type%TYPE;
-- ^^^ Variable to store user type
BEGIN
SELECT user_type
INTO v_user_type
FROM Employee
WHERE upper(username) = v('APP_USER');
IF v_user_type = 1 THEN
SELECT *
BULK COLLECT INTO tbl_emp
FROM employee;
-- ^^ Returns the entire table
ELSE
SELECT *
BULK COLLECT INTO tbl_emp
FROM employee;
WHERE upper(username) = v('APP_USER');
-- ^^ Returns the row related to the user.
END IF;
END;
/
Run Code Online (Sandbox Code Playgroud)
输出存储在嵌套表变量中tbl_emp.
编辑:
它也可以使用纯SQL实现,如下所示:
SELECT *
FROM employee e
WHERE EXISTS (SELECT 1
FROM employees e_in
WHERE e_in.user_type = 1
AND UPPER(e_in.username) = v('APP_USER'))
OR UPPER(e.username) = v('APP_USER')
Run Code Online (Sandbox Code Playgroud)
选择最适合您的方式.
| 归档时间: |
|
| 查看次数: |
12779 次 |
| 最近记录: |