Query across two instances - sql

I have one Oracle Instance which homes a number of different Databases, I would like to query across all instances as some of the other database contain related information. How do I setup a query to connect to more that one instance as part of my SELECT statement..
I'm using Oracle SQL Developer if that helps.

Prior to Oracle 12.1 the relationship between Oracle Instances and Oracle Databases was 1 to 1. A single instance could house at most 1 database, though a single server could host multiple instances. However, a single database would have multiple schemas each of which could singly or cooperatively host 1 or more database applications.
Access from one Oracle Instance to another is possible via database links. Database links may be either public or private and may be created with commands similar to this (other options exist):
CREATE [PUBLIC] DATABASE LINK LINK_NAME
CONNECT TO SOME_SCHEMA
IDENTIFIED BY SOME_PASSWORD
USING 'SOME_SERVICE_NAME';
Such a DB Link would be used to reference DB objects in the remote instance by appending the link name to the object reference with an at (#) sign, for example:
SELECT * FROM [SCHEMA.]TABLE_NAME#LINK_NAME;
The above select would return data provided the remote schema associated with the DB link (SOME_SCHEMA in the above create db link statement) has sufficient privileges to select from the referenced remote schema.

Related

Query table with compound primary keys

I'm using pyodbc to connect to a machine database, and query a number of tables in that database using
pandas.read_sql(tbl,cnxn), where tbl = "SELECT * FROM TABLE", cnxn is pyodbc.connect('DSN=DATASOURCE;UID=USERID;PWD=PASSWORD').
It works on most tables, but some tables return:
DatabaseError: Execution failed on sql 'SELECT * FROM TABLE': ('42S02', '[42S02] [Microsoft][ODBC driver for Oracle][Oracle]ORA-00942: table or view does not exist (942) (SQLExecDirectW)')
These tables that return an error, when opened in MS Access, have multiple columns with a key icon on the left when opened in design view (thus a compound primary key, made up from multiple columns).
Is this is the reason I'm having the error described above? How can I solve this?
Edit: as shown in this screenshot, there are multiple columns marked as making up the primary key in design view:
Edit2:Thanks for the feedbacks. After checking ODBC Data Source Administrator window, this data source is on 32-bit platform, and its driver is Microsoft ODBC for Oracle.
I don't think table's name is the issue, because other tables worked and they have the same naming convention (table name is in this format NAME_OF_THE_TABLE). Trying to avoid showing the table name because working on a company project.
I did research the concept of primary key and realized that there can only be one for a table, but as shown in the screenshot attached, there are a five fields shows a key icon on the left.
Before anything, understand MS Access is a unique, GUI tool that maintains its own default database, JET/ACE Engine, but can connect to other databases as well including Oracle, SQL Server, Postgres, etc. via OLEDB/ODBC connections. Essentially, both MS Access and Python are doing the same thing: make an ODBC connection to Oracle (the actual backend database).
Because all linked tables connect fine in MS Access, try matching connections and queries in Python. Likely, the issue involves table names, schema connection, or user access.
Table Name: Your table contains misspellings or reserved words, or a mix of upper or lower cases as defined in their CREATE TABLE setup causing case sensitivity, so Table as defined with CREATE TABLE "Table" is not the same as TABLE. For this reason, use the exact name in the MS Access linked table and wrap with double quotes.
pandas.read_sql('SELECT * FROM "Table"', cnxn)
(Do note: double quotes in SQL is entirely different meaning than double quotes in Python and are not interchangeable with single quotes.)
Connected Schema/User: Incorrect schema. Because schemas in Oracle are more or less users, you may have multiple connections for your MS Access linked tables. Though they all point to same database server with same ODBC driver, the user differs each with different underlying tables. To resolve, match the Python ODBC connection with the MS Access ODBC connection:
You can locate the MS Access connection string under: Table Design (from Navigation Pane) > Property Sheet (from Ribbon) > Description. Use this in the pyodbc.connect(...) call. Likely only the uid and pwd would differ if working across schemas.
Unprivileged User: The connected user does not have select privilege on that table.

Data Migration Assistant - Cross-database queries

I am running an assessment using the Azure SQL Data Migration Assistant (3.4.3948.1). In my initial assessment, I had a function that was calling fn_varbintohexstr so I fixed it (read removed the function). I also deleted all our synonyms.
Now I run the assessment more times and it continues to give the 'cross-database queries' error but without listing any more specifics. How can I find out what particular objects it means? Or is it possible that it has somehow cached my result and I need to invalidate it somehow?
This means you have programming objects in that database that reference another database. For example a query like this:
SELECT * FROM Database1.dbo.Table1
One of the options you have is to import those external objects to your database and change the three and four-part name references that SQL Azure does not support.
You can also use CREATE EXTERNAL DATA SOURCE and CREATE EXTERNAL TABLE on SQL Azure to query tables that belong to other databases that you have to migrate to SQL Azure too.

Query table from another ORACLE database

I have two different data base, one is DEVORADB which i use for development, and another one is UATORADB which tester use for testing. UATORADB have the most updated data which is not in development. I want to query tables from UATORADB database in DEVORADB. I was writing in DEVORADB in such a way but not getting the result:
SELECT * FROM TABLE_NAME#UATDEVORADB.
For Oracle,
CREATE DATABASE LINK ...
e.g.
With a database link created and tested, you can do a query (of the style you showed) to retrieve rows from a remote database.
Reference: http://docs.oracle.com/cd/E11882_01/server.112/e41084/statements_5005.htm#SQLRF01205
FOLLOWUP
NOTE: In Oracle, the term "database" refers to the datafiles and logfiles associated with an Oracle "instance". To retrieve data from a second "database" means you need a second connection to the other database. Oracle provides a facility called a "database link". That allows a session(connection) to one database instance to connect to another database instance. (Without this facility, a client would need to create two separate connections, and would need to query the two databases separately.)
If this question is regarding querying from two separate "schemas" within the same database, as long as the user has sufficient privileges on objects in the second schema, the identifier can be qualified with the name of the schema, e.g.
SELECT * FROM UATDEVORADB.TABLE_NAME
To access data on a separate database, a database link can be used...
CREATE DATABASE LINK UADEVORADB
CONNECT TO user
IDENTIFIED BY password
USING 'uadevoradb' ;
(This will require an appropriate matching entry in the tnsnames.ora file on the Oracle server, or the oracle names server, or the connection details can be spelled out in place of a tnsnames.ora entry, something like:
CREATE DATABASE LINK UADEVORADB
CONNECT TO user IDENTIFIED BY password
USING '(DESCRIPTION=
(ADDRESS=(PROTOCOL=TCP)(HOST=uadevorahost1)(PORT=1521))
(CONNECT_DATA=(SERVICE_NAME=uadevoradb.domaindb)))'
If the "user" specified in the database link differs from the "owner" of the table on the remote system, and there's no synonym that references the table, the table identifier will need to be qualified with the owner...
SELECT * FROM OWNER.TABLE_NAME#UADEVORADB ;

Can we define procedure or select statement on more than one database?

I need some data of one db and some data of another db and perform some operation. Can we do this in sql server 2008? In my project suppose I want to fetch data of doctor and Chemist database in CRM Database and want to generate operation, How can we do that?
If both db belongs on the same sql server instance you can use full qualified names of objects like [DbName].[scheme].[table] if on different instances then you must create linked server. Of course, providing access as expected.

SQL Server 2005 Linked server not finding tables

I have a linked server where I can clearly see all the databases and tables, so I know the server is properly linked. However, when I try to execute a query, it says invalid object name, at the linked server's table.
The linked server is aliased as TCS, therefore, my query takes that table as
FROM [TCS].dbo.table as b
I have also tried including the database name also as FROM [TCS\db1].dbo.table.
What am I missing here?
Try including the DB name like so:
FROM [TCS].db1.dbo.table as b
I don't think you can specify the DB using a slash.
I would also check to make sure your security settings for the linked server are allowing your account to connect. This article touches on how to do that.
either:
the user (used for the link) doesn't have access to the table; Grant access;
the default DB on the server doesn't have the table. You have to change it to the relevant one or included in the db in the name: [TCS].DATABASE.dbo.table as b;