Unable to delete restore point in Oracle - oracle

Someone (not me) has created a number of restore points where the name includes a hyphen. For example restore_point-001.
When I try drop the restore point with Drop Restore Point it will error with sql command error saying that it is not properly ended. I have tried putting the name in single quotes and I have tried renaming the name, but v$restorepoint doesn't allow for this.
Is there a way to drop this restore point being named this way?

I have tried putting the name in single quotes ...
I'm not a DBA, but what you described reminds me of this:
Let's try to create a table whose name contains a "minus" sign (just like your restore point):
SQL> create table restore_point-001 (id number);
create table restore_point-001 (id number)
*
ERROR at line 1:
ORA-00922: missing or invalid option
Nope, it won't work. You said you enclosed the name into single quotes:
SQL> create table 'restore_point-001' (id number);
create table 'restore_point-001' (id number)
*
ERROR at line 1:
ORA-00903: invalid table name
SQL>
Nah, it's not a string literal.
So, let's try with double quotes:
SQL> create table "restore_point-001" (id number);
Table created.
Ha! It works! Let's now drop it: without any quotes:
SQL> drop table restore_point-001;
drop table restore_point-001
*
ERROR at line 1:
ORA-00933: SQL command not properly ended
Single quotes:
SQL> drop 'table restore_point-001';
drop 'table restore_point-001'
*
ERROR at line 1:
ORA-00950: invalid DROP option
Double quotes:
SQL> drop table "restore_point-001";
Table dropped.
SQL>
So, yes - I have no idea why people insist on "invalid" characters and what good does -001 do over _001, but - if I were you, I'd try with
drop restorepoint "restore_point-001";
and see what happens.

Related

How do I create a trigger with the below problem statement?

I am trying to create a trigger named transaction_type_af_insert that is triggered whenever a new record is inserted into ransaction_type table. This trigger will insert the new type and action into the table transaction_type_log_history after the insertion of transaction type details. The action name in the affected log table transaction_type_log_history is 'After_Insert_transaction_type'.
But facing below error:
Warning: Trigger created with compilation errors.
insert into transaction_type(id,type)values(22,'Credit Card')
*
ERROR at line 1:
ORA-04098: trigger 'P12097.TRANSACTION_TYPE_AF_INSERT' is invalid and failed
re-validation
Please help me resolve the issue
CREATE TRIGGER transaction_type_af_insert
AFTER INSERT
ON transaction_type FOR EACH ROW
DECLARE
type varchar(30)
action varchar(30)
BEGIN
UPDATE transaction_type_log_history
SET action = 'After_Insert_transaction_type'
WHERE transaction_type.id = transaction_type_log_history.id;
END;
You can query the user_errors view to see what is actually wrong; some clients let you show errors as a shortcut after you create the object (SQL Developer, SQLcl, SQL*Plus..) and see the 'Warning: Trigger created with compilation errors' message. With your current code that will say something like:
PLS-00103: Encountered the symbol "(" when expecting one of the following:
; is default authid as force under accessible
type is a reserved word so you can't use that for a local variable name - it's expecting you to be declaring a record type. Changing the name of that variable reveals the next error:
PLS-00103: Encountered the symbol "ACTION" when expecting one of the following:
:= ; not null default character
which is because you don't have semicolons after your declarations.
Fixing that still gets 'ORA-00904: "TRANSACTION_TYPE"."ID": invalid identifier' because your update statement doesn't include the transaction_type table. You can use correlation names to refer to data in the original table row statement the trigger fired against, using :new by default; so this now compiles:
CREATE OR REPLACE TRIGGER transaction_type_af_insert
AFTER INSERT ON transaction_type
FOR EACH ROW
DECLARE
l_type varchar2(30);
l_action varchar2(30);
BEGIN
UPDATE transaction_type_log_history
SET action = 'After_Insert_transaction_type'
WHERE transaction_type_log_history.id = :new.id;
END;
/
db<>fiddle
You aren't actually using either of the the two locally declared variables so you can remove those and the declare keyword.
You also seem to be trying to update the history table instead of inserting a new row there, which the problem statement seems to suggest you should be doing.

