How to get column names in an oracle select statement - oracle

To get the column names in a mysql or mssql connection I'm able to do the following:
>>> cursor.execute('select * from table)
>>> [item[0] for item in cursor.description]
[u'provider', u'title', u'date', u'apple_id', u'country', u'genre', u'sales_in_usd']
How would I get the column names from an Oracle cursor?

The code you have above works just fine with cx_Oracle (the driver that enables access to Oracle databases) since it follows the Python Database API!

Related

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>";

Joining 2 Oracle tables using Join in SSIS

I have to get data from database1 something like
select *
from tab1
where dtCol > <*dtVariable*>
The value of dtVariable comes from database2 from
select *
processdt
where proc_name = 'PROCESS1'
Can you please let me how to do this?
I am using SSIS 2008
Oracle db - 12c
Oracle Drivers - Attunity 1.2
No DBLInks ( basically, we need to avoid using db links)
You would simply need to pull the input value from Db2 to a variable in SSIS as step 1.
Step 2, you would create an expression that forms that SELECT sql call encompassing that variable.
MSDN has an article how to use Execute SQL Task.

How to read a table and Sql query from Oracle in Pandas?

I am completely new to Python and pandas. I want to load a some tables and Sql Queries from Oracle and Teradata to pandas Dataframes and want to analyse them.
I know, we have to create some connection strings to Oracle and Teradata in Pandas. Can you please suggest me them and also add the sample code to read both table and SQL query in that?
Thanks Inadvance
I don't have Oracle server, so I take Teradata as an example
This is not the only way to to that, just one approach
Make sure you have installed Teradata ODBC Driver. Please refer to Teradata official website about the steps, I suppose you use Windows (since it is easy to use SQL Assistant to run query against Teradata, that is only on Windows). You can check it in ODBC Data Source Administrator
Install pyodbc by the command pip install pyodbc. Here is the official website
The connection string is db_conn_str = "DRIVER=Teradata;DBCNAME={url};UID={username};PWD={pwd}"
Get a connection object conn = pyodbc.connect(db_conn_str)
Read data from a SQL query to a DataFrame df = pd.read_sql(sql="select * from tb", con=conn)
The similar for Oracle, you need to have the driver and the format of ODBC connection string. I know there is a python module from Teradata which supports the connection too, but I just prefer use odbc as it is more generic purpose.
Here is an Oracle example:
import cx_Oracle # pip install cx_Oracle
from sqlalchemy import create_engine
engine = create_engine('oracle+cx_oracle://scott:tiger#host:1521/?service_name=hr')
df = pd.read_sql('select * from table_name', engine)
One way to query an Oracle DB is with a function like this one:
import pandas as pd
import cx_Oracle
def query(sql: str) -> pd.DataFrame:
try:
with cx_Oracle.connect(username, password, database, encoding='UTF-8') as connection:
dataframe = pd.read_sql(sql, con=connection)
return dataframe
except cx_Oracle.Error as error: print(error)
finally: print("Fetch end")
here, sql corresponds to the query you want to run. Since it´s a string it also supports line breaks in case you are reading the query from a .sql file
eg:
"SELECT * FROM TABLE\nWHERE <condition>\nGROUP BY <COL_NAME>"
or anything you need... it could also be an f-string in case you are using variables.
This function returns a pandas dataframe with the results from the sql string you need.
It also keeps the column names on the dataframe

Run a Query from Linked Server (Oracle) in SQL Server2008 R2

I have the linked server set up in SQL Server 2008. But I could not run any query against the linked server.
I tried to run this simple command but it's not working
SELECT * FROM MYSERVER..ALANH.TEMP_UPDATE1
This is the error I got when I run the above command.
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "OraOLEDB.Oracle" for linked server "MYSERVER" reported an error. The provider did not give any information about the error.
Msg 7312, Level 16, State 1, Line 1
Invalid use of schema or catalog for OLE DB provider "OraOLEDB.Oracle" for linked server "MYSERVER". A four-part name was supplied, but the provider does not expose the necessary interfaces to use a catalog or schema.
Could anyone help me to connect to the OracleLinkedServer? Thanks very much.
you can be that way too:
**SELECT * FROM OPENQUERY(MYSERVER, 'SELECT * FROM ALANH.TEMP_UPDATE1')**
You can write the query like this:
select * FROM [MYSERVER]..[ALANH].[TEMP_UPDATE1]
Important: In this case, the fully qualified table name must be written in upper case.
You might try the fix from this article.
Also, this could be a problem with naming. From an MS KB article
If you receive these error messages, a table may be missing in the
Oracle schema or you may not have permissions on that table. Verify
that the schema name has been typed by using uppercase. The
alphabetical case of the table and of the columns should be as
specified in the Oracle system tables.
On the Oracle side, a table or a column that is created without double
quotation marks is stored in uppercase. If the table or the column is
enclosed in double quotation marks, the table or the column is stored
as is.
First make sure the tnsping utility works from client server, then use the below string in linked server database source setup
host[:port]/service_name
Check this link for more details :
http://www.oracledistilled.com/oracle-database/oracle-net/using-easy-connect-ezconnect-naming-method-to-connect-to-oracle-databases/
Try something like this:
SELECT * FROM ALL_TABLES#"SOME.SERVER.NAME";
In this case I'm selecting ALL_TABLES from a linked server called SOME.SERVER.NAME.
Richard's post above was critical.
I am using v12 ODP.NET odbc drivers and had to ensure that "Zero Level Only" was NOT checked and that the names supplied for table and schema were the correct case. All of the schemas and tables I access are uppercase only.
Use the query below to determine what the correct table name is, though you will have to supply the schema name in the correct case for the query to work. Try all uppercase, try all lowercase, try mixed case, or better yet get the actual name from the dba (I've heard that only table/schema names that are "" quoted will be allowed mixed case, otherwise in oracle it's all uppercase.)
sp_tables_ex #table_server=InsertLinkedServerHere, #table_schema=InsertSchemaNameHere

SAS to Oracle ODBC - passing a SAS table INTO the database

Can anyone please advise the syntax for passing a table FROM a SAS library INTO an oracle database?
example code below (although obviously the connection to the WORK library cannot be referenced in this way)
PROC SQL noprint;
connect to ODBC as X (dsn='ALIAS' uid='USER1' pwd='passwd' quote_char='');
exec (CREATE TABLE Test AS
SELECT * from WORK.MY_SAS_TABLE
)by X;
disconnect from X;
quit;
A similar question was asked here but seems to relate to a SQLSERVER connection rather than oracle..
Set up a libref to point to your Oracle database, either using the ODBC libname engine or the Oracle libname engine (which will be faster if you have the right licence and software installed):
libname X oracle username='USER1' password='passwd' path=ORCL;
If an empty table with the right columns already exists in Oracle, you can use:
proc sql noprint;
insert into X.test select * from work.my_sas_table;
quit;
If the table doesn't exist, you can use a data step:
data X.test;
set work.my_sas_table;
run;
I'm a bit rusty, but what if you set up your database as a libref?
Something like:
libname X odbc dsn='ALIAS' uid='USER1' pwd='passwd' quote_char='';
data X.test;
set work.my_sas_table;
run;

Resources