Is it possible to query multiple schemas in Linqpad with the IQ connection driver for oracle?
E.G. I have a user that can see two schemas ,users and customers. Currently I have to specify the schema I want to use when setting up the connection. So, I can only query one schema per session. It would be great if I could do something like the following:
var customers = from c in *customers*.sometable
select c;
var users = from u in *users*.sometable
select u;
...
in a single session. Is such a thing possible with any existing oracle drivers in LINQPad?
This cannot be done at the moment: when the driver builds a typed datacontext, it creates objects for just one schema. Why don't you register a request in linqpad.uservoice.com - a few other people have asked about this.
Related
I'm using Oracle SQL Developer version 4.02.15.21.
I need to write a query that accesses multiple databases. All that I'm trying to do is get a list of all the IDs present in "TableX" (There is an instance of Table1 in each of these databases, but with different values) in each database and union all of the results together into one big list.
My problem comes with accessing more than 4 databases -- I get this error: ORA-02020: too many database links in use. I cannot change the INIT.ORA file's open_links maximum limit.
So I've tried dynamically opening/closing these links:
SELECT Local.PUID FROM TableX Local
UNION ALL
----
SELECT Xdb1.PUID FROM TableX#db1 Xdb1;
ALTER SESSION CLOSE DATABASE LINK db1
UNION ALL
----
SELECT Xdb2.PUID FROM TableX#db2 Xdb2;
ALTER SESSION CLOSE DATABASE LINK db2
UNION ALL
----
SELECT Xdb3.PUID FROM TableX#db3 Xdb3;
ALTER SESSION CLOSE DATABASE LINK db3
UNION ALL
----
SELECT Xdb4.PUID FROM TableX#db4 Xdb4;
ALTER SESSION CLOSE DATABASE LINK db4
UNION ALL
----
SELECT Xdb5.PUID FROM TableX#db5 Xdb5;
ALTER SESSION CLOSE DATABASE LINK db5
However this produces 'ORA-02081: database link is not open.' On whichever db is being closed out last.
Can someone please suggest an alternative or adjustment to the above?
Please provide a small sample of your suggestion with syntactically correct SQL if possible.
If you can't change the open_links setting, you cannot have a single query that selects from all the databases you want to query.
If your requirement is to query a large number of databases via database links, it seems highly reasonable to change the open_links setting. If you have one set of people telling you that you need to do X (query data from a large number of tables) and another set of people telling you that you cannot do X, it almost always makes sense to have those two sets of people talk and figure out which imperative wins.
If we can solve the problem without writing a single query, then you have options. You can write a bit of PL/SQL, for example, that selects the data from each table in turn and does something with it. Depending on the number of database links involved, it may make sense to write a loop that generates a dynamic SQL statement for each database link, executes the SQL, and then closes the database link.
If you want need to provide a user with the ability to run a single query that returns all the data, you can write a pipelined table function that implements this sort of loop with dynamic SQL and then let the user query the pipelined table function. This isn't really a single query that fetches the data from all the tables. But it is as close as you're likely to get without modifying the open_links limit.
Is there a way to find the details of the databases accessing our Oracle database using the DB links? Dba_db_links holds the information about the DB links that we have in our database to access other databases, but is there a similar kind of table from where we can find the DB links accessing our database or is that getting recorded some where?
Thanks in Advance.
I agree with Justin, that there's no way to determine an explicit list of all databases that have database links into a given database.
However, it is possible to monitor active database links. You can use the following query to see what sessions are via database links and from which databases:
-- who is querying via dblink?
-- Courtesy of Tom Kyte, via AskTom
-- this script can be used at both ends of the database link
-- to match up which session on the remote database started
-- the local transaction
-- the GTXID will match for those sessions
-- just run the script on both databases
Select /*+ ORDERED */
substr(s.ksusemnm,1,10)||'-'|| substr(s.ksusepid,1,10) "ORIGIN",
substr(g.K2GTITID_ORA,1,35) "GTXID",
substr(s.indx,1,4)||'.'|| substr(s.ksuseser,1,5) "LSESSION" ,
s2.username,
substr(
decode(bitand(ksuseidl,11),
1,'ACTIVE',
0, decode( bitand(ksuseflg,4096) , 0,'INACTIVE','CACHED'),
2,'SNIPED',
3,'SNIPED',
'KILLED'
),1,1
) "S",
substr(w.event,1,10) "WAITING"
from x$k2gte g, x$ktcxb t, x$ksuse s, v$session_wait w, v$session s2
where g.K2GTDXCB =t.ktcxbxba
and g.K2GTDSES=t.ktcxbses
and s.addr=g.K2GTDSES
and w.sid=s.indx
and s2.sid = w.sid;
Hope that helps.
When you create a database link in database A that points at database B, there is no notification sent to database B so there is no data dictionary table in B that will tell you that A has a link to it. As far as B is concerned, database A is simply another client that periodically opens a connection to the database.
Generally, when A wants to create a database link to B, a user will be created in B for this purpose (assuming the database link uses a fixed user rather than the current user) since you don't want the password for this account to expire regularly and you don't want the database link to be broken if a particular human leaves the company and has his or her accounts removed. You can audit connections on B, either for the particular accounts that have been created for database links or across all users, and then look through the audit logs to identify connections that are coming from servers that house other databases.
You might be looking out for this.
Step 1: Check the hash_value of the session in X database.
select sql_hash_value from v$session where sid=&sid;
Step 2: Check the full SQL of the session in X database where the SQL is fired.
select sql_fulltext from v$sql where hash_value=&hash_value;
Step 3: Make a note of all the DB links invloved in the SQL and identify the hosts for those DB links.
select * from dba_db_links where db_link like upper('&db_link');
Step 4: In each host (say only one remote host, pointing to database Y) and database X itself, fire the above query (Tom Kyte's) to gather the session details of sessions coming from remote DBs.
Step 5: In database X, check the SID of interest and its corresponding GTXID. Look for the same GTXID in the remote host Y.
Step 6: Get the session ID from the database Y for this GTXID and check the session waits or other details.
I have 2 connections with different tables in sqldeveloper.
let's say:
ConnectionA with tables: A,B,C
ConnectionB with tables: D,E,F
Now I want to have a query that looks like this:
select aa.name,dd.id
from A aa,D dd;
How can i do this?
If you want to query objects in two different databases using a single SQL statement, you would need to create a database link between the two databases. A database link is an object that resides in the database and is independent of the query tool. In database A, for example, you could create the database link
CREATE DATABASE LINK to_b
CONNECT TO username IDENTIFIED BY password
USING tns_alias_on_a_pointing_to_b
And then when you connect to A, you could do something like
SELECT aa.name, dd.id
FROM a aa,
d#to_b dd
WHERE aa.some_key = dd.some_key
Apparently TOAD Data Point supports Cross-Connection Queries , see:
http://dev.toadfordataanalyst.com/webhelp/Content/Query_Builder/Create_CrossConnection_Queries.htm
Also Oracle SQL Developer seems to support something similar. (see this blog post: Cross Connection Queries)
I found this helpful and to the point of the OP question for Oracle 11g rel 2 and later: http://www.dba-oracle.com/t_how_create_database_link.htm . Basically, right-click on the connection in the Connections pane in SQL Developer, click Properties, and you get the hostname, port, and service name that you can plug into the "USING" part of the CREATE DATABASE LINK statement. Whether you put in Service Name or SID I assume depends on which you used in your connection. example:
create public database link
mylink
connect to
remote_username
identified by
mypassword
using 'myserver:1521/MYSID';
I need to take information from two different data bases.
select * from TABLE_ONDB2 where column_on_db2 in ( select column_on_db1 from TABLE_ONDB1 );
Problem is both are on different db instances so I am not able to figure out how to put table names and column names etc.
I hope my question is clear.
I'd try to do it with a Database Link:
http://download.oracle.com/docs/cd/B28359_01/server.111/b28310/ds_concepts002.htm
That is, however, not a SQL*Plus feature. It works by makeing a connection from DB2 to DB1 (the database is doing that).
You can then query both tables from DB2 with the '#db-link' name notation. e.g.,
select *
from TABLE_ONDB2
where column_on_db2
in (select column_on_db1 from TABLE_ONDB1#DB_LINK_NAME);
^^^^^^^^^^^^^
The benefit is that you can access the table in all different ways, also as a join.
does statement object contain the session id the database returns for the current session? What does a resultset
contain?
To the best of my knowledge, no, Statements do not have session IDs. It seems like the Java API specifications for the Statement class backs that up. Basically, Statements are used to execute SQL statements by specifying a SQL query through the execute method.
A ResultSet is used to retrieve results which are returned by executing a query via a Statement or PreparedStatement.
The JDBC(TM) Database Access trail of The Java Tutorials contains some information on these topics. The following sections may be of interest:
Lesson: JDBC Basics
Updating Tables
Retrieving Values from Result Sets