Invalid Sequence name in Oracle db - sql

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.

Related

ORA-01722: invalid number while select numeric column and same numeric column reference in where

select id
from abc
where id = 1001;
return invalid number.
abc is a view
datatype of id is number
In create view cast is used
CREATE OR REPLACE VIEW abc AS
SELECT CAST (SUBSTR(T_ID,3,100) AS NUMBER) AS ID
from TEST
id column has all numeric values only
SELECT * FROM ABC WORKS FINE AND SO insert query works
tried below code which returns nothing in dbms output
declare
l_dummy number;
begin
for cur in (select ID from abc)
loop
begin
l_dummy := to_number(cur.ID);
exception
when others then dbms_output.put_line(cur.ID);
end;
end loop;
end;
column datatype nullable
ID NUMBER Yes 1 NO NO NO
SELECT *
FROM abc
WHERE
ID = 1001
returns:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause: The specified number was invalid.
*Action: Specify a valid number.
There are two problems:
SUBSTR(T_ID,3,100) will not return number always -- We need to consider the only numeric query
If we apply anything in WHERE condition of the view, Order of the execution of the WHERE condition is the call of Oracle optimizer.
Please see below code:
-- Data preparation
create table TEST (T_ID varchar2(20));
INSERT INTO TEST VALUES('AB1000'); -- Good data
INSERT INTO TEST VALUES('AB1001'); -- Good data
INSERT INTO TEST VALUES('CD1001'); -- Good data
INSERT INTO TEST VALUES('XY1004'); -- Good data
INSERT INTO TEST VALUES('XYZ1004'); -- Bad data
--
-- Data in the table
SELECT * FROM TEST;
Output
-- You need to create your view as following
-- rownum is used so that WHERE clause of view is executed first
-- and then any external WHERE clause on the view is executed
CREATE OR REPLACE VIEW ABC_NEWVIEW AS
SELECT
ID
FROM
(
SELECT
CAST(SUBSTR(T_ID, 3, 100) AS NUMBER) AS ID,
ROWNUM RN
FROM
TEST
WHERE
CASE
WHEN TRIM(TRANSLATE(SUBSTR(T_ID, 3, 100), '0123456789-,.', ' ')) IS NULL THEN 'numeric'
ELSE 'alpha'
END = 'numeric'
)
--
-- View
SELECT * FROM ABC_NEWVIEW
Output
-- Query using WHERE condition
SELECT *
FROM ABC_NEWVIEW
WHERE
ID = 1001
Output
You can find the demo in the following link:
DB Fiddle demo
You can see the demo of VIEW without ROWNUM:
Reproduced your issue -- So the issue is the order of the execution.
Cheers!!

Create a global temporary table in Oracle SQL

