user1279780
user1279780

Reputation: 99

Creating a VIEW that RESTRICTS user access to certain hours

I am trying to perform the following query "Create a view which restricts staff user access to the order table to the working hours between 9:00 and 18:00. Grant appropriate permissions to the staff users."

However I am struggling, what I have used so far is the following:

   CREATE VIEW STAFF_RESTRICTORDERVIEW AS
   SELECT TO_CHAR (SYSDATE, 'DD-MM-YYYY HH24:MI:SS'), ORDERNUMBER, CUSTOMERNUMBER, DATEORDERPLACED, DELIVERYINSTRUCTION, SCHEDULEID, 
   FROM ORDERS
   WHERE STAFF_RESTRICTORDERVIEW (SYSDATE, 'DD-MM-YYYY HH24:MI:SS') NOT BETWEEN
   (SYSDATE, 'DD-MM-YYYY 09:00:00') AND (SYSDATE, 'DD-MM-YYYY 18:00:00')

If that code above worked, then I would have been able to add the following:

  GRANT SELECT on STAFF_RESTRICTORDERVIEW TO 'STAFF_USER'

Any help would be appreciated, thanks in advance :).

Upvotes: 1

Views: 230

Answers (1)

Dan A.
Dan A.

Reputation: 2924

Try this:

CREATE VIEW STAFF_RESTRICTORDERVIEW (
    current_sys_date, 
    ordernumber, 
    customernumber, 
    dateorderplaced,
    deliveryinstruction, 
    scheduleid
) AS
SELECT 
    sysdate, 
    ORDERNUMBER, 
    CUSTOMERNUMBER, 
    DATEORDERPLACED, 
    DELIVERYINSTRUCTION, 
    SCHEDULEID
 FROM ORDERS
 WHERE SYSDATE NOT BETWEEN trunc(sysdate) + 9/24 and trunc(sysdate) + 18/24

I believe that this would return an empty recordset if you run it between 9:00 and 18:00. If you want the reverse, to only get records between those hours, then remove the NOT from the final line.

Upvotes: 3

Related Questions