Database Link creates more than 1 session - database

We have two Oracle databases
1. 11.2.0.4
2. 12.1.0.2
We create a Dblink from_11_to_12.
I connect via sqlplus to both databases and issue the commands below, step-by-step, first at 12c , second at 11g and so on:
At 12c
SQL> select sid,serial# from v$session where username = 'REP'; --Step 1
no rows selected
SQL> select sid,serial# from v$session where username = 'REP'; --Step 3
SID SERIAL#
---------- ----------
11 53421
17 28325
37 22230
453 23175
462 48252
SQL> select sid,serial# from v$session where username = 'REP'; --Step 5
SID SERIAL#
---------- ----------
11 53421
17 28325
378 55283
453 23175
462 48252
SQL> select sid,serial# from v$session where username = 'REP'; --Step 7
SID SERIAL#
---------- ----------
11 53421
17 28325
378 2986
453 23175
at 11g
SQL> select id from table1#from_11_to_12 where rownum <1; --Step 2
no rows selected
SQL> commit; --Step 4
Commit complete.
SQL> alter session close database link from_11_to_12; --Step 6
Session altered.
As you can see even after Step 6, still there are session at remonte database.
And more than that, why the connection creates 5 sessions?
How can I close those remote sessions. We work with shared servers, so if many people connect at the same time there will be at least 4 sessions busy.

Related

update not working during batch insert in TDengine database

I created a database with update=1, so that I can update some records based on the timestamp. but I found the update is not working when I use batch insert. I also tried normal insert, updating record is working.
Welcome to the TDengine shell from Linux, Client Version:2.1.7.2
Copyright (c) 2020 by TAOS Data, Inc. All rights reserved.
taos> create database test update 1;
Query OK, 0 of 0 row(s) in database (0.007977s)
taos> use test;
Database changed.
taos> create table tb(ts timestamp, c1 int);
Query OK, 0 of 0 row(s) in database (0.015282s)
taos> insert into tb values(now, 1)(now, null);
Query OK, 1 of 1 row(s) in database (0.000797s)
taos> select * from tb;
ts | c1 |
========================================
2021-09-28 11:37:32.339 | 1 |
Query OK, 1 row(s) in set (0.002671s)
taos> insert into tb values("2021-09-28 11:37:32.339", null);
Query OK, 1 of 1 row(s) in database (0.000611s)
taos> select * from tb;
ts | c1 |
========================================
2021-09-28 11:37:32.339 | NULL |
Query OK, 1 row(s) in set (0.002591s)
what is the difference between batch insert and normal insert in TDengine?
Batch insertion uses the same "now". It leads two records to use the same timestamp which is not possible to be a valid record as the time-series database needs each record to use a different timestamp.

How do I delete rows in one table where the ID matches another table row where a field is a certain value?

I admit the title question is convoluted; here is the situation.
I have two tables:
USERS Table
id
name
status
1
Monica
A
2
Ross
A
3
Phoebe
T
4
Chandler
A
5
Rachel
T
6
Joey
A
PERMISSIONS Table
user_id
permission_id
1
32
1
51
4
12
6
2
3
5
5
22
2
18
What I want is a way to delete all rows from the PERMISSIONS table where that user's STATUS is "T" but how would I do that?
I had tried this:
DELETE FROM permissions
WHERE user_id IN (
SELECT id FROM users
WHERE status = 'T'
);
However, SQL Server gives this error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
try apply join permissions and user and where status = 'T'
Example:
DELETE p
FROM permissions p
INNER JOIN users u
ON p.user_id=u.id
WHERE u.status = 'T'

How to find which user made the last modification of a table row