I am new to SQL. I want to create a (global or not) temporary table in Oracle SQL which will include a simple selection of data of the form SELECT * FROM tbl_NAME WHERE... and which after the end of my session will be deleted (just like the MSFT SQL temporary tables of the form ##tbl_NAME).
I found online that one way to do it is:
CREATE GLOBAL TEMPORARY TABLE tmp_table
SELECT * FROM tbl_NAME WHERE conditions.
ON COMMIT PRESERVE ROWS;
although I get the error ORA-00904: invalid identifier
I also found that another alternative is
CREATE PRIVATE TEMPORARY TABLE tmp_table AS
SELECT * FROM tbl_NAME WHERE conditions;
which gives the error ORA-00905: missing keyword.
Please note that I already know that one alternative that works is:
DROP TABLE tmp_table;
CREATE TABLE tmp_table AS
SELECT * FROM tbl_NAME;
DROP TABLE tmp_table;
What you want to do with rows comes first; SELECT comes next:
SQL> create global temporary table gtt_dept
2 on commit preserve rows --> first
3 as
4 select * from dept; --> next
Table created.
SQL>
The below format you had shown works in TERADATA
CREATE GLOBAL TEMPORARY TABLE tmp_table
SELECT * FROM tbl_NAME WHERE conditions.
ON COMMIT PRESERVE ROWS;
In Oracle it works like this
create global temporary table gtt_dept
on commit preserve rows
as
select * from dept;

How to display first name in single line?

I'm learning oracle sql.
I'm just trying to display all the employees first name from 'Employee' table in single line with comma separation.
ex: john,alex,rosy
I'm using SQL*Plus for running the query.
You have to use some built in function like:
SYS_CONNECT_BY_PATH , ROW_NUMBER () OVER
Here is the solution
SQL>
SQL> create table test(id int, name varchar(10));
Table created
SQL> begin
2 insert into test values(1,'john');
3 insert into test values(2,'alex');
4 insert into test values(3,'rosy');
5 end;
6 /
PL/SQL procedure successfully completed
SQL> select listagg(name ,',') within group(order by id) result from test;
RESULT
--------------------------------------------------------------------------------
john,alex,rosy
SQL> drop table test purge;
Table dropped
SQL>

How can I write a PL/SQL procedure to copy tables and contents from another account

I need to write a PL/SQL procedure to create tables that match the ones in another account(I have access to that account). They need to have same columns and types. Also, they need to be filled with the same data
Help me!
EDIT:
SQL> CREATE OR REPLACE PROCEDURE MakeTables
2 AS
3 BEGIN
4 EXECUTE IMMEDIATE
5 'CREATE TABLE Table1 AS (SELECT * FROM ANOTHER_ACCT.Table1);
6 CREATE TABLE Table2 AS (SELECT * FROM ANOTHER_ACCT.Table2);
7 CREATE TABLE Table3 AS (SELECT * FROM ANOTHER_ACCT.Table3);
8 CREATE TABLE Table4 AS (SELECT * FROM ANOTHER_ACCT.Table4)';
9 END;
10 /
Procedure created.
But when I run this I get this error:
SQL> BEGIN
2 MakeTables;
3 END;
4 /
BEGIN
*
ERROR at line 1:
ORA-00911: invalid character
ORA-06512: at "BS135.MAKETABLES", line 4
ORA-06512: at line 2
When you say, another "account", do you mean, another "user/schema"? If so, this can be simple. Go read/google about "oracle create table as select". This lets you create a table from a select statement, so you could issue a statement such as
create table new_table as select * from other_schema.old_table
You don't need any PL/SQL unless you wanted to automate the process for creating many tables. Then you could query the data dictionaries as a driver.
(also, please read on how to ask proper questions here: https://stackoverflow.com/questions/how-to-ask )

string comparing query with chinese chars - Oracle Database

I'm having trouble executing a simple query such as the following
select * from table_name where variabe_name like '在职'
the problem is the chinese chars. Those chars are in the table (I just copied them after doing a select * from table, so the displaying of the chinese chars works just fine) but it doesn't seem to work. When it execute the query, it returns 0 rows.
I' ve also tried
select * from table_name where variabe_name like '%在职%'
and
select * from table_name where variabe_name = '在职'
But that doesn't work either.
Any clue of what the problem might be?
Thnaks a lot
==> Found solution: put 'N' before the chinese characters, so that they are interpreted as Unicode. Like: where field like N'罐'
SQL> create table mytbl (data_col varchar2(200));
Table created
SQL> insert into mytbl values('在职');
1 row inserted.
SQL> commit;
Commit complete.
SQL> select * from mytbl where data_col like '%在职%';
DATA_COL
-----------
在职
SQL> SELECT * FROM nls_database_parameters where parameter='NLS_CHARACTERSET';
PARAMETER VALUE
------------------------------ ----------------------------------------
NLS_CHARACTERSET AL32UTF8
Your NLS_CHARACTERSET should be set to AL32UTF8. So try
SQL> ALTER SESSION SET NLS_CHARACTERSET = 'AL32UTF8';
Also make sure that parameter NLS_NCHAR_CHARACTERSET is set to UTF8.
SQL> ALTER SESSION SET NLS_NCHAR_CHARACTERSET = 'UTF8';