What is the meaning of '#' symbol in Oracle SQL? - oracle

What is the # symbol mean in oracle?
For example:
select * from question_answer#abcd where id = '45'

It refers to a non-local table, the bit behind the # is the db descriptor.
select * from question_answer#abcd where id = '45'
Means select not from the local question_answer table, but from the table on the db designated as abcd. The keyword to google for is dblink

This is the syntax for accessing a table via a database link called "abcd"
See the documentation for CREATE DATABASE LINK, or to see the defined db links:
SELECT * FROM all_db_links;

Related

data extraction from oracle source

I am trying to extract data from oracle source using pyspark.
I am using this code.
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('Spark_Job').getOrCreate()
driver = 'oracle.jdbc.driver.OracleDriver'
url = 'jdbc:oracle:thin:#XXXXXXXXXXXXXXXXXX/XXX'
user = 'XXXXX'
password = 'XXXXXXXX'
query = 'SELECT col_1 from schema_1.view_1'
df = spark.read.format('jdbc').option('driver', driver).option('url', url).option('dbtable', query)\
.option('user', user).option('password', password).load()
df.show(10)
I am getting this error :
java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name
ORA-00903 means that you tried to use table name, but - that table doesn't exist.
There's only one select statement here:
SELECT col_1 from schema_1.view_1
-------- ------
owner table (or view) name
("user")
Info you posted suggests that you're connected as
user = 'XXXXX'
If view_1 belongs to currently connected user, you don't have to specify owner name so query would then be just select col_1 from view_1.
If view_1 belongs to currently connected user, then view_1 must be correctly spelled. Oracle doesn't care about letter case unless you decided to enclose its (table's, view's) name into double quotes and used lower or mixed case - then you have to do it every time you reference that table, so query might be e.g. select col_1 from "View_1".
If view_1 belongs to user who is different from currently logged user (that's what information you posted suggests), then yes - preceding table's name with its owner's name is the way to do it. However, schema_1 (the owner) has to grant at least select privilege on view_1 to XXXXX - otherwise, it won't work. (Double quotes/letter case issue still stands.)
I can't tell which situation of these you have, but now you have something to check and act appropriately.

Oracle.ManagedDataAccess.Client ORA-00903 invalid table name

I'm using the sqldeveloper and have one database connection with the following connection string:
MES#//localhost:1521/xepdb1
MES is the schema owner and a select statement like this shows me what I want to see:
select count(*) from site
I'm also using Visual Studio and I'm trying to connect to the database by using the Oracle.ManagedDataAccess.Client
I'm using exactly the same connection string.
The connect to the database works fine. But I'm always getting the 00903 error.
Any idea what the problem can be ?
Many thanks in advance
I've tried also something like this:
select count(*) from mes.site
If you have used quoted identifiers to create the table then you will need to use quoted identifiers (and use the same case) whenever you access the table.
For example:
CREATE TABLE MES."site" (something NUMBER);
Then you would need to use:
SELECT count(*) FROM MES."site";
or
SELECT count(*) FROM mes."site";
or
SELECT count(*) FROM mEs."site";
The MES schema name is unquoted so you can use any case (and Oracle will implicitly convert it to upper-case to look it up in the data dictionary); however, "site" is a quoted identifier and Oracle will respect the case-sensitivity of the identifier and you MUST use the correct case and the surrounding quotes.
You can see the exact case you need to use in the result of the query:
SELECT owner, table_name FROM all_tables WHERE UPPER(table_name) = 'SITE';
If the data dictionary shows that the table name is upper-case then you can use an unquoted identifier (assuming that all the other naming rules have been respected and the table name is not a reserved/keyword) otherwise you will need to use a quoted identifier.
Normally you would get the ORA-00942: table or view does not exist exception but you can get ORA-00903: invalid table name when you use a keyword for a table name:
CREATE TABLE "NUMBER" (x) AS
SELECT 1 FROM DUAL;
Then:
SELECT COUNT(*) FROM NUMBER;
Give the exception:
ORA-00903: invalid table name
And:
SELECT COUNT(*) FROM "NUMBER";
Works.
However, MES and SITE are not keywords that should trigger that.
fiddle

Can't get Oracle SQL Developer to display specific values from object table

I've started learning SQL and playing with Oracle SQL Developer v22. For some reason I'm not able to retrieve value from an object table by using SELECT VALUE(A). It only gives my user ID.object_name as in the below screenshot. Any tips why?
SELECT VALUE(A) FROM table A;
If I use SELECT * FROM table; all is fine.
SELECT * FROM table A;
I've tried printing using dmbs_output, same problem.
Tried with other object tables, same behaviour.
Please share your table DDL and DML for a more accurate answer, but setting this preference should do what you're looking for
'Display Struct Value in Grid'
I talk about that here
Disclaimer: I work for Oracle and am the product manager for SQL Developer.

Informix - select from a table of another user

I have to do CRUD operations on a table that is not owned by the user I am using to connect to my Informix database. I have been granted the necessary privileges to do the operations, but I do not know how to do the actual query.
I have little experience with Informix, but I remember in OracleDB I had to do reference the shema like so:
SELECT * FROM SCHEMA.TABLE;
In Informix should I reference the user that owns the table ? Like :
SELECT * FROM OWNER:TABLE
Or can I just do :
SELECT * FROM TABLE
Thanks for any help !
In Informix you can generally use the table name without or without the owner prefix unless the database was created with mode ANSI in which case the owner prefix is required. Note that the correct syntax when using the owner is to use a period "." as in:
SELECT * FROM owner.table;
The colon is used to separate the database name as shown in the Informix Guide to SQL: Syntax https://www.ibm.com/docs/en/informix-servers/14.10?topic=segments-database-object-name#ids_sqs_1649
FYI you can determine if the database is mode ANSI with this query:
SELECT is_ansi FROM sysmaster:sysdatabases WHERE name = "<database name>";

ORA-00942: Can select from "schema.table" but not "table"?

I experienced an ORA-00942 ("table or view does not exist") when executing
select * from brunch
However, no such problem when executing
select * from joe.brunch
May i know what is the issue here?
Unqualified, BRUNCH refers to a different object than JOE.BRUNCH in your current session. You've got a couple of options to fix that.
Create a public synonym. This will allow any user that has privileges on the JOE.BRUNCH table to access it by querying BRUNCH
CREATE PUBLIC SYNONYM brunch
FOR joe.brunch
Create a private synonym. This will allow just the current user to access the JOE.BRUNCH table by querying BRUNCH
CREATE SYNONYM brunch
FOR joe.brunch
Change the current schema for the current session to JOE. This will cause all unqualified references in the current session to resolve to the JOE schema rather than to the current user's schema
ALTER SESSION SET current_schema = JOE
There are several possible causes
1) there is more than one object (table,view, procedure, etc) called brunch. Oracle does not know which one you are referring to.
2) most likely cause: the table exists in the joe schema but you are connecting as another user who has not been granted select on the joe.brunch object
Try
Grant select on joe.brunch to your_user
and try this and see how many objects match the name brunch
select *
from all_objects
where object_type in (‘TABLE’,'VIEW’)
and object_name = ‘brunch‘;
I found that the table I was referencing (Flyway's schema_version table), was created with double quotes... and thus needed double quotes wherever it was referenced.
Here's what Oracle says:
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.
In practice, these worked:
SELECT * FROM MYSCHEMA."schema_version";
SELECT * FROM "MYSCHEMA"."schema_version";
When this didn't (-> ORA-00942: table or view does not exist):
SELECT * FROM MYSCHEMA.schema_version;

Resources