Usually we use Select function(param) from dual to retrieve a value from function. I am trying to get a value from a function in a stored procedure in oracle. But it's getting Syntax error. The code is like this
voucher_no := select Func_Voucher_No_Gen (vc_comp_code,vou_dt,'JN') from dual;
Can anyone suggest what's wrong ? thanks in advance.
Direct assignment,
voucher_no := Func_Voucher_No_Gen (vc_comp_code,vou_dt,'JN') ;
should do the job.
Or,
select Func_Voucher_No_Gen (vc_comp_code,vou_dt,'JN') into voucher_no from dual;
Related
I am getting
ORA-06502 PL/SQL numeric or value error
when I am trying to execute this statement from Report Builder 6i and Oracle database 11g.
select to_char(to_date(lateby, 'sssss'), 'mi:ss')
from attendancelogs
where emp_no = :emp_no
and attendancedate = :dat;
late number(15);
The same statement when run under SQL*Plus is running fine without any error and fetching values the way I want.
Any help will be much appreciated.
Regards,
Mac
It might be a Reports bug, I don't know, but as a workaround you could try wrapping this into a stored procedure in the database as follows:
CREATE OR REPLACE FUNCTION sssss_to_miss ( p1 in varchar2 ) RETURN VARCHAR2 IS
BEGIN
RETURN ( to_char(to_date(p1, 'sssss'), 'mi:ss') );
END;
/
Then in the Reports Code:
select sssss_to_miss (lateby)
from attendancelogs
where emp_no = :emp_no
and attendancedate = :dat;
late number(15);
Alternatively, something like (not tested):
select to_char(to_number(lateby)/60,'00') || ':' || to_char(mod(to_number(lateby),60),'00')
from attendancelogs
where emp_no = :emp_no
and attendancedate = :dat;
I'd like to read boolean field from Oracle Database (11g), but I'm unable to do so, probably due to Oracle not fully supporting boolean data format.
Field I'm interessted in is dbms_utility.is_cluster_database.
Working statement:
set serveroutput on;
BEGIN
IF dbms_utility.is_cluster_database THEN
dbms_output.put_line('true');
ELSE
dbms_output.put_line('false');
END IF;
END;
This statement is working fine, however I need it as SQL query, like SELECT 'someValue' from dual;
I've tried:
SELECT dbms_utility.is_cluster_database FROM DUAL; fails with message "ORA-06553: PLS-382: expression is of wrong type"
SELECT CAST(dbms_utility.is_cluster_database AS INT) FROM DUAL; fails with same message
SELECT sys.diutil.bool_to_int(dbms_utility.is_cluster_database) from DUAL; fails with same message
SELECT CASE WHEN (dbms_utility.is_cluster_database) THEN 1 ELSE 0 END AS MY_BOOLEAN_COLUMN FROM DUAL; fails with message "SQL Error: ORA-00920: invalid relational operator"
I'm out of ideas how it can be fixed.
Problem is definitely not related to database access rights (since "dbms_output" solution works). Also, other fields from dbms_utility can be read using simple SELECT dbms_utility.<something> from dual;, eg. SELECT dbms_utility.get_endianness FROM DUAL;
i guess it's not possible to read boolean values in SQL, but you can write a wrapper function which would analyze that boolean and return varchar2/int/number.
alternatively, in your particular case you can do:
select value from v$parameter where name='cluster_database';
As suggest #MaxU you can create wrapper function.
create or replace function is_cluster_database return number is
begin
return sys.diutil.bool_to_int(dbms_utility.is_cluster_database);
end is_cluster_database;
And then used it like this
select is_cluster_database from dual;
Of course, result be number type.
is_cluster_database
0
Hello everyone, please help me again!
I could create the function but when I execute it, I always get the following error. Line 1, column 7 (I guess it's datatype of parameter) and statement ignored! :(
What you are using is to execute a function. Either assign the result to a variable or run sql query using dual
declare
x varachar2(100);
begin
x := fct1('dd');
end;
Alternatively you can also use
select fct1('dd') from dual;
I am working on ORACLE STORED PROCEDURES and I have a doubt.
I have a query which fetches more than 1 row and I want to store all those 3 row's values in 1 Variable.
Can anybody please help me with this.
My QUERY goes like this :
SELECT STUDENT_NAME
FROM STUDENT.STUDENT_DETAILS
WHERE CLASS_ID= 'C';
Here this query fetches 3 names
Jack,
Jill,
Bunny
I want all those 3 names to be stored in 1 variable i.e C_NAMES.
And after that I am using that variable in further steps of my procedure.
Can anyone please help me with this.
I would highly appreciate your time and effort.
Thanks in advance,
Vrinda :)
CREATE PROCEDURE a_proc
AS
CURSOR names_cur IS
SELECT student_name
FROM student.student_details
WHERE class_id = 'C';
names_t names_cur%ROWTYPE;
TYPE names_ntt IS TABLE OF names_t%TYPE; -- must use type
l_names names_ntt;
BEGIN
OPEN names_cur;
FETCH names_cur BULK COLLECT INTO l_names;
CLOSE names_cur;
FOR indx IN 1..l_names.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(l_names(indx).student_name);
END LOOP;
END a_proc;
Depending on your Oracle version(>= 11G(11.2)), you can use LISTAGG:
SELECT LISTAGG(STUDENT_NAME,',') WITHIN GROUP (ORDER BY STUDENT_NAME)
FROM STUDENT.STUDENT_DETAILS
WHERE CLASS_ID= 'C';
EDIT:
If your Oracle version is inferior to 11G(11.2), take a look here
Hi all and Thank you for your time.
I have resolved the question and all thanks to Ederson.
Here is the solution :
SELECT WM_CONCAT(STUDENT_NAME)
FROM STUDENT.STUDENT_DETAILS WHERE CLASS_ID= 'C';
Now if you are using this in a stored procedure or PLSQL you just have to create a variable and use SELECT INTO with it and print the variable.
Here is the code
DECLARE
C_NAMES VARCHAR2(100);
BEGIN
SELECT WM_CONCAT(STUDENT_NAME) INTO C_NAMES
FROM STUDENT.STUDENT_DETAILS WHERE CLASS_ID= 'C';
dbms_output.put_line(sname);
END;
Thanks again for your help people.
You'll need a cursor for that:
DECLARE
CURSOR stud_cur IS
SELECT STUDENT_NAME FROM STUDENT.STUDENT_DETAILS WHERE CLASS_ID= 'C';
l_stud STUDENT.STUDENT_DETAILS%ROWTYPE;
BEGIN
OPEN stud_cur;
LOOP
FETCH stud_cur INTO l_stud;
EXIT WHEN stud_cur%NOTFOUND;
/* The first time, stud_cur.STUDENT_NAME will be Jack, then Jill... */
END LOOP;
CLOSE stud_cur;
END;
I can't seem to get variables to work in an Oracle PL/SQL where clause. I come from a Microsoft SQL Server background and there it was easy. For example, what would be all steps needed to do something similar to the following?
declare #var int set #var = 1
select * from SomeTable where SomeField = #var
This doesn't seem like it should be hard in PL/SQL, but evidently it is. :-/ I hear I need to use cursors and the like in PL/SQL?
Any help would be greatly appreciated. Thanks.
What do you want to do with the data that the SELECT returns? If you just want to see it you don't need PL/SQL at all, just do this in SQL Plus:
variable var number
exec :var := 1
select * from SomeTable where SomeField = :var;
Or in a tool like SQL Developer or Toad, just do this:
select * from SomeTable where SomeField = :var;
and it will prompt you to enter the value for :var.
The following code declares a variable var to use in the WHERE clause, and a variable result to put the result in then executes it inside a PL/SQL block.
DECLARE
var INT := 1;
result INT;
BEGIN
SELECT 123
INTO result
FROM DUAL
WHERE var = 1;
DBMS_OUTPUT.put_line (var);
DBMS_OUTPUT.put_line (result);
END;
The DBMS_OUTPUT.PUT_LINE calls make it produce this DBMS output:
1
123
declare
type t_rec is record
(
col1 number,
col2 myTable.col2%type
);
v_rec t_rec;
type t_tab is table of v_rec%type index by binary_integer;
v_tab t_tab;
begin
select col1, col2
bulk collect into v_tab
from myTable
where col3 = 'BLAH';
-- do something great with v_tab...
end;
Also know that if you try to select into (or bulk collect into) a variable and no rows are returned, you'll get a no_data_found exception, so you may want to handle that situation.
See more here on pl/sql collections. Above uses an associative array, but there are nested tables and varrays as well. Again, see the link.
Hope that helps.
I use it like this
select * from sec_mainmenu where serno = '&MENU_ID';
When you run it, pl/sql will prompt for MENU_ID value.