在Oracle Sql Developer中的视图的WHERE子句中使用USER

时间:2018-05-13 08:59:45

标签: sql oracle parameters sql-view

我无法找到与此问题相关的任何信息,因为它非常具体。情况是我创建了一个视图,该视图应该根据调用它的用户返回结果。视图如下:

    CREATE VIEW V_ClientReward AS
    SELECT cl.id, EXTRACT(YEAR FROM r1.recommendationDate) AS 
    yearRecommended,

    COALESCE((SELECT COUNT(*) 
    FROM Recommendation r2 
    WHERE r2.client1 = cl.id AND 
    EXTRACT(YEAR FROM r2.recommendationDate)= EXTRACT(YEAR FROM 
    r1.recommendationDate)
    AND r2.client2 IN(SELECT id FROM Client WHERE registrationDate IS NOT 
    NULL) GROUP BY r2.client1), 0) AS numberRecommendedContracted,

    COALESCE((SELECT COUNT(*) 
    FROM Recommendation r2 
    WHERE r2.client1 = cl.id AND 
    EXTRACT(YEAR FROM r2.recommendationDate)= EXTRACT(YEAR FROM 
    r1.recommendationDate)
    AND r2.client2 NOT IN(SELECT id FROM Client WHERE registrationDate IS 
    NOT NULL) GROUP BY r2.client1), 0) AS numberRecommendedNotContracted,

    ((SELECT rewardImport FROM ClientReward WHERE rewardYear = EXTRACT(YEAR 
    FROM r1.recommendationDate))*COALESCE((SELECT COUNT(*) 
    FROM Recommendation r2 
    WHERE r2.client1 = cl.id AND 
    EXTRACT(YEAR FROM r2.recommendationDate)= EXTRACT(YEAR FROM 
    r1.recommendationDate)
    AND r2.client2 IN(SELECT id FROM Client WHERE registrationDate IS NOT 
    NULL)
    GROUP BY r2.client1), 0)) AS totalEarned 

    FROM Client cl INNER JOIN Recommendation r1
    ON cl.id=r1.client1
    INNER JOIN WebClient
    ON idUser = cl.id
    WHERE nick = USER
    GROUP BY cl.id, EXTRACT(YEAR FROM r1.recommendationDate);

我的想法是,在我为此视图授予用户SELECT之后,通过SELECT * FROM P18.V_ClientReward调用它的简单操作应该根据该用户的名称获得结果。 不幸的是,当我执行它时,根本没有结果,只有空字段。我已经尝试执行查询,直接指定有问题的用户为WHERE nick = 'user_name',并且它运行正常。

任何帮助都将非常感谢。

感谢。

1 个答案:

答案 0 :(得分:1)

我没有你的桌子,所以我使用了自己的桌子。

首先,表格内容:列NICK用于区分数据库用户(scott& hr)。该表属于用户SCOTT。

SQL> connect scott/tiger@xe
Connected.

SQL> select id_emp, fname, nick
  2  from person;

    ID_EMP FNAME                NICK
---------- -------------------- --------------------
         1 Little               HR
         2 Charles              HR
         3 Rio                  HR
         4 Seb                  SCOTT
         5 Romain               SCOTT

使用您使用的where子句创建视图:

SQL> create or replace view v_person as
  2    select id_emp, fname
  3    from person
  4    where nick = user;

View created.

向用户HR授予权限(否则,它不会看到任何内容):

SQL> grant select on v_person to hr;

Grant succeeded.

好的,让我们检查这些用户看到的内容。

SQL> show user
USER is "SCOTT"
SQL> select * from v_person;

    ID_EMP FNAME
---------- --------------------
         4 Seb
         5 Romain

SQL> connect hr/hr@xe
Connected.

SQL> -- including the owner's name:
SQL> select * from scott.v_person;

    ID_EMP FNAME
---------- --------------------
         1 Little
         2 Charles
         3 Rio

SQL>

似乎没事,不是吗?

截至您的问题:您是否有机会使用小写 nick 值?

SQL> update person set nick = 'scott' where id_emp > 3;

2 rows updated.

SQL> select * From v_person;

no rows selected

SQL>

如果没有,你能否创建一个相同的简单例子,清楚地显示你做了什么?