let's say schema user1 has a table called user1_table and user2 has insert/update/delete grants on user1_table
I know how to find the last modification date/time as follows
SELECT scn_to_timestamp(ORA_ROWSCN)
FROM user1_table
WHERE id = 1;
Is there a way to find out which user (user1 or user2?) last modified that particular row?
In order to know who made the modifications, you need to enable audit. It requires a setup and you have options ( DB , OS )
To allow auditing on the server you must:
Set audit_trail = true
Run the $ORACLE_HOME/rdbms/admin/cataudit.sql script while connected as SYS.
A simple example would be
SQL> create table t ( c1 number , c2 number )
2 ;
Table created.
SQL> audit insert,update,delete on t by access ;
Audit succeeded.
You can also create a trigger to store in a table who made the modification by applying the SYS_CONTEXT properties
The example below is a basic one, you can expand the properties of the trigger to control updates and deletes as well.
SQL> create table t ( c1 number, c2 number ) ;
Table created.
SQL> create table t_audit ( who varchar2(40) , c1 number, c2 number ) ;
Table created.
SQL>
create or replace trigger trg_aud_t after update on t
for each row
declare
v_user varchar2(40);
begin
select sys_context('userenv','session_user') into v_user from dual;
insert into t_audit values ( v_user , :new.c1 , :new.c2 );
end;
/SQL> 2 3 4 5 6 7 8 9
Trigger created.
SQL> update t set c1=2 , c2=2 where c1=1 ;
1 row updated.
SQL> commit ;
Commit complete.
SQL> select * from t_audit ;
WHO C1 C2
---------------------------------------- ---------- ----------
SYS 2 2

Query to transfer data from one database to another

I have 2 database's and I need to transfer data from database 1 to database 2.
Only the data that in database 1 but not in database 2.
DB 1
----
MyTBL 1
-------
111
222
333
444
555
666
DB 2
----
MyTBL 2
-------
111
222
666
I need to transfer from 1 to 2.
It will looks like this:
DB 2
----
MyTBL 2
--------
111
222
333
444
555
666
I need SQL query, I work in SQL Server 2012
I tried a few things - but without success.
Guessing at your names:
INSERT DB2.dbo.MyTBL2
(DataColumn)
SELECT DataColumn
from DB1.dbo.MyTBL1
EXCEPT SELECT DataColumn
from DB2.dbo.MyTBL2
SELECT EXCEPT can be very powerful. More info at https://learn.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-except-and-intersect-transact-sql?view=sql-server-ver15
If both databases are on the same instance, merge is a reasonable solution for this. Assuming the schema for all objects is dbo, run the merge statement inside db2:
merge dbo.MyTBL2 t2
using db1.dbo.MyTbl1 t1 on t1.yourcolumn = t2.yourcolumn
when not matched then
insert (yourcolumn) values (t1.yourcolumn);

oracle multiple database link read only access error

The query is like this
select 1 from dual#link1
union
select 1 from dual#link2
then i got the read only access error.
I have users in all those 3 databases and they are all read only users, so when i do the query "select 1 from dual#link1" then i got read only error, then i tried to change the query as
set transaction read only;
select 1 from dual#link1;
then it's solved.
then i tried the query as below:
select 1 from dual#link1
union
select 1 from dual#link2
it error again, i am not sure why 1 link works but 2 links won't work.
any one knows?
It is quite a surprising limitation of having the database in read-only mode, and multiple dblinks involved.
When you have read from the first dblink, you should close the current transaction (!) and only then read from the second dblink. This obviously prevents you from having a SELECT that joins tables from both dblinks.
It is documented, but rather hard to find. In Starting Up and Shutting Down you can see:
When executing on a read-only database, you must commit or roll back any in-progress transaction that involves one database link before you use another database link. This is true even if you execute a generic SELECT statement on the first database link and the transaction is currently read-only.
To reiterate the concept, if you have access to metalink, there is an oracle note in response to a customer which shows an example (Document 1296288.1) clarifying the limitation
SQL> select * from emp#link_emp_chicago;
select * from emp#link_emp_chicago
*
ERROR at line 1:
ORA-16000: database open for read-only access
Solution
SQL> select open_mode,database_role from v$database;
OPEN_MODE DATABASE_ROLE
---------- ----------------
READ ONLY PHYSICAL STANDBY
SQL> select owner,db_link from all_db_links;
OWNER
------------------------------
DB_LINK
PUBLIC
LINK_EMP_CHICAGO
SQL> select * from emp#link_emp_chicago;
select * from emp#link_emp_chicago
*
ERROR at line 1:
ORA-16000: database open for read-only access
SQL> set transaction read only;
Transaction set.
SQL> select * from emp#link_emp_chicago;
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- --------- ---------- --------- ---------- ----------
DEPTNO
----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
Just to speculate, the reason may be related to the fact that, in the words of Tom Kyte
distributed stuff starts a transaction "just in case".

Resources