store value of column into variable - oracle

I want to get a value of column, and put it into variable of the same type of that column, as the following :
select FRI_SEC_TEMP into friday_seconds
from TEMPTABLE
where rownum<2;
DBMS_OUTPUT.PUT_LINE(friday_seconds);
but when I run the script, it give me the following error:
Query Run In:Query Result 6
Error starting at line : 61 in command -
DBMS_OUTPUT.PUT_LINE(friday_seconds)
Error report -
Unknown Command
and when I run just the select statement I get the following error:
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:

Just execute the script as a PL SQL block by surrounding it with begin - end
begin
select FRI_SEC_TEMP into friday_seconds
from TEMPTABLE
where rownum<2;
DBMS_OUTPUT.PUT_LINE(friday_seconds);
end;

Related

oracle sql: collect aggregation

I want to group my database entries by an attribute and to know which entries are in each group at the same time. I collect the ids of the grouped entries with Oracle COLLECT function COLLECT Function
DECLARE
TYPE ids_type IS TABLE OF number(19, 0);
ids ids_type;
BEGIN
select cast(collect(r.id) as ids_type) into ids from rechnungsdaten r group by r.status;
END;
But then I get the error:
Error report - ORA-06550: Line 5, Column 44: PL/SQL:
ORA-00902: Invalid datatype ORA-06550: Line 5, Column 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What is wrong here?
You cannot use COLLECT function on a type declared in a PL/SQL anonymous block.
You have other options like
Create a database type and run your collect query.
create or replace TYPE ids_type IS TABLE OF number(19, 0);
SELECT
r.status,
CAST(COLLECT(r.id) AS ids_type)
FROM
rechnungsdaten r
GROUP BY
r.status;
Use a simple LISTAGG query to see the list of ids as a string
SELECT
r.status,
LISTAGG(r.id,',') WITHIN GROUP(
ORDER BY
id
)
FROM
rechnungsdaten r
GROUP BY
r.status;
Demo

"ORA-12401: invalid label string: " error

I am trying to run a simple update statement in Oracle to modify an Integer field of a specific row.
UPDATE Mytable
SET field1 = 2
WHERE field2 = 11111;
But I receive this error
SQL Error: ORA-12401: invalid label string:
ORA-06512: at "LBACSYS.NUMERIC_LABEL_TO_CHAR", line 1
ORA-06512: at "LBACSYS.LBAC$BU0_92924", line 1
ORA-04088: error during execution of trigger 'LBACSYS.LBAC$BU0_92924'
12401. 00000 - "invalid label string: %s"
*Cause: The policy could not convert the label string to a valid
internal label.
*Action: Correct the syntax of the label string.
Google search did not bring much results. Do you have any idea about the solution of this error?

SQL Error 00905. 00000 - "missing keyword"

I am trying to execute this query ;
Grant SELECT on TYPE PPZ_C.BTS_BAUTEIL_STATION_INFO to PPZ_W;
and I get the folowing error:
Error starting at line : 3 in command - Grant SELECT on TYPE
PPZ_C.BTS_BAUTEIL_STATION_INFO to PPZ_W Error report - ORA-00905:
missing keyword
00905. 00000 - "missing keyword"
*Cause:
*Action:
Does anyone know what keyword I am missing ?
Firstly you need to remove TYPE. This will fix your first part of issue.
Try this
Grant SELECT on PPZ_C.BTS_BAUTEIL_STATION_INFO to PPZ_W
Now when you execute the above , you will further get the error:
ORA-02305: only EXECUTE, DEBUG, and UNDER privileges are valid for
types
Which means you cannot grant select on any type. Only EXECUTE, DEBUG, and UNDER privileges are valid for types.
So you can use EXECUTE when you want it to be used in select statement :
Grant EXECUTE on PPZ_C.BTS_BAUTEIL_STATION_INFO to PPZ_W;
EDIT:
Your select statement should be :
SELECT ppz_bts.na_stat_anf( PPZ_C.BTS_BAUTEIL_STATION_INFO ('','IG','12345679') ) FROM dual;
See demo:
CREATE OR REPLACE TYPE myschema.array_t is varray(2) of number ;
---Running in My schema
SQL> select * from table(array_t('1','2'));
COLUMN_VALUE
------------
1
2
--Granted from myschema to otherschema
SQL> grant execute on myschema.array_t to othersschema ;
--Running in otherschema
SQL> select * from table(myschema.array_t('1','2'));
COLUMN_VALUE
------------
1
2
When Oracle receives requests for type information, it verifies that the requestor has EXECUTE privilege on the type before supplying the requested information.
Try this :
Grant EXECUTE on PPZ_C.BTS_BAUTEIL_STATION_INFO to PPZ_W;
Then try doing :
SELECT ppz_bts.na_stat_anf( PPZ_C.bts_bauteil_station_info('','IG','12345679') )
FROM dual;

SELECT a table from oracle data dictionary

I am new to SQL and recently installed Oracle 11g. I read the post here on selecting all tables from user_tables. I'm trying to select a specific table and following some of the suggestions in the post does not appear to work.
The following executes fine and returns all tables available to me including a table named faculty_t:
select * from user_tables;
select * from dba_tables;
select * from all_tables;
desc faculty_t;
But I get error when I do the following:
select * from user_tables where table_name = FACULTY_T;
The first set of statements confirm that I do have a table named faculty_t. However, trying to select this table from user_tables, all_tables, or dba_tables does not appear to work for me right now. The error message reads something like:
ORA-00904: "FACULTY_T": invalid identifier
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error at Line: 208 Column: 8
Any thoughts? Thanks!
String literals in SQL are wrapped in '. So:
select * from user_tables where table_name = 'FACULTY_T';
When you did a desc faculty_t, the SQL engine knew that a table name was expected at that spot (the syntax expects a table name there). But in your select query, sql is just looking for the value of a column that happens to have a string data type, so you need to use the ' for a string literal.

Hashing string in Oracle

I know there's a function to obtain the hash value from a varchar in Oracle, but when I run the query to see the returned value it sends the following message.
ORA-00904: : identificador no válido
00904. 00000 - "%s: invalid identifier"
*Cause:
*Action:
Error en la línea: 70, columna: 7
What I'm doing is the following:
select DBMS_CRYPTO.hash(utl_raw.cast_to_raw('Foo'), 3) FROM dual;
It should return the SHA-1 of the string.
That works for me
SQL> select DBMS_CRYPTO.hash(utl_raw.cast_to_raw('Foo'), 3) FROM dual;
DBMS_CRYPTO.HASH(UTL_RAW.CAST_TO_RAW('FOO'),3)
--------------------------------------------------------------------------------
201A6B3053CC1422D2C3670B62616221D2290929
Are you sure that the user that is running this has been granted EXECUTE access on both the UTL_RAW and the DBMS_CRYPTO packages? If line 70 of your code is this SELECT statement, I'd wager that column 7 is where the DBMS_CRYPTO call starts and that you don't have access to the DBMS_CRYPTO package.

Resources