can not update user_sdo_geom_metadata when creating spatial database in oracle - oracle

i was using the oracle 11g and try to create a spatial database, i just copied the sample code from oracle document
but when it comes to update the metadata part, it gave a duplicate entry error, and i tried delete from user_sdo_geom_metadata and it didnot give any error, and then tried to insert again, still got the duplicate entry error. I also tried select * from user_sdo_geom_metadata but got nothing.
any one have idea why? thanks
THE sample code:
CREATE TABLE cola_markets (
mkt_id NUMBER PRIMARY KEY,
name VARCHAR2(32),
shape SDO_GEOMETRY);
INSERT INTO cola_markets VALUES(
1,
'cola_a',
SDO_GEOMETRY(
2003, -- two-dimensional polygon
NULL,
NULL,
SDO_ELEM_INFO_ARRAY(1,1003,3), -- one rectangle (1003 = exterior)
SDO_ORDINATE_ARRAY(1,1, 5,7) -- only 2 points needed to
-- define rectangle (lower left and upper right) with
-- Cartesian-coordinate data
));
INSERT INTO user_sdo_geom_metadata
(TABLE_NAME,
COLUMN_NAME,
DIMINFO,
SRID)
VALUES (
'cola_markets',
'shape',
SDO_DIM_ARRAY( -- 20X20 grid
SDO_DIM_ELEMENT('X', 0, 20, 0.005),
SDO_DIM_ELEMENT('Y', 0, 20, 0.005)
),
NULL -- SRID
);
error cause: there are duplicate entries for the give table and column value pair in the user_sdo_geom_metadata view.

Just to be sure, what does the following show ?
SELECT * FROM USER_SDO_GEOM_METADATA;
Object names in dictionary views are stored in uppercase. So if you want to remove existing entries from the spatial metadata you need to do this (notice the uppper case table name):
DELETE FROM USER_SDO_GEOM_METADATA WHERE TABLE_NAME = 'COLA_MARKETS';
COMMIT;

Related

Inserting values into newly created table from a pre-existing table using a cursor and for loop - Snowflake SQL (classic web interface)

I'm trying to insert values into a new table in the classic Snowflake SQL web interface using data from a table that was already created, a cursor, and a for loop. My goal is to insert new information and information from the original table into the new table, but when I try and run my code, there is an error where I am referring to the column of my original table. (See code below)
-- Creation and inserting values into table invoice_original
create temporary table invoice_original (id integer, price number(12,2));
insert into invoice_original (id, price) values
(1, 11.11),
(2, 22.22);
-- Creates final empty table invoice_final
create temporary table invoice_final (
study_number varchar,
price varchar,
price_type varchar);
execute immediate $$
declare
c1 cursor for select price from invoice_original;
begin
for record in c1 do
insert into invoice_final(study_number, price, price_type)
values('1', record.price, 'Dollars');
end for;
end;
$$;
My end goal is to have the resulting table invoice_final with 3 columns - study_number, price, and price_type where the price value comes from the invoice_original table. The error I'm currently getting is:
Uncaught exception of type 'STATEMENT_ERROR' on line 6 at position 8 : SQL compilation error: error line 2 at position 20 invalid identifier 'RECORD.PRICE'.
Does anyone know why the record.price is not capturing the price value from the invoice_original table?
there are a number of type of dynamic SQL that do not handle the cursor name, and thus give this error if you push it into a single name temp value it will work:
for record in c1 do
let temp_price number := record.price;
insert into invoice_final(study_number, price, price_type)
values('1', temp_price, 'Dollars');
end for;
this sql has not been run, and could be the wrong format, but it is the base issue.
Also this really looks like an INSERT would work, but I also assume this is the nature of simplify the question down.
See the following for details on working with variables:
https://docs.snowflake.com/en/developer-guide/snowflake-scripting/variables.html#working-with-variables
The revised code below functions as desired:
-- Creation and inserting values into table invoice_original
create
or replace temporary table invoice_original (id integer, price number(12, 2));
insert into
invoice_original (id, price)
values
(1, 11.11),
(2, 22.22);
-- Creates final empty table invoice_final
create
or replace temporary table invoice_final (
study_number varchar,
price number(12, 2),
price_type varchar
);
execute immediate $$
declare
new_price number(12,2);
c1 cursor for select price from invoice_original;
begin
for record in c1 do
new_price := record.price;
insert into invoice_final(study_number, price, price_type) values('1',:new_price, 'Dollars');
end for;
end;
$$;
Note that I changed the target table definition for price to NUMBER (12,2) instead of VARCHAR, and assigned the record.price to a local variable that was passed to the insert statement as :new_price.
That all said ... I would strongly recommend against this approach for loading tables for performance reasons. You can replace all of this with an INSERT .. AS ... SELECT.
Always opt for set based processing over cursor / loop / row based processing with Snowflake.
https://docs.snowflake.com/en/sql-reference/sql/insert.html

