Sqlite update query with table shotcuts - sqlite

I am getting problem for UPDATE query in sqlite .
UPDATE Table1 T1, Table2 T2 SET T1.USE_MHE = T2.USE_MHE WHERE T1.EQ_NAME= T2.EQ_NAME
Above query works fine for MS access ADO connection.
But for SQLite it's looks like using table shortcut is not possible (Table1 T1) I have 100's of such query to update. Please let me know how table shortcut can be used in SQLite update query.,

Your problem is not aliases that you're using. Your UPDATE clause is wrong. Try:
UPDATE
Table1 AS T1
SET
T1.USE_MHE = (SELECT T2.USE_MHE FROM Table2 AS T2 WHERE T1.EQ_NAME = T2.EQ_NAME)
WHERE
EXISTS(SELECT * FROM Table2 AS T2 WHERE T1.EQ_NAME = T2.EQ_NAME);

Related

bulk update in SQLite

I have 2 tables with identical structure I want to update one table using data from the other, matching on primary key. SQLite has a with (CTE) statement but the following doesn't work (sqlite3 v. 3.29.0):
sqlite> select * from main;
1|A
2|B
4|D
5|E
6|F
sqlite> select * from temp;
1|aa
2|bb
3|cc
4|dd
5|ee
sqlite> with mapping as (select main.ID, temp.Desc from main join temp on temp.ID=main.ID) update main set Desc=mapping.Desc where main.ID=mapping.ID;
Error: no such column: mapping.Desc
I've tried using "select main.ID as ID, temp.Desc as Desc", but get the same error message.
To update your main table from your cte, use a subquery, since sqlite doesn't support update from
with mapping as
(select main.ID, temp.Desc
from main
join temp on temp.ID=main.ID)
update main set Desc=
(select Desc from mapping where ID = main.ID limit 1);
see dbfiddle

SQLite: updating column from another table

I have a old table and a new table. what i need is to copy the uuId of the old table to the new Table.
im following some answers from other references but i can`t get the ideal answer.
the closest answer i found is this:
update table1
set table1.uuid =
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription)
when i execute this query, it only saves the 1st found uuid of the old table to all the entry in the new Table.
Sample Table2 (old table):
uuid|itemDescription
1|item1
2|item2
3|item3
Sample Table1 (new Table):
uuid|itemDescription
Null|item1
Null|item2
Null|item3
Desired Output:
uuid|itemDescription
1|item1
2|item2
3|item3
what happens:
uuid|itemDescription
1|item1
1|item2
1|item3
In SQLite, you must not use the table name in the SET clause:
update table1
set uuid =
(select table2.uuid from table2 where table1.itemDescription = table2.itemDescription);
Try like this :
UPDATE table1
SET table1.uuid = table2.uuid
FROM table2 WHERE table1.itemDescription = table2.itemDescription
No need for subquery, otherwise you need to link item description in outer query also

How to query NOT EXISTS on the same table?

How to query NOT EXISTS on the same table?
I need to create the select to be able to perform the insert with the results. The idea is to only insert those records that do not exist in the table.
The keys are t1.tie_id = T2.tie_id AND t1.org_id = t2.org_id.
Currently in T1 I have a single record, which should put the remaining records.
SELECT
101 tie_id,
org_id,
tie_org_orden,
tie_org_activo,
tie_org_default
FROM
table1 T1
WHERE
NOT EXISTS (
SELECT
1
FROM
tabla2 T2
WHERE
t1.tie_id = T2.tie_id
AND
t1.org_id = t2.org_id
)
AND
t1.tie_id = 42 and t1.org_id = 181
you can use alias() like in Usage of "aliased" in SQLAlchemy ORM for aliasing the 2nd time you use the table and the WHERE NOT EXITS has sqlalchemy logic eg in Using NOT EXISTS clause in sqlalchemy ORM query

How can I see the last SQL statement executed in Oracle database 11g r2?

I am new to oracle database.
Can someone give me an example of the steps for how to see the last statements executed on the Oracle database 11g r2?
You can use the below query to get the last sql executed based on last sql which was active in database
select ltrim(sq.sql_text)
from v$sql sq, v$session se, v$open_cursor oc
where sq.sql_id = oc.sql_id
and se.saddr = oc.saddr
and se.sid = oc.sid
and se.audsid = SYS_CONTEXT('userenv', 'sessionid')
order by oc.LAST_SQL_ACTIVE_TIME desc;
You can also use the below to find the last query executed in your session.
SELECT (SELECT t2.sql_fulltext
FROM v$sql t2
WHERE t1.prev_sql_id = t2.sql_id
AND t1.prev_child_number = t2.child_number) sql_fulltext
FROM v$session t1
WHERE t1.audsid = Sys_context('userenv', 'sessionid');
You can use the below query:
SELECT program_id, program_line#, sql_text
FROM V$SQL VS , ALL_USERS AU
WHERE (executions >= 1)
AND (parsing_user_id != 0)
AND (AU.user_id(+) = VS.parsing_user_id)
AND UPPER(AU.USERNAME) IN (UPPER('YourUser'))
ORDER BY last_active_time DESC;
if you need to know the statements of an PL/SQL object were executed then use or join with
select *
from dba_objects
where object_id = program_id
Find all sql where sql is like ....
select h.sample_time
, u.username
, h.machine
, s.sql_text
, h.*
from dba_hist_active_sess_history h
inner join v$sql s
on s.sql_id = h.sql_id
left outer join dba_users u
on u.user_id = h.user_id
where s.sql_text like 'DELETE%'
order by h.sample_time desc;
You need to be connected as sysdba user for this sql
A couple of hints:
In SQLplus, type a semicolon+ to see, and slash to execute again
In SQLdeveloper, use F8
If you mean see other users' statements then it's not possible by default.
You can configure AUDIT.
You can see some SQL statements in SELECT * FROM V$SQLAREA;
Connect as SYS user and execute the following query
select sql_text from v$sql where first_load_time=(select max(first_load_time) from v$sql) ;
select sq.PARSING_SCHEMA_NAME, sq.LAST_LOAD_TIME, sq.ELAPSED_TIME, sq.ROWS_PROCESSED, ltrim(sq.sql_text)
from v$sql sq, v$session se
where sq.PARSING_SCHEMA_NAME = 'YOUR_SCHEMA'
order by sq.LAST_LOAD_TIME desc;

How to get list of all the tables in sqlite programmatically

How can I get the list of all the available tables in sqlite programmatically?
try this :
SELECT * FROM sqlite_master where type='table';
Use the below sql statement to get list of all table in sqllite data base
SELECT * FROM dbname.sqlite_master WHERE type='table';
The same question asked before on StackOverFlow.
How to list the tables in an SQLite database file that was opened with ATTACH?
worked for me
SELECT * FROM sqlite_master where type='table'
con = sqlite3.connect('db.sqlite')
cur = con.cursor()
cur.execute('''
SELECT tbl_name
FROM sqlite_master
WHERE type = 'table';
''')
print(cur.fetchall())

Resources