I use Quest TOAD for Oracle and Quest SQL Navigator for Oracle as my database
query tools of choice.
These tools allow me to export the query results grid as INSERT statements.
For example
SELECT dummy
FROM dual;
exports to
INSERT INTO dual
(DUMMY)
VALUES
('X')
/
Is there an Oracle database query tool that exports query results as UPDATE statements?
For example
SELECT dummy
FROM dual;
would export to
UPDATE dual
SET dummy = 'X'
/
Try this one: http://www.sql-workbench.net
I don't know if there are other tools (wasn't able to find one, to be precise).
What I used to do for this to export the data as CSV, then hack a quick awk script to generate the desired UPDATEs.
You can export the Insert query first, insert into a backup table. Then update the target table with the backup table with PK.
UPDATE (SELECT tr.id,
tr.name a,
tr.desc b,
bk.name A,
bk.desc B
FROM target tr,
backup bk
WHERE tr.id = bk.id)
SET a = A,
b = B
Related
I created a table in oracle like
CREATE TABLE suppliers AS (SELECT * FROM companies WHERE id > 1000);
I would like to know the complete select statement which was used to create this table.
I have already tried get_ddl but it is not giving the select statement. Can you please let me know how to get the select statement?
If you're lucky one of these statements will show the DDL used to generate the table:
select *
from gv$sql
where lower(sql_fulltext) like '%create table suppliers%';
select *
from dba_hist_sqltext
where lower(sql_text) like '%create table%';
I used the word lucky because GV$SQL will usually only have results for a few hours or days, until the data is purged from the shared pool. DBA_HIST_SQLTEXT will only help if you have AWR enabled, the statement was run in the last X days that AWR is configured to hold data (the default is 8), the statement was run after the last snapshot collection (by default it happens every hour), and the statement ran long enough for AWR to think it's worth saving.
And for each table Oracle does not always store the full SQL. For security reasons, DDL statements are often truncated in the data dictionary. Don't be surprised if the text suddenly cuts off after the first N characters.
And depending on how the SQL is called the case and space may be different. Use lower and lots of wildcards to increase the chance of finding the statement.
TRY THIS:
select distinct table_name
from
all_tab_columns where column_name in
(
select column_name from
all_tab_columns
where table_name ='SUPPLIERS'
)
you can find table which created from table
I have written a program using pyspark to connect to oracle database and fetch data. Below command works fine and returns the contents of the table:
sqlContext.read.format("jdbc")
.option("url","jdbc:oracle:thin:user/password#dbserver:port/dbname")
.option("dbtable","SCHEMA.TABLE")
.option("driver","oracle.jdbc.driver.OracleDriver")
.load().show()
Now I do not want to load the entire table data. I want to load selected records. Can I specify select query as part of this command? If yes how?
Note: I can use dataframe and execute select query on the top of it but I do not want to do it. Please help!!
You can use subquery in dbtable option
.option("dbtable", "(SELECT * FROM tableName) AS tmp where x = 1")
Here is similar question, but about MySQL
In general, the optimizer SHOULD be able to push down any relevant select and where elements so if you now do df.select("a","b","c").where("d<10") then in general this should be pushed down to oracle. You can check it by doing df.explain(true) on the final dataframe.
I'd like to run statements like
SELECT date_add('2008-12-31', 1) FROM DUAL
Does Hive (running on Amazon EMR) have something similar?
Best solution is not to mention table name.
select 1+1;
Gives the result 2. But poor Hive need to spawn map reduce to find this!
Not yet: https://issues.apache.org/jira/browse/HIVE-1558
To create a dual like table in hive where there is one column and one row you can do the following:
create table dual (x int);
insert into table dual select count(*)+1 as x from dual;
Test an expression:
select split('3,2,1','\\,') as my_new_array from dual;
Output:
["3","2","1"]
There is a nice working solution (well, workaround) available in the link, but it is slow as you might imagine.
The idea is that you create a table with a dummy field, create a text file whose content is just 'X', load that text into that table. Viola.
CREATE TABLE dual (dummy STRING);
load data local inpath '/path/to/textfile/dual.txt' overwrite into table dual;
SELECT date_add('2008-12-31', 1) from dual;
Hive does support this function now and also does support many other dates function as well.
You can run query like below in hive, which will add days the provided date in first argument.
SELECT DATE_ADD('2019-03-01', 5);
Hive Date Functions
Quick Solution:
We can use existing table to achieve dual functionality by following query.
SELECT date_add('2008-12-31', 1) FROM <Any Existing Table> LIMIT 1
For example:
SELECT CONCAT('kbdjj','56454') AS a, null AS b FROM tbl_name LIMIT 1
Result
"limit 1" in query is used to avoid multiple occurrences of specified values (kbdjj56454,null).
I have been using SQL Server 2008 for a short time now and have never used Oracle before. I am able to access an Oracle table through SQL Server with the syntax
select * from [OracleDB1]..[OracleDB1].[Zips]
(where OracleDB1 is the oracle database and Zips is the table I require)
Is it possible to join a SQL Server table with this one in a Table-valued Function? Just using a normal join as I would with SQL Server tables gives an Invalid object name error on the Oracle table.
Can this be done directly (or at all) or is it possible to do this some other way such as table variables?
example query:
select * from dbo.Table1 t INNER JOIN [OracleDB1]..[OracleDB1].[Zips] z where t.zip = z.zip
I was performing the join wrong since I missed the ON clause. I was able to get it to work by declaring a temptable and joining on that.
declare #tempTable table{
ZIP nvarchar(5),
COUNTY nvarchar(10)
}
insert #tempTable select ZIP, COUNTY, from [OracleDB1]..[OracleDB1].[ZIPS]
select * from dbo.Table1 t INNER JOIN #tempTable z on t.ZIP = v.ZIP where t.AdmissionOn >= '08-08-2011' AND t.AdmissionOn <= ''09-08-2011'
This also worked in line as I had in the original question once I added the ON clause but the table variable suits my needs better since it only has to access the Oracle table once and not each comparison.
In Oracle's SQL*Plus, the results of a SELECT are displayed in a tabular manner. Is there a way to display a row in a key-value manner (like MySQL's \G option)?
The database I am working on (the schema is not defined by me) has a bunch of columns named e.g. YN_ENABLED (YN = yes no) which are CHAR(1). So when I do a query I get a result like
ID_MYTABLE Y Y Y
------------ - - -
3445 Y N Y
So it's not really clear which columns have which values (without having the schema open in another window).
Not built in to SQL PLus, but Tom Kyte has provided a procedure called print_table that does this. You would run it like this:
SQL> exec print_table ('select * from mytable where id_mytable=123');
And see results like:
ID_MYTABLE : 123
YN_ENABLED : Y
YN_SOMETHING : N
...
I know your question is about SQL*PLus, but you might be interested to know that Oracle's SQL Developer can do this. The feature can be used by right clicking on the Query Results and selecting "Single Record View...".
Here's a trick that may do in a pinch if you don't want to (or cannot) install a new procedure on your server:
Select the row (or rows) of interest in the Oracle SQL Developer
query results window.
Use shift-control-c in Oracle SQL Developer to copy the rows and
headers to the clipboard.
Paste into your favorite spreadsheet
(e.g., MS Excel). Now you have records in rows.
Copy the rows that you just pasted into the spreadsheet
Use the "Paste Special - Transpose" feature of your spreadsheet program to paste the values into a new spreadsheet. Now your records should be in columns.
Coming in late, but I found this
SQL> select * from xmltable('ROWSET/ROW/*' passing xmltype(cursor(select * from emp where rownum = 1
)) columns name varchar2(30) path 'node-name(.)', value varchar2(30) path '.');
Found here