Change size table name in Oracle 11g [duplicate]

In my Oracle DB setup all the tables are created under dedicated user account SYS0MYUSER. When executing following query on my system I got SQL Error: ORA-00903: invalid table name
SELECT COUNT(*) FROM SYS0MYUSER.USER;
I tried to escape the reserved keyword like this:
SELECT COUNT(*) FROM "SYS0MYUSER.USER";
But then I got another error SQL Error: ORA-00942: table or view does not exist
What is the correct way to escape user name + reserved keyword combination ?
UPDATE:
What's about table alias do I have to use double quotes too ?
If you have created the table using quoted identifier, then you must always use double-quotation marks wherever you refer the object.
From documentation,
Database Object Naming Rules
Every database object has a name. In a SQL statement, you represent
the name of an object with a quoted identifier or a nonquoted
identifier.
A quoted identifier begins and ends with double quotation marks ("). If you name a schema object using a quoted identifier, then you
must use the double quotation marks whenever you refer to that object.
A nonquoted identifier is not surrounded by any punctuation.
For example,
SQL> CREATE TABLE "USER"(A NUMBER);
Table created.
SQL>
SQL> SELECT COUNT(*) FROM LALIT.USER;
SELECT COUNT(*) FROM LALIT.USER
*
ERROR at line 1:
ORA-00903: invalid table name
SQL>
SQL> SELECT COUNT(*) FROM LALIT."USER";
COUNT(*)
----------
0
SQL>
So, you need to refer the table as a quoted identifier:
SELECT COUNT(*) FROM SYS0MYUSER."USER";
Update OP updated his question regarding table alias.
What's about table alias do I have to use double quotes too ?
Table alias has nothing to do with the quoted identifier.
For example,
SQL> SELECT t.* FROM LALIT."USER" t;
no rows selected
SQL>
SELECT COUNT(*) FROM "SYS0MYUSER"."USER";

How to recreate public synonym "DUAL"?

Using SQLDeveloper 4.0.1.14 to create an export script (separate files & "drops" checked), it generated me those 4 lines among others in DROP.sql:
DROP SYNONYM "PUBLIC"."DUAL";
DROP SYNONYM "PUBLIC"."DBMS_SQL";
DROP SYNONYM "PUBLIC"."DBMS_LOCK";
DROP SYNONYM "PUBLIC"."DBMS_OUTPUT";
Now that I have accidentally passed the whole script using SYSTEM user, I can no longer do modification (create or drop tables) to the database, I have that error popping:
An error was encountered performing the requested operation:
ORA-00604: error occurred at recursive SQL level 1
ORA-00942: table or view does not exist
00604. 00000 - "error occurred at recursive SQL level %s"
*Cause: An error occurred while processing a recursive SQL statement
(a statement applying to internal dictionary tables).
*Action: If the situation described in the next error on the stack
can be corrected, do so; otherwise contact Oracle Support.
Vendor code 604
The problem is that I'm getting that error event when I try this:
CREATE OR REPLACE PUBLIC SYNONYM "DUAL" FOR "SYS"."DUAL";
I precise that the SYS.DUAL table still exists as SELECT 1 FROM SYS.DUAL works but SELECT 1 FROM DUAL fails with ORA-00942: table or view does not exist.
I tried to recreate the synonym as SYSTEM and SYSDBA with the same failure.
Can I recreate those synonyms with another way?
Try it as SYS but without the doauble quotes, i.e.:
CREATE OR REPLACE PUBLIC SYNONYM DUAL FOR SYS.DUAL;
not
CREATE OR REPLACE PUBLIC SYNONYM "DUAL" FOR "SYS"."DUAL";
As I understand it the double quotes make the object name case sensitive.
Update - If you have access to metalink then you will find the answer in note 973260.1, something aboput a trigger firing :
ALTER SYSTEM SET "_SYSTEM_TRIG_ENABLED"=FALSE SCOPE=MEMORY;
CREATE OR REPLACE PUBLIC SYNONYM DUAL FOR SYS.DUAL;
ALTER SYSTEM SET "_SYSTEM_TRIG_ENABLED"=true SCOPE=MEMORY;
The note suggest that if this doesnt work, query DBA_TRIGGERS to find a BEFORE CREATE trigger enabled, and if found disable it and then re-issue create synonym statement, then re-enable the trigger.

Double quotes inside single quotes in oracle statement

I'm having the following problem with an old database. We migrated from sql to oracle recently and I'm having trouble with an insert statement where the column name is "default". Any ideas (I'm not allowed to change the column name, that would be by far the best solution!)?
It looks somehow like this, only with a billion more columns and it's inside a large if-when-else construction for validating issues, so I can't drop the execute immediate.
EXECUTE IMMEDIATE 'INSERT INTO trytable (ID, "DEFAULT") VALUES (''monkey1'', 0)'
I don't think the problem comes from your column name, as shown in this working example:
SQL> CREATE TABLE trytable (ID VARCHAR2(10), "DEFAULT" NUMBER);
Table created
SQL> BEGIN
2 EXECUTE IMMEDIATE
3 'INSERT INTO trytable (ID, "DEFAULT") VALUES (''monkey1'', 0)';
4 END;
5 /
PL/SQL procedure successfully completed
Technically, you can have a table column name named DEFAULT, even if it's generally a bad idea that will lead to confusion. You will only be able to interact with it through the double-quote " syntax because DEFAULT is a reserved word.
If you specify double quotes around identifiers, Oracle will treat them as case-sensitive, so you have to make sure that they match the table specs.
In your case, it would help to have the specific error message.
The below will executes if your column name is "DEFAULT":
BEGIN
EXECUTE IMMEDIATE 'INSERT INTO TRYTABLE(ID, "DEFAULT")VALUES(''monkey1'',0)';
END;
"DEFAULT" and "default" makes difference.

How can i ignore dupkey with sqlplus?

I want to write a sql plus error for when the oracle find a record, or more records, that already exist and just ignore it/them. This is a example:
sqlError=`egrep "ORA-[0-9][0-9][0-9][0-9][0-9]" ${FILE_SPOOL_DAT} | awk '{print $0}';`
if test ! -f ${FILE_SPOOL_DAT}
then
echo "Error request " >> ${FILE_SPOOL_DAT}
else
if [ ! "$sqlError" = "" ] #controls if the variable $sqlError contains a value different from spaces, i think this is the point to change
then
echo "Error $sqlError" >> ${FILE_SPOOL_DAT}
fi
fi
In this example sqlplus controls if the variable $sqlError contains a value different from spaces. How can i change this condition put the DUPKEY error? Thanks
If you're using 11g, the IGNORE_ROW_ON_DUPKEY_INDEX hint can help.
SQL> create table table1(a number primary key);
Table created.
SQL> insert into table1 values(1);
1 row created.
SQL> insert into table1 values(1);
insert into table1 values(1)
*
ERROR at line 1:
ORA-00001: unique constraint (JHELLER.SYS_C00810741) violated
SQL> insert /*+ IGNORE_ROW_ON_DUPKEY_INDEX(table1(a))*/ into table1 values(1);
0 rows created.
Yikes, that's dangerous! If Oracle finds one or more records that already exists, it will normally rollback the transaction. Suppressing the relevant error messages is way to late.
A much better place is to instruct Oracle to ignore duplicate records directly during the INSERT, for instance by using the MERGE command:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/statements_9016.htm#i2081218
When you get a duplicate key error it really means you have violated a constraint set on a table.
Assuming whoever designed the table understood the data model, what you want to do is a BAD idea.
If it is a bad design, change the table's metadata by removing the constraint.
The MERGE command may let you work around it, which I doubt, but with the subsequent data mess left behind I do not believe it is worth the risk.
If you are just playing, remove the constraint anyway. I don't know how your table is set up,
but you will probably have to ALTER or DROP and CREATE an index, or ALTER the table.
If this is development for production and thus paid work, don't try to get around the constraint without talking to the person(s) who designed the table to start with.

Resources