unable use columns defined in xmltable - sql

Getting error while running the below query
select xt.IssueDate, xt.SequenceNumber from xml_tab x ,
XMLTABLE('/EmployeeInfo'
PASSING x.xml_data COLUMNS
"IssueDate" varchar2(10) path '#IssueDate',
"SequenceNumber" integer path '#SequenceNumber'
) xt;
error I am getting
ORA-00904: "XT"."SEQUENCENUMBER": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action: Error at Line: 114 Column: 22

You are mixing quoted and non-quoted identifiers. The columns clause in the XMLTable call is defining SQL-type names, not XML-type names.
You can either quote both:
select xt."IssueDate", xt."SequenceNumber" from xml_tab x ,
XMLTABLE('/EmployeeInfo'
PASSING x.xml_data COLUMNS
"IssueDate" varchar2(10) path '#IssueDate',
"SequenceNumber" integer path '#SequenceNumber'
) xt;
or preferably quote neither:
select xt.IssueDate, xt.SequenceNumber from xml_tab x ,
XMLTABLE('/EmployeeInfo'
PASSING x.xml_data COLUMNS
IssueDate varchar2(10) path '#IssueDate',
SequenceNumber integer path '#SequenceNumber'
) xt;

Related

Implement cursor when select value changes name

I cannot seem to successfully implement the cursor what I change the statement name picked from select,
For example:
SELECT userid as user_no, CURSOR(SELECT user_no
FROM my_table.punter P
WHERE P.user_no = Q.user_no)
FROM another_table.maria_punter Q;
I get the following error:
ORA-00904: "Q"."USER_NO": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 4 Column: 22
You cannot use the column alias given in the same SELECT clause; instead, use the unaliased column name from the table:
SELECT userid as user_no,
CURSOR(
SELECT user_no
FROM my_table.punter P
WHERE P.user_no = Q.userid
)
FROM another_table.maria_punter Q;
fiddle

Invalid datatype for range function

I am trying to use range partition on a table for a column of type number. However, Oracle 12c throws an error saying it is an invalid data-type. I don't understand why/what is invalid in the command.
create table partition_tester (
some_column number not null,
another_column varchar2(10),
partition by range(some_column) interval(10)
);
I am connecting to the database using SQL developer. It seems to have issue with the range function. On executing above script it throws:
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
Any help is appreciated.
remove the comma after " another_column varchar2(10)," and specify at least one partition.
create table partition_tester (
some_column number not null,
another_column varchar2(10))
partition by range(some_column) interval(10)
(
partition p0 values less than (10)
)

How to match user defined data type in SQL

I have a user defined data type CRM_IDS:
create or replace TYPE CRM_IDS AS VARRAY(10) OF VARCHAR(32);
In my table purecov_summary, the data type of column NAV_CRM_ID is CRM_IDS:
CREATE TABLE "PE_REG"."PURECOV_SUMMARY"
(
...
"NAV_CRM_ID" "PE_REG"."CRM_IDS" ,
...
)
When I select the nav_crm_id of the first line with:
select nav_crm_id
from purecov_summary
where rownum = 1
I can get "PE_REG.CRM_IDS('10035005')".
But when I run:
select *
from purecov_summary
where nav_crm_id = PE_REG.CRM_IDS('10035005')
I get this error:
ORA-00932: inconsistent datatypes: expected - got PE_REG.CRM_IDS
00932. 00000 - "inconsistent datatypes: expected %s got %s"
*Cause:
*Action:
Error at Line: 1 Column: 37
How can I select with PE_REG.CRM_IDS type?
The trick is to use nested varray column (nav_crm_id) within table keyword as table(nav_crm_id). So consider one of the following :
select p.*
from purecov_summary p
where exists (select column_value from table(p.nav_crm_id) where column_value = '10035005')
or
select p.*
from purecov_summary p
join table(p.nav_crm_id) c
on c.column_value = '10035005';
Demo

Wrong SQL statement in Oracle 11g

I want to get the value SESSIONID from this table:
CREATE TABLE SESSIONSLOG(
SESSIONID VARCHAR2(30 ) NOT NULL,
USERNAME VARCHAR2(30 ),
IPADDRESS VARCHAR2(30 ),
LOGINTIME TIMESTAMP(6),
LOGOUTTIME TIMESTAMP(6)
)
/
I tried this SQL query:
SELECT SESSIONID FROM ACTIVESESSIONSLOG
But I get this error message:
ORA-00904: "SESSIONID": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 1 Column: 7
How I can fix this?
You're querying the wrong table:
CREATE TABLE SESSIONSLOG
...
SELECT SESSIONID FROM ACTIVESESSIONSLOG
ACTIVESESSIONSLOG is not the same as SESSIONSLOG.
You are querying wrong table ... table should be SESSIONSLOG
SELECT SESSIONID FROM SESSIONSLOG
#user1285928 you are not supposed to use " ACTIVESESSIONSLOG " .
Instead Use SESSIONSLOG. Also use the delimiter symbol ;
SELECT SESSIONID FROM SESSIONSLOG;

Column Name beginning with a number?

I have a column name in one of my tables called: 3RD_DIAG_CODE - VARCHAR2 (10 Byte)
When I try to run a query, it gives me the following error highlighting 3RD_DIAG_CODE.
ORA-00923: FROM keyword not found where expected.
How can I bring this field in without it throwing an error every time I bring this field in?
If you are using column names that start with a number then you need to use double quotes. For example:
create table foo (
"3RD_DIAG_CODE" varchar2(10 byte) --make sure you use uppercase for variable name
);
insert into foo values ('abc');
insert into foo values ('def');
insert into foo values ('ghi');
insert into foo values ('jkl');
insert into foo values ('mno');
commit;
select * from foo;
3RD_DIAG_C
----------
abc
def
ghi
jkl
mno
select 3RD_DIAG_CODE from foo;
RD_DIAG_CODE
------------
3
3
3
3
3
select "3RD_DIAG_CODE" from foo;
3RD_DIAG_C
----------
abc
def
ghi
jkl
mno
Edit: As for the error message itself, you are probably (as BQ wrote) missing a comma from the select clause.
Check your specification, but in SQL Server we would have to enclose that column name in square brackets: [3RD_DIAG_CODE]
You probably have two columns listed without a comma between them.
create table t (id number primary key, 3d varchar2(30))
Error at Command Line:1 Column:39
Error report:
SQL Error: ORA-00904: : invalid identifier
00904. 00000 - "%s: invalid identifier"
create table t (id number primary key, "3d" varchar2(30));
table T created.
desc t
Name Null Type
---- -------- ------------
ID NOT NULL NUMBER
3d VARCHAR2(30)
> select id, 3d from t --[as #gsiem mentions: THIS IS BAD]
ID 3D
---------------------- --------
> select id, "3d" from t
ID 3d
---------------------- ------------------------------
> select id, [3d] from t
Error starting at line 7 in command:
select id, [3d] from t
Error at Command Line:7 Column:11
Error report:
SQL Error: ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
> select id 3d from t
Error starting at line 8 in command:
select id 3d from t
Error at Command Line:8 Column:10
Error report:
SQL Error: ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"
*Cause:
*Action: