Apex: How to find out the schema name? - oracle-apex

I need to find my schema name because i want to delete triggers which i created.
For example the following:
CREATE OR REPLACE TRIGGER TRIGGER_ORDER
BEFORE INSERT ON HOUSE_ORDER
REFERENCING OLD AS OLD NEW AS NEW
FOR EACH ROW
WHEN (NEW.ORDER_ID IS NULL)
BEGIN
SELECT SEQ_ORDER_ID.NEXTVAL
INTO :NEW.ORDER_ID FROM DUAL;
END;
/
When i now try to drop the trigger:
DROP TRIGGER TRIGGER_ORDER
I get the following error:
ORA-04080: trigger 'TRIGGER_ORDER' does not exist
I found out that i need to call something like
DROP TRIGGER SCHEMA_NAME.TRIGGER_ORDER
but i have no idea what my schema name is. so how can i find it?

You should use the ALL_TRIGGERS view. There's a column named Table Owner which indicates the schema.
select * from all_triggers
where table_name = 'YOUR_TABLE'

Related

Rename Column Name in Athena AWS

I have tried several ways to rename some column name in athena table.
after reading the following article
https://docs.aws.amazon.com/athena/latest/ug/alter-table-replace-columns.html
But I have get a no luck on it.
I tried
ALTER TABLE "users_data"."values_portions" REPLACE COLUMNS ('username/teradata' 'String', 'username_teradata' 'String')
Got error
no viable alternative at input 'alter table "users_data"."values_portions" replace' (service: amazonathena; status code: 400; error code: invalidrequestexception; request id: 23232ssdds.....; proxy: null)
You can refer to this document which talks about renaming columns. The query that you are trying to run will replace all the columns in the existing table with provided column list.
One strategy for renaming columns is to create a new table based on the same underlying data, but using new column names. The example mentioned in the link creates a new orders_parquet table called orders_parquet_column_renamed. The example changes the column o_totalprice name to o_total_price and then runs a query in Athena.
Another way of changing the column name is by simply going to AWS Glue -> Select database -> select table -> edit schema -> double click on column name -> type in new name -> save.

cx_oracle insert with select query not working

I am trying to insert in database(Oracle) in python with cx_oracle. I need to select from table and insert into another table.
insert_select_string = "INSERT INTO wf_measure_details(PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS) \
select PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS from wf_measure_details_stag where oozie_job_id = '{0}'.format(self.DAG_id)"
conn.executemany(insert_select_string)
conn.commit()
insert_count = conn.rowcount
But I am getting below error. I do not have select parameter of data as data is getting from select query.
Required argument 'parameters' (pos 2) not found
Please suggest how to solve this
As mentioned by Chris in the comments to your question, you want to use cursor.execute() instead of cursor.executemany(). You also want to use bind variables instead of interpolated parameters in order to improve performance and reduce security risks. Take a look at the documentation. In your case you would want something like this (untested):
cursor.execute("""
INSERT INTO wf_measure_details(PARENT_JOB_ID, STAGE_JOB_ID,
MEASURE_VALS, STEP_LEVEL, OOZIE_JOB_ID, CREATE_TIME_TS)
select PARENT_JOB_ID, STAGE_JOB_ID, MEASURE_VALS, STEP_LEVEL,
OOZIE_JOB_ID, CREATE_TIME_TS
from wf_measure_details_stag
where oozie_job_id = :id""",
id=self.DAG_id)

referencing new Table as new_table not visible to the trigger function Postgresql

I am new in Postgresql so this question may be dumb for you guys!
I am tring to use the referenced tables in a trigger function, but some how the function doesn't seem to have access on the referenced alias (new_table) :
CREATE FUNCTION Function_Name()
RETURNS TRIGGER AS
$$
BEGIN
SELECT tbl.some_field INTO new_table.other_field
from some_table as tbl;
return null;
END;
$$ LANGUAGE PLPGSQL;
I am having this error:
"new_table.other_field" is not a known variable
and here is the trigger code:
CREATE TRIGGER Trigger_Name
AFTER INSERT
ON Table_Name
REFERENCING NEW TABLE AS new_table
FOR EACH ROW
EXECUTE FUNCTION Function_Name();
The function code should be executed first and then the trigger's, so how could the function access the alias that is referenced later in the trigger defenition??
and how to access the referenced table aliases in the function?
Note: In my example I am tring to use the alias, so when I use NEW inplace of new_table the function is created successfully!
The problem was that i am tring to set data in the NEW table using the alias name, however to do that i should use the original name NEW and not the referenced alias new_table..
The referenced alias new_table could be used to only get data from, like in FROM CLAUSE, joins and WHERE CLAUSE where one doesn't change the data in the referenced table.
Update:
here is an example of what i did to test that:
create table test_table2(
id int primary key,
name varchar(255)
)
create table test_table(
id int primary key,
name varchar(255)
)
create or replace function F_test_table()
returns trigger as $$
begin
insert into test_table2(id, name)
select id, name from new_table;
return null;
end;
$$ LANGUAGE PLPGSQL;
drop trigger if exists tr_test_table ON test_table;
create trigger tr_test_table
AFTER INSERT
ON test_table
REFERENCING NEW TABLE AS new_table
for each row ------- row level trigger ---------
EXECUTE FUNCTION F_test_table();
insert into test_table
values(1, '11111')
select * from test_table2
notice that the trigger inserts the data into test_table2

