So I have a function to return an Average of a column such as
CREATE OR REPLACE FUNCTION avgCol
RETURN DEC IS avgNum DEC;
BEGIN
SELECT AVG(myCol)
INTO avgNum
FROM MyTable;
RETURN avgNum;
END;
/
While trying to test the results, i have the following
SELECT avgCol
INTO RESULT
FROM DUAL;
but it gives me the error
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Error at Line: 175 Column: 6
Where line 175 is INTO RESULT. As far as I know, this is a scalar function and I'm trying to return a signal variable so it should work right? What keyword am I missing here?
Also I know I can just use AVG(), but I am learning how to create a scalar function. this is strictly for learning purposes.
While testing your code (which should be ok), you need
SELECT avgCol AS result FROM DUAL;
'INTO' assigns the result value to a PL/SQL variable; 'AS' creates an alias/name for a SQL SELECT (not PL/SQL) result column/field.
Related
I'm trying to do overlap in SQL (using postgres) but receive an syntax error. And i do not know why I'm getting error and whats wrong with my SQL Statement
Error text
[42601] ERROR: syntax error at or near "[" Position: 296
SQL Statement
SELECT *,
ARRAY_AGG("reserve"."state_id" ) AS "states" FROM "orders_order"
JOIN "reserve" ON ("order"."id" = "reserve"."order_id")
GROUP BY "order"."id"
HAVING (NOT (ARRAY_AGG("reserve"."state_id" )&& ['test']::varchar(255)[]))
As documented in the manual there are two ways to specify an array:
First with the array keyword:
array['test']
or as a string enclosed with curly braces:
'{test}'::text[]
In the second case the casting might or might not be needed depending on the context on where it's used.
There is no need to cast it to a varchar(255)[] array. text[] will do just fine - especially when comparing it to the result of an array_agg() which returns text[] as well.
I'm querying an Oracle Spatial database with this query (I left out the other 1496 coordinates):
SELECT *
FROM pointsofinterest
WHERE Sdo_inside (pointsofinterest.geoloc,
Sdo_geometry(2003, 4326, NULL, Sdo_elem_info_array(1, 1003, 1),
Sdo_ordinate_array(4.237378120400001, 43.7904510498, 4.2357025146,
43.7882575989
,
4.232352256800001, 43.7882575989,
4.232352256800001, 43.7871589661))) = 'TRUE'
and get this error:
ORA-00939: too many arguments for function
00939. 00000 - "too many arguments for function"
*Cause:
*Action: Error at Line: 4 Column: 27
The SDO_GEOMETRY object contains 1500 2D-coordinates. That should not be a problem according to http://docs.oracle.com/cd/B28359_01/appdev.111/b28400/sdo_objrelschema.htm#SPATL489
When I remove a lot of coordinates, there is no error.
Am I missing something about the max number of coordinates in the SDO_GEOMETRY constructor? Why am I getting this error?
This error comes from one of the limitations in SQL: the maximum number of arguments that can be passed to a stored function or procedure, i.e. 999 arguments (actually 1000 including the return value). This is because sdo_ordinate_array() is actually a function - the constructor of the sdo_ordinate_array type.
The proper approach (which is also more efficient) is to pass the sdo_geometry object as a bind variable and use that in the query. The way that is done depends on the language you use to submit the select statement ...
I Have an error on a oracle server but I don't understand why is not work.
I use the software oracle sql developer.
The query is:
SELECT * FROM TestView WHERE REPLACE(TestView.Row2, '.', ',') > 0 ;
The value who is in TestVue.Row2 : '46.08','-46.47','1084.05','66500',...
"TestView" is a view who check to return a row without empty value
When I Execute the query I have always an error who says:
ORA-01722: invalid number
01722. 00000 - "invalid number"
*Cause:
*Action:
Thanks for your help
Zoners
You have a row in your table, which cannot be parsed into number format. Most likely it is a blank column.
SELECT * FROM TestVue WHERE TestVue.Row2 IS NULL ;
Use a NVL Function to avoid errors caused by null field and a TO_NUMBER function to cast into a number before comparision with zero
SELECT * FROM TestView WHERE TO_NUMBER(REPLACE(NVL(TestView.Row2,'0'), '.', ',')) > 0 ;
You should be able to find the row with something like:
select Row2
from TestVuw
where length(trim(translate(Row2, '+-.,0123456789', ' '))) = 0
Of course, this allows multiple +, -, ., and ,. It can be further refined using a regular expression, but this usually works to find the bad ones.
Please explain why the following query:
select in.status as "no_installments"
, count(in.id) as "installment"
FROM instalsched.instalment in
GROUP BY in.status;
returns
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 1 Column: 12
in is a key word in SQL. It is used as part of a where clause, such as where person_id in (1,2,3,4). To remedy, simply change the alias.
select
in1.status as "no_installments",
count(in1.id) as "installment"
FROM instalsched.instalment in1
GROUP BY in1.status;
in is a keyword. Use a different alias or wrap it in double quotes.
"in" is a reserved word in SQL syntax. You should try to use other non-reserved word like "inst" or something similar.
I'm having a little trouble with this sql view.
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE
WHEN (tb.themed_party_size % 2) = 0 THEN
(tb.themed_party_size-2)/2
ELSE ((tb.themed_party_size-2)/2) + 0.5
END themed_tables
FROM themed_booking tb;
Can anyone help me here? I'm trying to add a column to the end of the view that the natural number result of (S-2)/2 where S is the themed_party_size.
When i say natural number result i mean like round up the answers that end in .5 so if S=7 the answer would be 3 and not 2.5.
The error I get when I try and run the above code is
Error starting at line 1 in command:
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE WHEN (tb.themed_party_size % 2) = 0
THEN (tb.themed_party_size-2)/2
ELSE ((tb.themed_party_size-2)/2) + 0.5
END themed_tables
FROM themed_booking tb
Error at Command Line:3 Column:34
Error report:
SQL Error: ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
If it makes a difference I am using sqldeveloper connected to an oracle server so I can use PL/SQL.
The error message is telling you what the problem is.
Look at Line:3 Column:34
It is an invalid character
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
CASE WHEN (tb.themed_party_size % 2) = 0
^
My suspicion is that you are trying to use the modulo operator.
Since you are using oracle PL/SQL, you should use mod
Here is a reference Oracle/PLSQL: Mod Function
I think you can simplify with the CEIL() or ROUND() function:
CREATE OR REPLACE VIEW view_themed_booking AS
SELECT tb.*,
ROUND((tb.themed_party_size-2)/2) AS themed_tables
FROM themed_booking tb;
Not sure why you get that error. Perhaps it's the % operator that is not available in Oracle. This links suggests so: Fundamentals of PL/SQL. There seems to be a MOD() function though.