Apex - Create workspace user automatically when creating the user in the access control form - access-control

Hi I need to setup a new user in Apex you must first create them as a workspace user, then again as an app user in the access control for of that app.
I want to create a user in the form and have the apex user created in the background via PLSQL.
I found example code once that did it and it was great, can't find it now, anyone know how to do this?
I am sure I am not the first person to not want to set it up twice for each user :)

Looks like I may have found it, will test then confirm.
APEX_UTIL.CREATE_USER(
p_user_id NUMBER IN DEFAULT NULL
p_user_name VARCHAR2 IN
p_first_name VARCHAR2 IN DEFAULT NULL
p_last_name VARCHAR2 IN DEFAULT NULL
p_description VARCHAR2 IN DEFAULT NULL
p_email_address VARCHAR2 IN DEFAULT NULL
p_web_password VARCHAR2 IN
p_web_password_format VARCHAR2 IN DEFAULT NULL
p_group_ids VARCHAR2 IN DEFAULT NULL
p_attribute_01 VARCHAR2 IN DEFAULT NULL
p_attribute_02 VARCHAR2 IN DEFAULT NULL
p_attribute_03 VARCHAR2 IN DEFAULT NULL
p_attribute_04 VARCHAR2 IN DEFAULT NULL
p_attribute_05 VARCHAR2 IN DEFAULT NULL
p_attribute_06 VARCHAR2 IN DEFAULT NULL
p_attribute_07 VARCHAR2 IN DEFAULT NULL
p_attribute_08 VARCHAR2 IN DEFAULT NULL
p_attribute_09 VARCHAR2 IN DEFAULT NULL
p_attribute_10 VARCHAR2 IN DEFAULT NULL)
Credit to Oracle Doc

Related

PostgreSQL-Update last_update_date to current if modif at table

I'm trying to create a function with a trigger which when modifying a field from table tb_customer, the attribute last_update_date should be updated to the current_date of the modification.
If an user tries to enter a value in attribute last_update_date, an error should be raised with a message, and not allow this insert.
The table code for creation is:
CREATE TABLE erp.tb_customer (
cust_no CHARACTER(5) NOT NULL,
cust_name CHARACTER VARYING(50) NOT NULL,
cust_cif CHARACTER VARYING(150) NOT NULL,
last_updated_by CHARACTER VARYING(20) DEFAULT 'SYSTEM',
last_update_date DATE NOT NULL,
CONSTRAINT pk_customer PRIMARY KEY (cust_no)
);
So far, I have this code:
CREATE OR REPLACE FUNCTION modif_update_date_tracker()
RETURNS trigger AS $$
BEGIN
IF INSERT AT last_update_date THEN
RAISE EXCEPTION 'Not possible to update this field'
END IF;
NEW.last_update_date := current_time;
RETURN NEW;
END;
$$ LANGUAGE plpgsql
------------------------------------------------------------------------------------------------
-- Create trigger 1
------------------------------------------------------------------------------------------------
CREATE TRIGGER trigger_modif_update_date_tracker
BEFORE UPDATE
ON tb_customer
FOR EACH ROW
EXECUTE PROCEDURE modif_update_date_tracker();
BEGIN
IF OLD.last_update_date!=NEW.last_update_date THEN
RAISE EXCEPTION 'Not possible to update this field';
END IF;
NEW.last_update_date := CURRENT_TIMESTAMP;
RETURN NEW;
END;
When a user is not allowed to update a column, you should REVOKE him from doing so. You don't need a trigger for that.
testing code is something you can do yourself. But I can already tell you that UDAPTE is not a valid value for TG_OP.
A complete trigger example can be found in the manual.
It needs to be created as a FUNCTION, not as a PROCEDURE. There are other problems as well, but I don't know which ones are real and which ones you introduced while retyping it.

Average Rating Trigger ORACLE SQL