How to update redshift column: simple text replacement

I have a large target table with columns (id, value). I want to update value='old' to value='new'.
The simplest way would be to UPDATE target SET value='new' WHERE value='old';
However, this deletes and creates new rows and is not recommended, possibly. So I tried to do a merge column update:
# staging
CREATE TABLE stage (LIKE target INCLUDING DEFAULTS);
INSERT INTO stage (SELECT id, value FROM target WHERE value=`old`);
UPDATE stage SET value='new' WHERE value='old'; # ??? how do you update value?
# merge
begin transaction;
UPDATE target
SET value = stage.value FROM stage
WHERE target.id = stage.id and target.distkey = stage.distkey; # collocated join?
end transaction;
DROP TABLE stage;
This can't be the best way of creating the table stage: I have to do all these UPDATE delete/writes when I update this way. Is there a way to do it in the INSERT?
Is it necessary to force the collocated join when I use CREATE TABLE LIKE?
Are you updating all the rows in the table?
If yes you can use CTAS (create table as) which is recommended method
Assuming you table looks like this
table1
id, col1,col2, value
You can use the following SQL to create a new table
CREATE TABLE tmp_table AS
SELECT id, col1,col2, 'new_value'
FROM table1;
After you verify data in tmp_table
DROP TABLE table1;
ALTER TABLE tmp_table RENAME TO table1;
If you are not updating all the rows you can use a filter to do a CTAS and insert the rest of the rows to the new table, let me know if you need more info if this is the case
CREATE TABLE tmp_table AS
SELECT id, col1,col2, 'new_value'
FROM table1
WHERE value = 'old'
INSERT INTO tmp_table SELECT * from table1;
Next step would be DROP the tmp table and rename table1
Update: Based on your comment you can do the following, let me know if this solves your case.
This method basically creates a new table to replace your existing table.
I have used some of your code
CREATE TABLE stage (LIKE target INCLUDING DEFAULTS);
INSERT INTO stage SELECT id, 'new' FROM target WHERE value=`old`;
Above INSERT inserts rows to be updated with 'new', no need to run an UPDATE after this.
Bring unchanged rows
INSERT INTO stage SELECT id, value FROM target WHERE value!=`old`;
After this point you have target table which is your original table intact
stage table will have both sets of rows, updated rows with 'new' value and rows you did not want to change
To replace your target with stage
DROP TABLE target;
or to keep it further verification
ALTER TABLE target RENAME TO target_old;
ALTER TABLE stage RENAME TO target;
From a redshift developer:
This case doesn't require an upsert, or update+insert, and it is fine to just run the update:
UPDATE target SET value='new' WHERE value='old';
Another way would be to INSERT the rows you need and DELETE the other rows, but that's unnecessarily complicated.

APEX: Item is blank when trying to prepopulate with value

My Scenario:
I have a report on page 1 which has a link to page 2. This link passes an ID to page 2 (V2_ID gets set with V1_ID).
I then have two items called V2_ID and V2_NAME on page 2, as well as a PL/SQL process on page 2 which executes after the header loads which
select name into :V2_NAME from table where id = :V_ID;
The V2_ID shows the value, but the V2_NAME is always blank.
How can I pre-populate this variable. This is a very simple example as my use case is much more complicated, but the concept is the same. I can't use automatic row fetch because each item comes from a different table (it is a horrible database design, but I have to work with it).
Cheers
If on page 2 you have a PL/SQL process at the process point='On Load - After Header' and the procedure looks something like this:
Begin
select name into :V2_NAME from table where id =:V2_ID;
end;
and no condition on it then it should work.
Check that :V2_NAME does not have Source Type= Always NULL;
Correct me if Im wrong, from what ive understood, only one value is passed from page 1 to page 2 which is V1_ID to V2_ID,right?then on page 2, you have a process that will execute after the header loads and that is
SELECT name
INTO :V2_NAME
FROM TABLE
WHERE id = :V_ID
Is the :V_ID a typo?It should be :V2_ID,if its not maybe its the cause as to why :V2_NAME doesnt give you a value.
Instead of having a process after header, why dont you just put the query in the item SOURCE, choose type: QUERY, used: ALWAYS, REPLACE ANY EXISTING VALUES ON SESSION STATE.
then in the query box, put
SELECT name
FROM TABLE
WHERE id = :V2_ID