我想实现一个只返回登录用户的查询,并且只显示记录,我按照以下步骤操作:
SELECT * FROM EMPLOYEE
WHERE UPPER(username) = v('APP_USER')
但是,我有另一个名为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;
/
但它在APEX Oracle PLSQL中不起作用。
有什么建议吗?
答案 0 :(得分:7)
根据我的理解,你需要尝试这个:
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;
/
输出存储在嵌套表变量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')
选择最适合您的人。
答案 1 :(得分:2)
您希望来自UPPER(username)
v('APP_USER')
或User_Type
的用户的所有记录为1吗?然后使用OR
:
SELECT * FROM Employee WHERE upper(username) = v('APP_USER') OR User_Type = 1
如果这不是您的意思,那么您能更清楚地解释一下吗?
答案 2 :(得分:1)
尝试:
select distinct e2.*
from employee e1
join employee e2 on (e1.username = e2.username or e1.User_Type = 1)
where UPPER(e1.username) = v('APP_USER')