I am trying to create a trigger where is takes a several number of ratings from a table called FEEDBACK, and it creates an Average Rating on the MEMBER table. I want the trigger to update the average rating of each person whenever a new feedback is inserted.
----------- These are my tables -----------
create table Member_T(
MemberID Varchar2 (10) primary key,
MemberFirstName Varchar2 (20) NOT NULL,
MemberLastName Varchar2 (20) NOT NULL,
MemberMidleName Varchar2 (10),
MemberEmail Varchar2 (50) NOT NULL,
MemberPassword Varchar2 (20) NOT NULL,
MemberAdress Varchar2 (50) ,
MemberCity Varchar2 (20) ,
MemberState char (2) ,
MemberCountry Varchar2 (20) ,
MemberZipCode number (5,0),
MemberPhone Varchar2 (12) ,
MemberAverageRating number (3,1) check (MemberAverageRating >= 0.0 AND MemberAverageRating <= 5.0));
create table Feedback_T(
FeedbackID Varchar2 (10) primary key,
FeedbackMemberGiverID Varchar2 (10) references Member_T(MemberID),
MemberReceiverID Varchar2 (10) references Member_T(MemberID),
MemberRating number (3,1) check (MemberRating >= 0.0 AND MemberRating <= 5.0),
MemberComment Varchar2 (500),
MemberFeedbackDate Date Default(sysdate));
----------- This is my trigger -----------
create or replace trigger updateRating
after insert
on Feedback_T
for each row
Declare
rating Feedback_T.MemberRating%type;
receiver Feedback_T.MEMBERRECEIVERID%type;
averageRating Member_T.MemberAverageRating%type;
begin
select AVG(MemberRating), count(MEMBERRECEIVERID)
into rating, receiver
from Feedback_T
where Feedback_T.FEEDBACKID = :new.FEEDBACKID;
update Member_T
set averageRating = rating / receiver
where Member_T.MemberID = :new.MemberID;
end;
----------- I get this error -----------
TRIGGER UPDATERATING compiled
Errors: check compiler log
If you run "show errors" from the sql prompt, you will see your errors, or compile this in SQLDeveloper, which will show you the errors. Your problem is:
where Member_T.MemberID = :new.MemberID;
Where there is no bind variable called :new.MemberID, since MemberID is not a column in the Feedback_T table that the trigger is for. Maybe you meant to reference FeedbackMemberGiverID or MemberReceiverID?

Setting the default value to the current user in Oracle

I am trying to create a new table in Oracle 11g where the default value for a column is the currently logged in user. I need to do this is for logging purposes.
CREATE TABLE tracking (
pk NUMBER(19,0) PRIMARY KEY,
description VARCHAR2(50),
created_by VARCHAR2(128) DEFAULT CURRENT_USER
);
How can I write the DEFAULT CURRENT_USER section so it will take the current Oracle user as the default value? I know I could use a trigger, but I shouldn't have to...
You need to use USER not CURRENT_USER:
CREATE TABLE tracking (
pk NUMBER(19,0) PRIMARY KEY,
description VARCHAR2(50),
created_by VARCHAR2(128) DEFAULT USER
);
SQL Fiddle
The maximum length of a user is 30, so you could reduce this and I would increase the size of your DESCRIPTION column unless you're very sure that everything will come in at less than 51 characters.
Try user instead:
CREATE TABLE tracking (
pk NUMBER(19,0) PRIMARY KEY,
description VARCHAR2(50),
created_by VARCHAR2(128) DEFAULT USER
);
By the way, I also think created_at for the datetime is another useful default column.

Confusing error about missing left parenthesis in SQL statement

SQLPLUS says I have missing left parenthesis with this statement in my sql script..
CREATE TABLE people(
id INT NOT NULL PRIMARY KEY,
name VARCHAR2
);
I had uploaded my script with sftp, could that have played around with the script?
VARCHAR2 is a type that needs a maximum size/length. Try something like...
varchar2(50)
Your missing left parenthesis is the parenthesis that surrounds the size.
CREATE TABLE people(
id INT NOT NULL PRIMARY KEY,
name VARCHAR2(50)
);
You need to specify a size for the VARCHAR2 data type.
E.g. VARCHAR2(30)
SQL*Plus is looking for the brackets around the VARCHAR2 size definition.
You are getting this error because you didn't specify the character with datatype varchar2.
Try something like this:
CREATE TABLE people(
id INT NOT NULL PRIMARY KEY,
name VARCHAR2(20) );
You need to specify the size of Varchar2.
For example:- Name Varchar2(50)
Note:- The maximum size of the Varchar2 is 4000.

auto_increment with varchar in oracle

i have this table
CREATE TABLE WishList(
idWishList VARCHAR2(40 BYTE) ,
WishListName VARCHAR2(40 CHAR) NOT NULL,
id_User VARCHAR2(40 BYTE) NOT NULL
)
now how can i use auto_increment with varchar in oracle ??
You can add a trigger:
create or replace trigger some_trig_name
before insert on WishList
for each row
begin
:new.idWishList := to_char(your_sequence.nextval);
end some_trig_name;
In the example I used a seq but you can put whatever you want
As I remember, Oracle doesn't have an auto_increment functionality. It has sequences and developers should add special function like getNextId() and use it in insert statements like
insert into table (id,...) values(getNextId() ,..)
So, you can implement you own function which returns new id for your field with your own algorithm.