Collections: Nested Tables- data not displaying

I am working with Collections Nested Tables in SQL Developer. When I select from a table that contains a nested table column, it only returns the container holder and not the data.
Output:
COLLECTION_USER.T_NESTED_TABLE_ADDRESS([COLLECTION_USER.T_ADDRESS],[COLLECTION_USER.T_ADDRESS])
Once I export the results. The export shows everything correctly.
Output:
COLLECTION_USER.T_NESTED_TABLE_ADDRESS(COLLECTION_USER.T_ADDRESS('2 State Street','Beantown','MA','12345'),COLLECTION_USER.T_ADDRESS('4 Hill Street','Lost Town','CA','54321'))
Has anyone else seen this? I think I just need to upgrade SQL Developer but I am just asking.
From an online example since you didn't provide your source code/SQL
CREATE OR REPLACE TYPE MY_TAB_T AS
TABLE OF VARCHAR2(30);
/
CREATE TABLE NESTED_TABLE (
ID NUMBER,
COL1 MY_TAB_T
)
NESTED TABLE COL1 STORE AS COL1_TAB;
INSERT INTO NESTED_TABLE VALUES (
1,
MY_TAB_T(
'A'
)
);
INSERT INTO NESTED_TABLE VALUES (
2,
MY_TAB_T(
'B', 'C'
)
);
INSERT INTO NESTED_TABLE VALUES (
3,
MY_TAB_T(
'D', 'E',
'F'
)
);
COMMIT;
select * from nested_table;
If you double-click on the nested table column, you'll see a Pencil Icon, and then clicking that -
This is using version 19.4, but I'm not aware of any bugs in this area in the recent past, although my memory is usually suspect at best.

Oracle Spatial issue PLS-306: wrong number or types of arguments in call to 'SDO_GEOMETRY'

I created a table:
CREATE TABLE cola_markets (
mkt_id NUMBER PRIMARY KEY,
name VARCHAR2(32),
shape SDO_GEOMETRY);
I tried to insert a row:
INSERT INTO cola_markets VALUES(
1,
'cola_a',
SDO_GEOMETRY(
2003, -- two-dimensional polygon
NULL,
NULL,
SDO_ELEM_INFO_ARRAY(1,1003,3), -- one rectangle (1003 = exterior)
SDO_ORDINATE_ARRAY(1,1, 5,7) -- only 2 points needed to
-- define rectangle (lower left and upper right) with
-- Cartesian-coordinate data
)
);
I got the error:
SQL Error: ORA-06553: PLS-306: wrong number or types of arguments in call to 'SDO_GEOMETRY'
If I switch SDO_ELEM_INFO_ARRAY to NULL:
INSERT INTO cola_markets VALUES(
1,
'cola_a',
SDO_GEOMETRY(
2003, -- two-dimensional polygon
NULL,
NULL,
NULL, -- one rectangle (1003 = exterior)
SDO_ORDINATE_ARRAY(1,1, 5,7) -- only 2 points needed to
-- define rectangle (lower left and upper right) with
-- Cartesian-coordinate data
)
);
I don't get error:
1 row inserted.
What's the issue here? Why can't I use SDO_ELEM_INFO_ARRAY?

Trouble adding metadata to a table in Oracle

