计算用户名可以查看的文档数

时间:2016-02-06 21:22:54

标签: sql oracle plsql

我创建了一个名为UserPermissions的表。

CREATE TABLE UserPermissions
(
UserName varchar(255) NOT NULL PRIMARY KEY,
Document varchar(255) NOT NULL,
)

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Policy','SYSTEM');

INSERT INTO UserPermissions (Document)
VALUES ('Menu','JDOW');

INSERT INTO UserPermissions (Document)
VALUES ('W2','USAM');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Permissions','SYSTEM');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('W2','JDOW');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Form 1040','USAM');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Policy','JDOW');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('W2','SYSTEM');

然后,我必须编写一个PL / SQL存储函数,该函数将用户名作为输入,并返回用户有权查看的文档数。如果表中没有用户名,则您的过程应返回“未找到用户”。这是我到目前为止所做的,它确实可以编译。

 CREATE OR REPLACE
  FUNCTION user_documents_func(UserName VARCHAR2)
  RETURN int 
  AS
  num_views int;
  BEGIN
  SELECT count(*) INTO num_views from UserPermissions WHERE UserName LIKE                             

  'SYSTEM' || 'JDOW' || 'USAM';  
  IF num_views > 0 THEN
  RETURN UserName || ' can view ' || num_views || ' document(s).';
  ELSE
  dbms_output.put_line('Username not found!');
  END IF;
  END;
  /
  SET SERVEROUTPUT ON;

然后我运行了以下select语句

 SELECT user_documents_func('SYSTEM') FROM dual
 UNION
 SELECT user_documents_func('JDOW') FROM dual
 UNION
 SELECT user_documents_func('USAM') FROM dual;

我收到以下错误:

  

ORA-06503:PL / SQL:返回的函数没有值ORA-06512:at   " SYSTEM.USER_DOCUMENTS_FUNC",第12行   06503. 00000 - " PL / SQL:函数返回没有值"   *原因:对PL / SQL函数的调用已完成,但没有RETURN语句              执行。   *动作:重写PL / SQL函数,确保它始终返回              适当类型的值。

感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

戈登是对的。你需要重写你的功能。以下应该可以正常工作。它将生成可以读取的文档数量并输出消息。我还修复了DDL / DML的语法。

创建表格:

CREATE TABLE UserPermissions(
    UserName varchar2(255) NOT NULL,
    Document varchar2(255) NOT NULL,
CONSTRAINT pk_UserPermissions PRIMARY KEY (UserName, Document));

插入记录:

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Policy','SYSTEM');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Menu','JDOW');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('W2','USAM');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Permissions','SYSTEM');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('W2','JDOW');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Form 1040','USAM');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('Policy','JDOW');

INSERT INTO UserPermissions (Document, UserName)
VALUES ('W2','SYSTEM');

创建功能:

CREATE OR REPLACE
  FUNCTION user_documents_func(uname VARCHAR2)
  RETURN int 
  AS
  num_views int;
BEGIN
  SELECT count(*) INTO num_views from UserPermissions WHERE UserName = uname;

IF num_views > 0 THEN
    dbms_output.put_line(uname || ' can view ' || num_views || ' document(s).');
ELSE
      dbms_output.put_line(uname || ' not found!');
END IF;

RETURN num_views;

END;

运行以下sql语句:

SELECT 'SYSTEM',  user_documents_func('SYSTEM') as CNT FROM dual
UNION 
SELECT 'JDOW', user_documents_func('JDOW') FROM dual
UNION 
SELECT 'USAM', user_documents_func('USAM') FROM dual
UNION
SELECT 'RANDOMUSER', user_documents_func('RANDOMUSER') FROM dual;

输出:

结果:

UserName   | Count
+++++++++++|+++++++
JDOW       |  3 
RANDOMUSER |  0 
SYSTEM     |  3 
USAM       |  2

DBMS输出:

SYSTEM can view 3 document(s).
JDOW  can view 3 document(s).
USAM  can view 2 document(s).
RANDOMUSER not found!