Oracle Spatial question: can SDO_ORDINATE_ARRAY be populated using query results from another Oracle table? - oracle

I have a business development goal to use Oracle Spatial to store our coordinate data. Currently, we receive coordinates from scientists who are taking measurements in marine areas and these are stored in our Oracle database as either numeric pairs for points, or long varchar arrays for polygons. However, we would like to improve our management of these data by using Oracle Spatial.
The coordinate information we get from scientists normally comes in CSV files, with data attached and gets loaded into Oracle tables as entries in fields.
I know that I could manually enter the vertices into SDO_ORDINATE_ARRAY but we regularly get hundreds of coordinate pairs being supplied in one CSV file, which makes the manual route very inefficient.
Could someone please advise me if there is a way to populate the contents of the SDO_ORDINATE_ARRAY by pulling the information out from the other tables in the database where it is already stored?
An example of what I've tried is below:
Test table called GEOMTEST
consisting of
NAME varchar2(50)
COORDS varchar2(4000)
COORD_GEOM SDO_GEOMETRY
I've populated name with the area of interest 'Cardigan Bay' in Wales, UK.
COORDS is my polygon stored as an array in varchar2. This was imported from a CSV file.
COORD_GEOM is what I wish to transfer the content of COORDS into.
I attempted to run this piece of code but received an error:
insert into geomtest (coord_geom) values(SDO_GEOMETRY(2003,4326,null,SDO_ELEM_INFO_ARRAY(1,1003,1),SDO_ORDINATE_ARRAY values(select coords from geomtest));
I am using Toad as my client, and the error was "ERROR: line 18. column 120, ending line 18, column 125: Found 'values': A reserved word cannot be used as an identifier.
I presume this is related to my use of the select statement within the SDO_ORDINATE_ARRAY part of the INSERT statement but am uncertain as to how to proceed.
I would be grateful for any advice,
Many thanks
Sean

Unfortunately, you cannot directly pass a string containing numbers to the SDO_ORDINATE_ARRAY constructor. One solution is to write a custom string tokenizer function that will parse the string of coordinates into individual numbers, and build an SDO_ORDINATE_ARRAY object. Here is one:
create or replace function tokenize (str clob)
return sdo_ordinate_array
is
s clob := str||',';
i number;
j number;
t sdo_ordinate_array := sdo_ordinate_array();
begin
i := 1;
loop
j := instr(s, ',', i);
exit when j = 0;
t.extend();
t(t.count) := to_number(substr(s,i,j-i));
i := j+1;
end loop;
return t;
end;
/
show errors
And here is how it works. First let's create a simple table with a couple of examples:
drop table geomtest purge;
create table geomtest (
id number,
name varchar2(50 char),
coords clob,
coord_geom sdo_geometry
);
insert into geomtest (id, name, coords)
values (
2686,
'TX/Mitchell',
'-101.17416, 32.527592, -101.17417, 32.523998, -101.1836, 32.087082, -100.82121, 32.086479, -100.66497, 32.085278, -100.66024, 32.5252, -101.17416, 32.527592'
);
insert into geomtest (id, name, coords)
values (
2769,
'TX/Yoakum',
'-103.05616, 33.388332, -103.06416, 32.958992, -102.59455, 32.958733, -102.59436, 33.388393, -103.05616, 33.388332'
);
commit;
Then let's use the tokenizer function to update the geometry column:
update geomtest
set coord_geom = sdo_geometry(2003,4326,null,sdo_elem_info_array(1,1003,1),tokenize(coords));
commit;
Check the results:
SQL> select * from geomtest;
ID NAME COORDS COORD_GEOM(SDO_GTYPE, SDO_SRID, SDO_POINT(X, Y, Z), SDO_ELEM_INFO, SDO_ORDINATES)
---- ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
2686 TX/Mitchell -101.17416, 32.527592, -101.17417, 32.523998, -101.1836, 32.087082, -100.82121, 32.086479, -100.66497, 32.085278, -100.66024, 32.5252, -101.17416, 32.527592 SDO_GEOMETRY(2003, 4326, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(-101.17416, 32.527592, -101.17417, 32.523998, -101.1836, 32.087082, -100.82121, 32.086479, -100.66497, 32.085278, -100.66024, 32.5252, -101.17416, 32.527592))
2769 TX/Yoakum -103.05616, 33.388332, -103.06416, 32.958992, -102.59455, 32.958733, -102.59436, 33.388393, -103.05616, 33.388332 SDO_GEOMETRY(2003, 4326, NULL, SDO_ELEM_INFO_ARRAY(1, 1003, 1), SDO_ORDINATE_ARRAY(-103.05616, 33.388332, -103.06416, 32.958992, -102.59455, 32.958733, -102.59436, 33.388393, -103.05616, 33.388332))
2 rows selected.
NOTES
I used a CLOB column for storing the coordinates. A 4000 bytes string is too small to hold any serious geometry (unless all your shapes are very simple - just a few points).
There are more efficient ways if doing this string-to-geometry conversion, but they imply that you use geometry-oriented strings notations: GeoJSON, WKT, GML. Those are naturally supported by Oracle. They also allow more complex structures like multi-polygons or polygons with holes.
EDIT: I rewrote the function to directly return a SDO_GEOMETRY object instead. This makes it easier to use:
create or replace function string_to_geom (str clob)
return sdo_geometry
is
s clob := str||',';
i number;
j number;
t sdo_ordinate_array := sdo_ordinate_array();
begin
i := 1;
loop
j := instr(s, ',', i);
exit when j = 0;
t.extend();
t(t.count) := to_number(substr(s,i,j-i));
i := j+1;
end loop;
return sdo_geometry (2003, 4326, null, sdo_elem_info_array (1,1003,1), t);
end;
/
show errors
User it like this:
update geomtest
set coord_geom = string_to_geom(coords);
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

Writing a Version Number Function in PL/SQL

I want to write a function that will give me the next version number for a table. The table stores the existing version on each record. For example,
I have the cat table
cats
seqid 1
name Mr Smith
version number 1.2b.3.4
How can I write a program that will be able to increment these values based on various conditions?
This is my first attempt
if v_username is not null
then v_new_nbr = substr(v_cur_nbr, 1,7)||to_number(substr(v_cur_nbr, 8,1))+1
should be 1.2b.3.5
substr(v_cur_nbr, 1,7)||to_number(substr(v_cur_nbr, 8,1))+1
This hurls ORA-01722: invalid number. The reason is a subtle one. It seems Oracle applies the concatenation operator before the additions, so effectively you're adding one to the string '1.2b.3.4'.
One solution is using a TO_CHAR function to bracket the addition with the second substring before concatenating the result with the first substring:
substr(v_cur_nbr, 1,7) || to_char(to_number(substr(v_cur_nbr, 8,1))+1)
Working demo on db<>fiddle.
Incidentally, a key like this is a bad piece of data modelling. Smart keys are dumb. They always lead to horrible SQL (as you're finding) and risk data corruption. A proper model would have separate columns for each element of the version number. We can use virtual columns to concatenate the version number for display circumstances.
create table cats(
seqid number
,name varchar2(32)
,major_ver_no1 number
,major_ver_no2 number
,variant varchar2(1)
,minor_ver_no1 number
,minor_ver_no2 number
,v_cur_nbr varchar2(16) generated always as (to_char(major_ver_no1,'FM9') ||'.'||
to_char(major_ver_no2,'FM9') ||'.'||
variant ||'.'||
to_char(minor_ver_no1,'FM9') ||'.'||
to_char(minor_ver_no2,'FM9') ) );
So the set-up is a bit of a nause but incrementing the version numbers is a piece of cake.
update cats
set major_ver_no1 = major_ver_no1 +1
, major_ver_no2 = 0
, variant = 'a';
There's a db<>fiddle for that too.
Try searching mask for TO_NUMBER to be able to get the decimal number, this small example might help:
CREATE TABLE tmp_table (version varchar2(100));
INSERT INTO tmp_table(version) VALUES ('1.2b.3.4');
DECLARE
mainVersion NUMBER;
subVersion NUMBER;
currentVersion VARCHAR2(100);
BEGIN
SELECT version INTO currentVersion FROM tmp_table;
mainVersion := TO_NUMBER(SUBSTR(currentVersion,1,3),'9.9') + 0.1;
subVersion := TO_NUMBER(SUBSTR(currentVersion,6,3),'9.9') + 1.1;
UPDATE tmp_table SET version = (mainVersion||'b.'||subVersion);
END;

Procedure to add history of an event to Oracle SQL

I need to write a procedure in Oracle SQL that inserts data from three tables into one separate table without any keys. Here is the model of my database:
. (All of the column names are in Polish, sorry about that).
I have already created a table for history:
create table art_historia(
data_koncertu date default sysdate, --Concert date
miejsce_koncertu varchar2(40), --Place of concert
nazwa_zespolu varchar2(30), --Name of band
liczba_widzow number(5), --Number of viewers
bilans_finansowy number (8,2)); --Finance balance
And the values that I want to insert there using this procedure I can picture with this select:
select s.miasto, a.nazwa_zespolu, k.ilosc_sprzedanych_biletow, k.zysk
from art_sala s, art_koncert k, art_artysta a
where k.kod_sali_koncertowej = s.kod_sali_koncertowej and
k.kod_artysty = a.kod_artysty and
k.id_koncertu = (variable of concert id);
How can I do this?
Try this
CREATE PROCEDURE INSERT_ART_HIST_P (p_in_concert_id number) IS
BEGIN
INSERT INTO art_historia
select s.miasto, a.nazwa_zespolu, k.ilosc_sprzedanych_biletow, k.zysk
from art_sala s, art_koncert k, art_artysta a
where k.kod_sali_koncertowej = s.kod_sali_koncertowej and
k.kod_artysty = a.kod_artysty and
k.id_koncertu = p_in_concert_id;
END;

Generate automatic Id

I am making a web application which uses a database in which I have a field I_ID which i want to automatically increment like I0 then I1 then I2 and so on with each record insertion in the database.
To achieve it I made a trigger for this table.But its not working fine.What can be the reason.Please help
My Trigger T1:
CREATE OR REPLACE TRIGGER "T1"
before
insert on "TBINDIVIDUAL"
for each row
declare
x varchar2(10);
mx varchar2(13);
mx2 varchar2(13);
y number(3);
begin
x:=:new.I_ID;
mx:=substr(x,1,1);
select max(I_ID) into mx2 from tbindividual where I_ID like mx||'%';
y:=to_number(substr(mx2,2));
:new.I_ID:=mx||to_char(y+1);
end t1;
/
EDITED :
As i do by answer
CREATE OR REPLACE TRIGGER "TBINDIVIDUAL_T1"
BEFORE
insert on "TBINDIVIDUAL"
for each row
begin
:new.I_ID = SEQ1.nextval;
end;
/
But it give two errors
Encountered the symbol "=" when expecting one of the following: := . ( # % ; indicator
Encountered the symbol "END"
Please help
So Oracle is not SQL Server...
if you want to get unique ID's, you need to populate them from a sequence.
for creating a sequence use:
create sequence myseq;
and in your code use (depends on the version):
:new.I_ID := myseq.nextval;
or
select myseq.nextval into :new.I_ID from dual;
the problem your code doesn't work is what :new and :old means...
I would recommend you reading about their meaning...
Hope I've been helpful...
Because of the discussion in the comments - here is a full example:
for this table:
CREATE TABLE test (A number);
to add a unique, sequential ID you need to first create a sequence:
CREATE SEQUENCE myseq;
and a trigger:
CREATE OR REPLACE TRIGGER "T1"
before
insert on "test"
for each row
begin
:new.I_ID := myseq.nextval;
end t1;
/
by the way - I would recommend to check before substituting :new.I_ID, if it is null or not, cause sometimes in upgrades people add a unique ID from an external resource.. (such as them getting a unique number from the sequence themselves...)
you can read more about sequences here:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_6015.htm#SQLRF01314
sorry for the way the code is displayed.. need to learn how to write code here...
One more thing - In Oracle - you cannot create such a PL/SQL to increase existing counter without locks.
Concurrent queries might run the first query in the PL/SQL simultaneously, which means multiple sessions will get the same I_ID.
Also notice that in your code you queried the max on varchar, which is not the same as max on number...
Adding concatenated text is unrelated to the unique ID. In your case it will look like:
:new.I_ID = substr(:new.I_ID,1,1)||to_char(myseq.nextval);
assuming x is being inputted with the char you want..

How to write oracle insert script with one field as CLOB?

I want to create an insert script which will be used only to insert one record into one table.
It has 5 columns and one of them is of type CLOB.
Whenever I try, it says can not insert string is so long . larger than 4000.
I need an insert statement with clob as one field.
INSERT INTO tbltablename
(id,
NAME,
description,
accountnumber,
fathername)
VALUES (1,
N'Name',
clob'some very long string here, greater than 4000 characters',
23,
'John') ;
Keep in mind that SQL strings can not be larger than 4000 bytes, while Pl/SQL can have strings as large as 32767 bytes. see below for an example of inserting a large string via an anonymous block which I believe will do everything you need it to do.
note I changed the varchar2(32000) to CLOB
set serveroutput ON
CREATE TABLE testclob
(
id NUMBER,
c CLOB,
d VARCHAR2(4000)
);
DECLARE
reallybigtextstring CLOB := '123';
i INT;
BEGIN
WHILE Length(reallybigtextstring) <= 60000 LOOP
reallybigtextstring := reallybigtextstring
|| '000000000000000000000000000000000';
END LOOP;
INSERT INTO testclob
(id,
c,
d)
VALUES (0,
reallybigtextstring,
'done');
dbms_output.Put_line('I have finished inputting your clob: '
|| Length(reallybigtextstring));
END;
/
SELECT *
FROM testclob;
"I have finished inputting your clob: 60030"
I solved my problem with a solution that is simpler than the most voted answer.
You must divide your big clob string into multiple strings, each one with less than 4000 chars, convert each one using the to_clob method, and concatenate them with the || operator.
Here is an example of the final insert statement:
INSERT INTO tbltablename
(id,
name,
big_clob_description)
VALUES (1,
N'A Name',
to_clob('string with less than 4000 chars')
|| to_clob('rest of string here, with less than 4000 chars')
) ;
My insert code was generated by a script, and it wasn't difficult to break the strings.
You can use the to_clob function too.
INSERT INTO tbltablename
(id,
NAME,
description,
accountnumber,
fathername)
VALUES (1,
N'Name',
to_clob('clob''some very long string here, greater than 4000 characters'),
23,
'John') ;
You can find more information here: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions182.htm.
Regards.

Resources