I created a table in Oracle with this code:
CREATE TABLE Roads (
TYPE VARCHAR2(40),
ADMN_CLASS VARCHAR2(20),
TOLL_RD VARCHAR2(10),
RTE_NUM1 VARCHAR2(3),
RTE_NUM2 VARCHAR2(3),
ROUTE VARCHAR2(40),
LOCATION MDSYS.SDO_GEOMETRY);
Then I wanted to add metadata to the LOCATION column which will hold the geometry:
INSERT INTO USER_SDO_GEOM_METADATA
(TABLE_NAME, COLUMN_NAME, DIMINFO, SRID)
VALUES
('ROADS', 'LOCATION',
MDSYS.SDO_DIM_ARRAY
( MDSYS.SDO_DIM_ELEMENT('X', -180, 180, 0.5),
MDSYS.SDO_DIM_ELEMENT('Y', -90, 90, 0.5)
),
8256
);
However, I get an error:
Error report -
SQL Error: ORA-13223: duplicate entry for ROADS.LOCATION in SDO_GEOM_METADATA
ORA-06512: at "MDSYS.MD", line 1723
ORA-06512: at "MDSYS.MDERR", line 17
ORA-06512: at "MDSYS.SDO_GEOM_TRIG_INS1", line 48
ORA-04088: error during execution of trigger 'MDSYS.SDO_GEOM_TRIG_INS1'
13223. 00000 - "duplicate entry for %s in SDO_GEOM_METADATA"
*Cause: There are duplicate entries for the given table and column
value pair in the USER_SDO_GEOM_METADATA view.
*Action: Check that the specified table and geometry column names
are correct. There should be only one entry per table, geometry
column pair in the USER_SDO_GEOM_METADATA view.
I am not sure why it says there are duplicate values because I have not created this table before. It also tells me to make sure that table and column name is correct and they are.
I've created your table and executed your insert statement OK.
Once executed again, I was able to reproduce your problem.
The error "SQL Error: ORA-13223: duplicate entry for ROADS.LOCATION in SDO_GEOM_METADATA" definitely points to duplicate data.
This trigger MDSYS.SDO_GEOM_TRIG_INS1 just performs a count(*) validation on the following MDSYS table and raise the -13223 exception when rowcount is != 0 .
SQL> desc mdsys.SDO_GEOM_METADATA_TABLE
Name Null? Type
----------------------------------------- -------- ----------------------------
SDO_OWNER NOT NULL VARCHAR2(32)
SDO_TABLE_NAME NOT NULL VARCHAR2(32)
SDO_COLUMN_NAME NOT NULL VARCHAR2(1024)
SDO_DIMINFO MDSYS.SDO_DIM_ARRAY
SDO_SRID NUMBER
Query mdsys.SDO_GEOM_METADATA_TABLE in order to validate its contents. You should find the duplicate record:
SQL> select * from mdsys.SDO_GEOM_METADATA_TABLE ;
USER1
ROADS
LOCATION
SDO_DIM_ARRAY(SDO_DIM_ELEMENT('X', -180, 180, .5), SDO_DIM_ELEMENT('Y', -90, 90,
.5))
8256
To workaround this issue just delete that record from mdsys.SDO_GEOM_METADATA_TABLE.
I think the problem is because some space characters on the column_name.
COR_NB_SC_LON,COR_CD_EW_ LON
I have fixed it and it works.
insert into user_sdo_geom_metadata (table_name, column_name, diminfo, srid)
values (
'COORD_REF',
'myuser.CONVERT_DMS_TO_GEOM(COR_NB_DG_LON,COR_NB_MN_LON,COR_NB_SC_LON,COR_ CD_EW_LON,COR_NB_DG_LAT,COR_NB_MN_LAT,COR_NB_SC_LAT,COR_CD_NS_LAT)',
sdo_dim_array (
sdo_dim_element('long', -180.0, 180.0, 0.5),
sdo_dim_element('lat', -90.0, 90.0, 0.5)
),
4326
);
commit;

How to duplicate all data in a table except for a single column that should be changed

I have a question regarding a unified insert query against tables with different data
structures (Oracle). Let me elaborate with an example:
tb_customers (
id NUMBER(3), name VARCHAR2(40), archive_id NUMBER(3)
)
tb_suppliers (
id NUMBER(3), name VARCHAR2(40), contact VARCHAR2(40), xxx, xxx,
archive_id NUMBER(3)
)
The only column that is present in all tables is [archive_id]. The plan is to create a new archive of the dataset by copying (duplicating) all records to a different database partition and incrementing the archive_id for those records accordingly. [archive_id] is always part of the primary key.
My problem is with select statements to do the actual duplication of the data. Because the columns are variable, I am struggling to come up with a unified select statement that will copy the data and update the archive_id.
One solution (that works), is to iterate over all the tables in a stored procedure and do a:
CREATE TABLE temp as (SELECT * from ORIGINAL_TABLE);
UPDATE temp SET archive_id=something;
INSERT INTO ORIGINAL_TABLE (select * from temp);
DROP TABLE temp;
I do not like this solution very much as the DDL commands muck up all restore points.
Does anyone else have any solution?
How about creating a global temporary table for each base table?
create global temporary table tb_customers$ as select * from tb_customers;
create global temporary table tb_suppliers$ as select * from tb_suppliers;
You don't need to create and drop these each time, just leave them as-is.
You're archive process is then a single transaction...
insert into tb_customers$ as select * from tb_customers;
update tb_customers$ set archive_id = :v_new_archive_id;
insert into tb_customers select * from tb_customers$;
insert into tb_suppliers$ as select * from tb_suppliers;
update tb_suppliers$ set archive_id = :v_new_archive_id;
insert into tb_suppliers select * from tb_suppliers$;
commit; -- this will clear the global temporary tables
Hope this helps.
I would suggest not having a single sql statement for all tables and just use and insert.
insert into tb_customers_2
select id, name, 'new_archive_id' from tb_customers;
insert into tb_suppliers_2
select id, name, contact, xxx, xxx, 'new_archive_id' from tb_suppliers;
Or if you really need a single sql statement for all of them at least precreate all the temp tables (as temp tables) and leave them in place for next time. Then just use dynamic sql to refer to the temp table.
insert into ORIGINAL_TABLE_TEMP (SELECT * from ORIGINAL_TABLE);
UPDATE ORIGINAL_TABLE_TEMP SET archive_id=something;
INSERT INTO NEW_TABLE (select * from ORIGINAL_TABLE_TEMP);

Resources