dbms_metadata bug if I try to generate ddl for java? - oracle11g

I have to copy some objects from one schema to another on the same database, between others java sources too. The dbms_metadata.get_ddl(object_type, object_name, schema_name) returns schema name in the ddl. Because I want to execute this ddl on the new schema the old schema name in the ddl doesn't help me in my job. To avoid this problem I use a following function a step before:
execute dbms_metadata.set_transform_param(dbms_metadata.session_transform,'EMIT_SCHEMA', false);
In case of a table it works (it means, it omits the schema name in ddl):
select dbms_metadata.get_ddl('TABLE', object_name, schema_name) from dual;
but in case of java source:
select dbms_metadata.get_ddl('JAVA_SOURCE', object_name, schema_name) from dual;
it doesn't!
I've tested these functions on VM with database 12.2 from Oracle too. The same behavior.
Is it a bug? Any workaround?
Regards,
Jacek

Related

sqlite ODBC driver and "attach" statement

I want to use the sqlite ODBC driver from http://www.ch-werner.de/sqliteodbc/ and start with an ATTACH statement, as I need joint data from two databases. However the driver returns no dataw when provided the following SQL:
attach 'my.db' as mydb; select 1
It however correctly complains with only one SQL statement allowed when the first statement is indeed a SELECT:
select 2;attach 'my.db' as mydb; select 1
Checking at the source, a checkddl() function analyzes if the provided requests contains DDL (Data Definition Language) statement. Before digging in the complete code, question is:
did someone manage to issue a select after an attach with this driver ?

The new line doesn't work when generating xml

