Error while renaming the column in postgres - sql

I have created new table by:
CREATE TABLE DIFF_ODATE_PERIOD AS
select test_3.odate - test_3.max_period from test_3;
And it gave me column name: ?column?
And when I am trying to change the name it gives me error:
ALTER TABLE DIFF_ODATE_PERIOD
RENAME COLUMN ?column? TO test;
ERROR: syntax error at or near "?" LINE 71: RENAME COLUMN ?column?
TO test;
Can I define the name while creating or after?

You would need to alias that column directly in the create table ... as select statement:
create table diff_odate_period as
select odate - max_period as test from test_3;

Related

How to change a column data type in Oracle View? [duplicate]

This question already has answers here:
How to alter column size of a view in Oracle
(2 answers)
Closed 3 years ago.
I am trying to change a data type of a column in a Oracle View by executing the following statement:
ALTER VIEW <view_name>
MODIFY (ID VARCHAR2(100));
I get the following error:
Error starting at line : 1 in command -
ALTER VIEW <view_name>
MODIFY (ID VARCHAR2(100))
Error report -
ORA-00922: missing or invalid option
00922. 00000 - "missing or invalid option"
*Cause:
*Action:
I referred to the post here. What's the correct way to achieve this? I guess Oracle expects CONSTRAINT keyword after MODIFY. The column I am modifying the data type of is one of the primary keys in the table on which this view stands.
You can not change the column data type of the VIEW.
Instead, you can change the data type of underlying base table's column (In that case refer the answer of the link mentioned in your question) or you can create a view query with that data type.
-- Your view
CREATE OR REPLACE VIEW <YOUR VIEW> AS
SELECT ID,
...
FROM <TABLE>;
You can change the SELECT query of the view.
CREATE OR REPLACE VIEW <YOUR VIEW> AS
SELECT cast(ID as varchar2(20)) as ID,
...
FROM <TABLE>;
Hope, this will help.
Cheers!!
You cannot change the column size of a view using ALTER. You just can't.
If you want to change a view definition, you have to find the original definition and rewrite it.
To find the original definition, look for the TEXT or TEXT_VC column in USER_VIEWS. Then use CAST to adjust the definition of the column of interest.
Example:
SQL> create or replace view V as
2 select dummy from dual;
View V created.
SQL> desc v;
Name Null? Type
DUMMY VARCHAR2(1)
SQL> select text_vc from user_views where view_name = 'V';
TEXT_VC
----------------------
select dummy from dual
SQL> create or replace view V as
2 select cast(dummy as varchar2(100)) dummy from dual;
View V created.
SQL> desc v;
Name Null? Type
DUMMY VARCHAR2(100)
Best regards,
Stew Ashton

Get table name from sub-query to use in delete / update / insert clauses in Oracle in one line

The goal is to use the table name returned from subselect in insert/update/delete statements. It fails to achieve desired results.
If we execute with TABLE (TABLE is oracle syntax word as described in reference)
delete from TABLE
(select DECODE(this returns table name as string) from REF_TABLE where where_clause);
then:
22905. 00000 - "cannot access rows from a non-nested table item"
If we execute without TABLE:
delete from
(select DECODE(this returns table name as string) from REF_TABLE where where_clause);
Then it actually deletes from REF_TABLE the record that satisfies when clause.
What is a proper way to pass table name from subselect to outer query?
Doc examples
EXECUTE IMMEDIATE 'delete from table :1' USING (select DECODE(...) from REF_TABLE WHERE where_clause);
PLS-00103: Encountered the symbol "delete from table :1" when
expecting one of the following:
:= . ( # % ;
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
You never mention table keyword when executing a DML query for example Insert, update and delete
delete from table_name;
update table_name set field='value';
insert into table_name (field) values ('value');
Table keyword is mentioned when you are executing a DDL statement like Create, Alter, drop and Truncate
Create table table_name (field varchar2(100));
Alter table table_name modify field1 varchar2(1000);
Drop table table_name;
truncate table table_name;
Hence the below query mentioned by you is correct
delete from
(select DECODE(this returns table name as string) from REF_TABLE where where_clause);
only limitation with the above query is that you have to use table_name source from a fixed table or view i.e. it cannot be picked from data dictionary views or you will get below error.
SQL Error: ORA-02030: can only select from fixed tables/views
02030. 00000 - "can only select from fixed tables/views"
*Cause: An attempt is being made to perform an operation other than
a retrieval from a fixed table/view.
*Action: You may only select rows from fixed tables/views.
you can use PL/SQL block similar as mentioned below to delete tables recursively
DECLARE
CURSOR c
IS
SELECT table_name FROM user_tables WHERE table_name LIKE '%SANDEEP26FEB16_2%';
BEGIN
FOR c1 IN c
LOOP
EXECUTE IMMEDIATE 'delete from ' || c1.table_name;
COMMIT;
END LOOP;
END;

Rename column named TYPE, LEVEL in sqlplus

This needs to be done in sql plus.
Hi, I'm struggling to rename two columns in my table. They are named "TYPE" and "LEVEL".
This needs to be done in sql plus with no exception.
The following does not work (works in sql developer tho):
alter table client rename column level to clevel;
alter table client rename column "level" to clevel;
LEVEL is an Oracle keyword, though not reserved. If you want to use it as an object name then you need to represent the name of an object with a quoted identifier using double quotation marks whenever you refer to that object.
SQL> SELECT keyword, reserved FROM V$RESERVED_WORDS WHERE keyword='LEVEL';
KEYWORD R
------------------------------ -
LEVEL N
SQL>
That is the reason if you use the keyword LEVEL as nonquoted identifier, it will throw an error:
SQL> CREATE TABLE t(level NUMBER);
CREATE TABLE t(level NUMBER)
*
ERROR at line 1:
ORA-00904: : invalid identifier
Now, per the documentation about Database Object Names and Qualifiers, if you create the object using double-quotation marks, it becomes case sensitive and must be always used the same way wherever the object is referenced.
For example,
SQL> CREATE TABLE t1("level" NUMBER);
Table created.
SQL>
SQL> ALTER TABLE t1 RENAME COLUMN "level" to clevel;
Table altered.
SQL>
SQL> CREATE TABLE t2("LEVEL" NUMBER);
Table created.
SQL>
SQL> ALTER TABLE t2 RENAME COLUMN "LEVEL" to clevel;
Table altered.
SQL>
It is better not to use the keyword, and give a proper naming convention.
SQL> CREATE TABLE t(clevel NUMBER);
Table created.
SQL>
You can also execute sql scripts in sqlplus. Just save your statement in a file and then use the following command:
#{file-path}
See also this link

Change table name with sysdate

I want to change a table name by appending SYSDATE to it. For example, I want to change table EXAMPLE_TABLE to EXAMPLE_TABLE_05_01_2015, but I want to get the date from SYSDATE.
I prepared the following but it is not working:
ALTER TABLE "MYDB"."EXAMPLE_TABLE" rename to (SELECT 'EXAMPLE_TABLE' || TO_CHAR(SYSDATE, '_dd_MM_yyyy') FROM DUAL);
How can I make it work?
Here is the error:
SQL Error: ORA-14047: ALTER TABLE|INDEX RENAME may not be combined with other operations
14047. 00000 - "ALTER TABLE|INDEX RENAME may not be combined with other operations"
*Cause: ALTER TABLE or ALTER INDEX statement attempted to combine
a RENAME operation with some other operation which is illegal
*Action: Ensure that RENAME operation is the sole operation specified in
ALTER TABLE or ALTER INDEX statement;
Use execute immediate.
begin
execute immediate
'alter table mydb.example_table rename to ' ||
'example_table_' || to_char(sysdate, 'dd_mm_yyyy');
end;
/
That said, I have the hunch that you'd be better off using partitioned tables.
In SQL*Plus, you could use the variable substitution.
Just another way :
SQL> CREATE TABLE t(ID NUMBER)
2 /
Table created.
SQL>
SQL> COLUMN new_tablename NEW_VALUE new_tablename
SQL> SELECT 't_' || to_char(sysdate, 'dd_mm_yyyy') AS new_tablename from dual
2 /
NEW_TABLENAM
------------
t_05_01_2015
SQL>
SQL> RENAME t TO &new_tablename
2 /
old 1: RENAME t TO &new_tablename
new 1: RENAME t TO t_05_01_2015
Table renamed.
SQL>
SQL> select * from t_05_01_2015;
no rows selected
SQL>
So, now the table T is renamed to T_05_01_2015.

Invalid Sequence name in Oracle db

Recently , I am facing an issue with sequence in Oracle.
alter sequence seq_name increment by 100
will give me an error "Invalid sequence name"
However, if I changed it to
alter sequence "seq_name" increment by 100
It will work perfectly fine. Anyone is able to explain the rational behind this?
Thanks
Sebastian
ps. I am using rails with oci8 to create my oracle tables.
Your sequence was created with case-sensitive name (using quatation marks), so you can refer to it only with strict name - in quotation marks. If you want to refer to it without such problems just create sequence not using quotation marks. Examples below (with table name):
SQL> create table "t1"(c int);
Table created.
SQL> select * from t1;
select * from t1
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from "t1";
no rows selected
SQL> select * from "T1";
select * from "T1"
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> create table t2(c int);
Table created.
SQL> select * from t2;
no rows selected
SQL> select * from T2;
no rows selected
SQL> select * from "t2";
select * from "t2"
*
ERROR at line 1:
ORA-00942: table or view does not exist
SQL> select * from "T2"; -- name without quatation marks is uppercase by default
no rows selected
Sequence was created as lowercase. Like this:
CREATE SEQUENCE "seq_name" MINVALUE 1 MAXVALUE 999999999999999 INCREMENT BY 2 START WITH 1 CACHE 20 NOCYCLE NOKEEP NOSCALE GLOBAL ;
Example:
select * from user_sequences where sequence_name='SEQ_NAME'; --No rows selected
select * from user_sequences where sequence_name='seq_name'; -- 1 row
If you create with name : "SEQ_name".
select * from user_sequences where sequence_name='SEQ_name'; -- 1 row
Because,
when you create an object (with object name enclosed with double quotes) it will store/create as it is. Without double quotes, it would be uppercase.