let's suppose to have the table
create table mytable
(val1 number(5),
val2 varchar2(10));
insert into mytable values (1,'XXX');
and asked to generate the following XML
<ns1:head>
<ns1:val1>1</val1>
<ns1:val2>XXX</val2>
</ns1:head>
It's quite simple by running
select xmlelement("head",
xmlelement("val1",val1).extract('/*'),
xmlelement("val2",val2).extract('/*')
).extract('/*')
from mytable;
and to get
<head>
<val1>1</val1>
<val2>XXX</val2>
</head>
The problem is that if I try to do that way but for getting each node with "ns1:" in front of every tag
select xmlelement("ns1:head",
xmlelement("ns1:val1",val1).extract('/*'),
xmlelement("ns1:val2",val2).extract('/*')
).extract('/*')
from mytable;
I get an ORA-31011: XML parsing failed
Maybe I dont' konw how exactly the .extract('/*') works and in my case the "ns1:" could fails
Oracle version 10g
Thanks in advance!
Mark
What is the purpose of using extract('/*')?
Also a namespace prefix must be defined. You can add its declaration in the root element with xmlattributes.
select xmlelement("ns1:head", xmlattributes('http://www.example.com/ns1' as "xmlns:ns1"),
xmlelement("ns1:val1",val1),
xmlelement("ns1:val2",val2)
)
from mytable;
result:
<ns1:head xmlns:ns1="http://www.example.com/ns1"><ns1:val1>1</ns1:val1><ns1:val2>XXX</ns1:val2></ns1:head>
Update:
XMLELEMENT returns and XML object. If you want to get it as a formatted text, you can use XMLSERIALIZE.
select XMLSERIALIZE(document xmlelement("ns1:head", xmlattributes('http://www.example.com/ns1' as "xmlns:ns1"),
xmlelement("ns1:val1",val1),
xmlelement("ns1:val2",val2)
) indent)
from mytable;
result:
<ns1:head xmlns:ns1="http://www.example.com/ns1">
<ns1:val1>1</ns1:val1>
<ns1:val2>XXX</ns1:val2>
</ns1:head>
Update 2:
as I found out, indent instruction does not exist in Oracle 10g. So, if extract('/*') worked without namespaces, then adding xmlattributes to your original query with namespaces might work. I don't have Oracle 10g to test this.
select xmlelement("ns1:head", xmlattributes('http://www.example.com/ns1' as "xmlns:ns1"),
xmlelement("ns1:val1",val1).extract('/*'),
xmlelement("ns1:val2",val2).extract('/*')
).extract('/*')
from mytable;
Update 3:
Using extract('/*') for indenting lines looks like an undocumented feature. On Oracle 12c it doesn't work this way. So, updating Oracle version might break this undocumented behavior.

Export Multpile Tables Data as Insert Statements in to single file Oracle DB [duplicate]

The only thing I don't have an automated tool for when working with Oracle is a program that can create INSERT INTO scripts.
I don't desperately need it so I'm not going to spend money on it. I'm just wondering if there is anything out there that can be used to generate INSERT INTO scripts given an existing database without spending lots of money.
I've searched through Oracle with no luck in finding such a feature.
It exists in PL/SQL Developer, but errors for BLOB fields.
Oracle's free SQL Developer will do this:
http://www.oracle.com/technetwork/developer-tools/sql-developer/overview/index.html
You just find your table, right-click on it and choose Export Data->Insert
This will give you a file with your insert statements. You can also export the data in SQL Loader format as well.
You can do that in PL/SQL Developer v10.
1. Click on Table that you want to generate script for.
2. Click Export data.
3. Check if table is selected that you want to export data for.
4. Click on SQL inserts tab.
5. Add where clause if you don't need the whole table.
6. Select file where you will find your SQL script.
7. Click export.
Use a SQL function (I'm the author):
https://github.com/teopost/oracle-scripts/blob/master/fn_gen_inserts.sql
Usage:
select fn_gen_inserts('select * from tablename', 'p_new_owner_name', 'p_new_table_name')
from dual;
where:
p_sql – dynamic query which will be used to export metadata rows
p_new_owner_name – owner name which will be used for generated INSERT
p_new_table_name – table name which will be used for generated INSERT
p_sql in this sample is 'select * from tablename'
You can find original source code here:
http://dbaora.com/oracle-generate-rows-as-insert-statements-from-table-view-using-plsql/
Ashish Kumar's script generates individually usable insert statements instead of a SQL block, but supports fewer datatypes.
I have been searching for a solution for this and found it today. Here is how you can do it.
Open Oracle SQL Developer Query Builder
Run the query
Right click on result set and export
http://i.stack.imgur.com/lJp9P.png
You might execute something like this in the database:
select "insert into targettable(field1, field2, ...) values(" || field1 || ", " || field2 || ... || ");"
from targettable;
Something more sophisticated is here.
If you have an empty table the Export method won't work. As a workaround. I used the Table View of Oracle SQL Developer. and clicked on Columns. Sorted by Nullable so NO was on top. And then selected these non nullable values using shift + select for the range.
This allowed me to do one base insert. So that Export could prepare a proper all columns insert.
If you have to load a lot of data into tables on a regular basis, check out SQL Loader or external tables. Should be much faster than individual Inserts.
You can also use MyGeneration (free tool) to write your own sql generated scripts. There is a "insert into" script for SQL Server included in MyGeneration, which can be easily changed to run under Oracle.

Oracle 11g data pump 10 column limit

I am using an Oracle data pump to do a schema "rename." There is a primary key column on all (2000) tables. For example, I need to run this on all tables:
update mytable set mykey='foo2' where mykey='foo';
I would use the remap_data option of expdp to do this. The problem is that there are some columns that I would need to do the rename on 10+ columns. Has anyone had a problem like this and found a way to handle this?
Previously, I had tried using "Create Table As." The problem would be having to recreate the schema structure for all of the tables (views/triggers/grants/indexes/constraints). I am aware of the DBMS_METADATA.GET_DDL package. Offhand, doing a diff of the database schema before and after and recreating the diffs seems ugly.
I have also tried doing inserts on the table without any constraints or indexes, so I would only have to re-enable constraints and recreate the indexes, but I would like to try something faster.
I am using Oracle 11.2.0.3.0.
If i understand correctly, your real problem (or goal) is to 'RENAME' a schema.
You chose to export / import (using a different NAME to achieve RENAME) using oracle data pump.
Then DROP old schema (if you feel redundant).
If this is correct, here are the steps, you can do to achieve your goal. I did it successfully on my DEV env. All objects (including PK, FKs) were imported successfully.
-- Export RMCORE_QA
expdp DIRECTORY=DMPDIR DUMPFILE=RMCORE_QA.dmp SCHEMAS='RMCORE_QA' LOG=RMCORE_QA_EXP_DP.lst
-- Import using RMCORE_QA3
impdp DIRECTORY=DMPDIR DUMPFILE=RMCORE_QA.dmp REMAP_SCHEMA='RMCORE_QA:RMCORE_QA3' SCHEMAS='RMCORE_QA' LOG=RMCORE_QA_IMP_DP.lst TRANSFORM=OID:N
You can also compare objects b/w schemas by-
SELECT OBJECT_NAME, STATUS, object_type FROM dba_objects WHERE owner LIKE 'RMCORE_QA'
MINUS
select OBJECT_NAME, STATUS, object_type from dba_objects where owner like 'RMCORE_QA3';
HTH. Let me know if i did not get your problem...

SQLite "Drop table" error message: "SQL logic error or missing database"

I am running a SQLite database in memory and I am attempting to drop a table with the following command.
DROP TABLE 'testing' ;
But when I execute the SQL statement, I get this error
SQL logic error or missing database
Before I run the "Drop Table" query I check to make sure that the table exists in the database with this query. So I am pretty sure that the table exists and I have a connection to the database.
SELECT count(*) FROM sqlite_master WHERE type='table' and name='testing';
This database is loaded in to memory from a file database and after I attempt to drop this table the database is saved from memory to the file system. I can then use a third party SQLite utility to view the SQLite file and check to see if the "testing" exists, it does. Using the same 3rd party SQLite utility I am able to run the "Drop TABLE" SQL statement with out error.
I am able to create/update tables without any problems.
My questions:
Is there a difference between a memory database and a file database in SQLite when dropping a table?
Is there a way to disable the ability to drop a table in SQLite that I may have accentually turned on somehow?
Edit: It appears to have something to do with a locked table. Still investigating.
You should not have quotes in your DROP TABLE command. Use this instead:
DROP TABLE testing
I had the same problem when using Sqlite with the xerial jbdc driver in the version 3.7.2. and JRE7
I first listed all the tables with the select command as follows:
SELECT name FROM sqlite_master WHERE type='table'
And then tried to delete a table like this:
DROP TABLE IF EXISTS TableName
I was working on a database stored on the file system and so it seems not to effect the outcome.
I used the IF EXISTS command to avoid listing all the table from the master table first, but I needed the complete table list anyway.
For me the solution was simply to change the order of the SELECT and DROP.

Resources