Dynamic SQL with in bulk in the record type
Oracle 10.2 gI received this Tom
[http://asktom.oracle.com/pls/apex/f?p=100:11:0:NO:]
I'm able to do this without dynamic SQL, but my requirement is to do it in dynamic SQL
create table t1 ( x int, y int );
insert into t1 select rownum, rownum+1 from all_users where rownum <= 5;
create table t2 ( x int, y int, z int );
declare
type array is table of t1%rowtype;
l_data array;
begin
select * bulk collect into l_data from t1;
forall i in 1 .. l_data.count
execute immediate 'insert into (select x, y from t2) values :x' using l_data(i);
end;
Error at line 1
ORA-06550: line 9, column 90:
PLS-00457: expressions have to be of SQL types
ORA-06550: line 9, column 20:
PL/SQL: Statement ignored
There is a work around in 11g, but can we do something in 10g?Thank you
HESH.
HESH wrote:
but following does not.
declare type array is table of t1%rowtype; l_data array; begin select * bulk collect into l_data from t1; forall i in 1 .. l_data.count execute immediate 'insert into (select x, y from t2) values :x' using l_data(i); end;
I want just a dynamic SQL code for the insert with FORALL statement would adopt as well as collections.
Doesn't make much sense.
Extract you the data from the SQL engine in the table of the record type. If the output data that cursor SQL must be read in the SQL engine and copied into the memory of PL/SQL engine.
Then, you send that VERY SAME DATA back to the SQL engine to be used by a SQL insert cursor.
Where is the logic behind the extraction of data from SQL in a PL/SQL table structure and then push this same structure table on the SQL engine database? What is the purpose to send data on a detour of underperforming and non-scalale through the PL/SQL engine?
You have any justification (technical or functional wise) to back up this absurd approach?
Why this can be achieved using a single SQL cursor that does both the choice (extraction) and (in bulk) insertion - using the plain old INSERT... SELECT structure?
And if the insert is variable, then what? Create a dynamic INSERT... SELECT cursor and execute it (using bind values). This simple... Right?
Tags: Database
Similar Questions
-
How to use Bulk collect in dynamic SQL with the example below:
My Question is
Using of dynamic SQL with collection in bulkif we pass the name of the table as "to the parameter' function, I want to display those
An array of column names without vowels (replace the vowels by spaces or remove vowels and display).
Please explain for example.
Thank you!!It's just a predefined type
SQL> desc sys.OdciVarchar2List sys.OdciVarchar2List VARRAY(32767) OF VARCHAR2(4000)
You can just as easily declare your own collection type (and you are probably better served declaring your own type of readability if nothing else)
SQL> ed Wrote file afiedt.buf 1 CREATE OR REPLACE 2 PROCEDURE TBL_COLS_NO_VOWELS( 3 p_owner VARCHAR2, 4 p_tbl VARCHAR2 5 ) 6 IS 7 TYPE vc2_tbl IS TABLE OF varchar2(4000); 8 v_col_list vc2_tbl ; 9 BEGIN 10 EXECUTE IMMEDIATE 'SELECT COLUMN_NAME FROM DBA_TAB_COLUMNS WHERE OWNER = :1 AND TABLE_NAME = :2 ORDER BY COLUMN_ID' 11 BULK COLLECT 12 INTO v_col_list 13 USING p_owner, 14 p_tbl; 15 FOR v_i IN 1..v_col_list.COUNT LOOP 16 DBMS_OUTPUT.PUT_LINE(TRANSLATE(v_col_list(v_i),'1AEIOU','1')); 17 END LOOP; 18* END; SQL> / Procedure created. SQL> exec tbl_cols_no_vowels( 'SCOTT', 'EMP' ); MPN NM JB MGR HRDT SL CMM DPTN PL/SQL procedure successfully completed.
Justin
-
How to use the record type as a parameter IN PL/SQL procedure or package
Hi people,
I need help on the record as the OUT parameter type. I am able to get out a single line as a parameter, but not getting do not idea how to get a multi ranks as output parameter.
I have the code that works very well for a single line. Please see CODE1.
But when I try to get several lines, I'm failing to do. Please see the CODE2. I get the error of compilation as
Error report:
ORA-06550: line 11, column 35:
PLS-00487: Invalid reference to the variable "P_NAME.
ORA-06550: line 11, column 1:
PL/SQL: Statement ignored
06550 00000 - "line %s, column % s:\n%s".
* Cause: Usually a PL/SQL compilation error.
Any help or a sample execution of script would be really useful.
Thanks in advance.
YZ
--------------------------CODE1------------------------------------------
-------------------------Package Spec-------------------------------
CREATE OR REPLACE
PACKAGE xx_sample_pkg as
--
Xx_sample_table_rectype RECORD TYPE IS
(p_name varchar2 (40))
number of p_emp_id
);
PROCEDURE xx_sample_prc (xx_sample_rec1, OUT xx_sample_table_rectype);
END xx_sample_pkg;
------------------------------Package Body------------------------
create or replace
PACKAGE xx_sample_pkg AS BODY
--
PROCEDURE xx_sample_prc (xx_sample_rec1 OUT xx_sample_table_rectype) IS
BEGIN
SELECT ename, empno
IN xx_sample_rec1
FROM scott.emp
WHERE ename = 'SMITH ';.
END xx_sample_prc;
END xx_sample_pkg;
-------------------------------------------Execute----------------------
DECLARE
l_rec_type xx_sample_pkg.xx_sample_table_rectype;
BEGIN
dbms_output.put_line ('xx_sample_prc appeal');
xx_sample_pkg.xx_sample_prc (l_rec_type);
dbms_output.put_line ('YZ' | l_rec_type.p_name |') '|| l_rec_type.p_emp_id);
END;
---------------------------------------------------------------
-------------------------CODE2-------------------------------------------
-------------------------Package Spec-------------------------------
CREATE OR REPLACE
PACKAGE xx_sample_pkg as
--
Xx_sample_table_rectype RECORD TYPE IS
(p_name varchar2 (40))
number of p_emp_id
);
PROCEDURE xx_sample_prc (xx_sample_rec1, OUT xx_sample_table_rectype);
END xx_sample_pkg;
------------------------------Package Body------------------------
create or replace
PACKAGE xx_sample_pkg AS BODY
--
PROCEDURE xx_sample_prc (xx_sample_rec1 OUT xx_sample_table_rectype) IS
BEGIN
SELECT ename, empno
IN xx_sample_rec1
FROM scott.emp;
END xx_sample_prc;
END xx_sample_pkg;
-------------------------------------------Execute----------------------
DECLARE
l_rec_type xx_sample_pkg.xx_sample_table_rectype;
BEGIN
dbms_output.put_line ('xx_sample_prc appeal');
xx_sample_pkg.xx_sample_prc (l_rec_type);
for l_rec in 1.l_rec_type.p_name.count
loop
dbms_output.put_line ('YZ' | l_rec_type.p_name (l_rec) |) » '|| l_rec_type.p_emp_id (l_rec));
end loop;
end;
---------------------------------------------------------------
bb8c573a-6ca3-4d7c-90ed-e55c2df67201 wrote:
But now, my question would be why the record type could not be used? My understanding is missing some concept between use of type type array collection record vs. Please specify.
Do not confuse the folder with the collection.
SY.
-
Hello experts.
create type emp2_obj is object
(
objno number,
objname varchar2(20),
objdept number
);
create type emp2_objarr is table of emp2_obj;and
type emp2_rec is record
(
recno number,
recname varchar2(20),
recdept number
);
create type emp2_recarr is table ofemp2_rec
;Objects and types of records are created similar and have the same similar object. Is it only advisable to use registration type if you use a collection in PL/SQL type... Please advice
user13328581 wrote:
the only reason why I ask is because he asked during an interview
-Objects and the types of records are created similar and have the same similar object. Is it only advisable to use registration type if you use a collection in PL/SQL type... Please advice
Not at all. You can use record types used with collection types outside. They are a group concept. If you want to group a set of values together (including fields), you can use a record. It may be convenient. You can send documents around proc and functions.
Be aware that the record types are limited to procedures, functions, anonymous blocks and packages (i.e. PL/SQL), while the types of objects are stored in the dictionary of data as a separate Oracle objects (they can also have methods, the records may not). Object types can be used in SQL, as said sol.beach types of records (outside of the intelligent pipeline situations) is for PL/SQL.
-
concatenate the fields of the record type
Hello
Is it possible to concatenate the fields of the record type?, example
I have a tabledeclare TYPE customer_sales_rectype IS RECORD (campo1 char(3), campo2 char(3), campo3 char(3)); TYPE ZZZ IS RECORD (kkk customer_sales_rectype); x customer_sales_rectype; begin x.campo1:='000'; x.campo2:='aaa'; x.campo3:='BBB'; end;
ID_REPORT NUMBER
My charly had more than 50 columns and unavoidable to concatenate the columns, example:
ID_LINE NUMBER
DT_FILE DATE Y
DS_LINE VARCHAR2(2048) Yselect column001 ,
I think that movement query result in a variable of type record that I would can insert in the table in the column is DS_LINE some transformatcion.
';' PV1,
COL2,
COL3
COL4,
';' PV2,....ETC
FROM MY TABLES
Is it possible without using COL1 | PV1. COL2. COL3 | COL4 | PV2... etc.
Thanks in advance
using 9.2.02It's pretty easy to write a function for this. Here's a 'coffee time ': implementation
create or replace function multi_concat (p_args sys.dbms_debug_vc2coll) return clob as rv clob; begin dbms_lob.createtemporary(rv, TRUE); for i in p_args.first .. p_args.last loop dbms_lob.writeappend(rv, length(p_args(i)), p_args(i)); end loop; return rv; end multi_concat; /
And the proof of coffee is in the dip of the ring:
SQL> select multi_concat ( sys.dbms_debug_vc2coll ( ('Red Fish', 'Blue Fish', 1, 'Fish',2,'Fish')) 2* from dual MULTI_CONCAT(SYS.DBMS_DEBUG_VC2COLL('REDFISH','BLUEFISH',1,'FISH',2,'FISH')) -------------------------------------------------------------------------------- Red FishBlue Fish1Fish2Fish SQL>
I add a member TO_STRING function to your type and use something like the above, implement.
You can also use the data dictionary to generate a string that connects the attributes of your type with the concatenation operator.
Cheers, APC
-
Error in passing in the RECORD type in the API
Gurus,
Get the following error when I try and change from one type of RECORD in an API. I am in passage correctly?
Any help is appreciated.
Thank you
-Scott
Here is my error:
fnd_descr_flex_col_usage_pkg.load_row
*
ERROR at line 21:
ORA-06550: line 21, column 4:
PLS-00306: wrong number or types of arguments in the call to 'LOAD_ROW '.
ORA-06550: line 21, column 4:
PL/SQL: Statement ignored
Here is my anon block:
declare
Who_type RECORD TYPE IS
(
created_by NUMBER,
CREATION_DATE DATE,
last_updated_by NUMBER,
last_update_date DATE,
last_update_login NUMBER
);
v_who_type who_type;
date of v_sysdate;
Start
Select sysdate
in v_sysdate
Double;
v_who_type.created_by: = 0;
v_who_type. CREATION_DATE: = v_sysdate;
v_who_type.last_updated_by: = 0;
v_who_type.last_update_date: = v_sysdate;
v_who_type.last_update_login: = 0;
fnd_descr_flex_col_usage_pkg.load_row
(x_application_short_name = > 'SPL',)
x_descriptive_flexfield_name = > 'HR_LOCATIONS ',.
x_descriptive_flex_context_cod = > '441',.
x_application_column_name = > 'ATTRIBUTE5 ',.
x_who = > v_who_type,
x_end_user_column_name = > "District."
x_column_seq_num = > 10,
x_enabled_flag = > 'Y ',.
x_required_flag = > 'n',.
x_security_enabled_flag = > 'n',.
x_display_flag = > 'Y ',.
x_display_size = > 50,
x_maximum_description_len = > 50,
x_concatenation_description_le = > 25,
x_flex_value_set_name = > 50 characters,
x_range_code = > ",
x_default_type = > ",
x_default_value = > ",
x_runtime_property_function = > ",
x_srw_param = > ",
x_form_left_prompt = > "District."
x_form_above_prompt = > "District."
x_description = > ");
...sreese wrote:
Tubby,Im not asking for your help with this error. I want to define my own FILE type that mimics the call package so I can spend in my own variables.
Pretty sure I've described previously, there was a specific question with the answer that you do not understand?
>
The problem with this type of recording is that it contains all the 'who' columns that the application requires. When it is called from a package within the schema, the package has no difficulty arising from these data. When you call the package from an anon block, I have to pull my own values.
You did before declaring a LOCAL record type, then you need to reference to the PACKAGE of type folder, as I showed you... it makes you EF values even as you were in your first post.
I want to define the RECORD type of the manner in which it has been set to 'fnd_flex_loader_apis.who_type', but using my own variable and passing in the parameter. Make sense?
Thank you
ScottHope that helps.
-
Dynamic SQL with dynamic identifiers
Oracle 10gXE
I have a table that is used to track student attendance. If the student is present on a given day, a record is inserted with the student ID and the date they frequented.
presence of DESC
Name of Type Null
--------------- -------- ------
ID NOT NULL NUMBER
NUMBER OF STUDENT_ID
SITE_ID NUMBER
DATE OF ATTENDANCE_DATE
I want to view a report (in TOP) which shows all students for a given site, the days when school was in session for this month (not Saturday, Sunday) and a X for the student who participated in that day or a null value if they do not have.
ID # FNAME LNAME 3 4 5 6 7 10
38754636 POE JANE X X X
81248754 DOE KAYLA X X X X X
43127409 RAO JOHN X X X X X
In this example, days 3 and 7 of the month are from Monday to Friday and 8-9 have been excluded because they are on Saturday and Sunday.
I am trying to find a way to dynamically generate a query for any month/year and returning to a report. The obstacle is that given that I don't know in advance which days in a month will be excluded, the identifiers in my select statement are not fixed.
I found a query that returns a list of day numbers and dates for a given month. It looks like this:
SELECT LEVEL lv, TO_DATE (TO_CHAR (LEVEL, '09')
|| TO_CHAR (EXTRACT (TO_DATE('01-SEP-11') MONTHS), '09')
|| To_char (EXTRACT (TO_DATE('01-SEP-11') YEAR), ' 9999'), 'dd.mm.yyyy') mon_day
OF double WHERE ROWNUM < = EXTRACT (DAY OF LAST_DAY (TO_DATE('01-SEP-11')))
CONNECT BY LEVEL = ROWNUM
I combined this request with another brings my student attendance data and results that pivots in what I need. Then, I created a function that accepts the month and year as parameters and creates the combined request.
SELECT c.ID student_id, c.last_name, c.first_name,.
MAX (decode (mon_day, ' 01 - SEPT.-11', 'X', NULL)) '1', MAX (decode (mon_day, ' 02 - SEVEN.-11', 'X', NULL)) '2 ',.
MAX (decode (mon_day, ' 05 - SEPT.-11', 'X', NULL)) '5', MAX (decode (mon_day, ' 06 - SEPT.-11', 'X', NULL)) "6."
MAX (decode (mon_day, ' 07-SEP-11', 'X', NULL)) '7', MAX (decode (mon_day, ' 08 - SEPT.-11', 'X', NULL)) "8."
MAX (decode (mon_day, ' 09 - SEPT.-11', 'X', NULL)) '9', MAX (decode (mon_day, 12-SEP-11', 'X', NULL)) "12."
MAX (decode (mon_day, 13-SEP-11', 'X', NULL)) '13', MAX (decode (mon_day, 14-SEPT-11', 'X', NULL)) "14."
MAX (decode (mon_day, 15-SEP-11', 'X', NULL)) '15', MAX (decode (mon_day, 16-SEP-11', 'X', NULL)) "16."
MAX (decode (mon_day, 19-SEP-11', 'X', NULL)) '19', MAX (decode (mon_day, 20-7.-11', 'X', NULL)) "20."
MAX (decode (mon_day, 21-SEP-11', 'X', NULL)) "21", MAX (decode (mon_day, 22-SEP-11', 'X', NULL)) '22 ',.
MAX (decode (mon_day, 23-SEP-11', 'X', NULL)) '23', MAX (decode (mon_day, 26-SEP-11', 'X', NULL)) "26."
MAX (decode (mon_day, 27-SEP-11', 'X', NULL)) '27', MAX (decode (mon_day, 28-SEP-11', 'X', NULL)) "28."
MAX (decode (mon_day, 29-SEP-11', 'X', NULL)) '29', MAX (decode (mon_day, 30-SEP-11', 'X', NULL)) '30 '.
(SELECT level lv, TO_DATE (TO_CHAR (LEVEL, '09')
|| TO_CHAR (EXTRACT (TO_DATE('01-SEP-11') MONTHS), '09')
|| To_char (EXTRACT (TO_DATE('01-SEP-11') YEAR), ' 9999'), 'dd.mm.yyyy') mon_day
OF double WHERE ROWNUM < = EXTRACT (DAY OF LAST_DAY (TO_DATE('01-SEP-11')))
CONNECTION LEVEL = ROWNUM) a, b of attendance, student c
WHERE to_char(MON_DAY,'D') NOT IN (1.7)
AND A.mon_day = b.attendance_date
AND b.student_id = c.ID
Group of c.ID, student_id, c.last_name, c.first_name
Now I'm stuck on what I can pass this request in to get my result. What I've read, dynamic SQL method 4 would work if I incorporate Pro/C. I hope this isn't my only option. I have considered to try to do this in a function table in pipeline, but since the columns returned are not known in advance, I can't create a corresponding data type.
I'm starting to wonder if I forgot a simpler method to achieve this.
Published by: David Sumner on April 12, 2012 18:52Ignoring the actual query and looking at the dynamic SQL part of the question.
There are 3 ways to run a dynamic select SQL in PL/SQL code.
immediate execution requires a fixed number of bind variables, linking him in position and translates a single implicit output cursor extraction - requiring the projection of cursor to be known at the time of coding.
REF CURSOR are essentially the same - the only exception being that it requires an extraction explicit coding process output cursor.
DBMS_SQL differs in all of these respects. The connection is by name and not position. The connection is dynamic. Projection of the cursor must not be known at the time of coding. It is dynamically determined at runtime and fetch offers of release of the cursor with the projection of unknown SQL - coding time.
Apex uses DBMS_SQL internally (in fact he uses DBMS_SYS_SQL properly run dynamic and specific patterns of Oracle SQL, as if a session connected as a scheme runs the SQL code). This allows the Apex to take a dynamic SQL that you enter a region (which can contain a variable number of bind variable), run it and make the projection of the cursor as an HTML report.
Apex supports in turn also dynamic SQL - what you (Apex developer) to provide a dynamic SQL code for a region, instead of you having to provide SQL fixed (with bind variable) for the region in question.
As Apex creates DBMS_SQL sliders, it doesn't have your SQL as a ref cursor or cursor DBMS_SQL. All they need is the source of the SQL statement. And that's what you create dynamically.
So in the Apex, dynamic SQL means using a function from PL/SQL to return the source for Apex SQL analyze like a slider DBMS_SQL - instead of coding the source SQL statement in a fixed statement.
So, using the option of function for an area considered in the Apex, allows you to provide the following in the code that must run to get the SQL statement for the report Apex (Apex running this as a dynamic function that returns a string):
--// return the dynamic SQL for the report region return( case when :P1_OPTION = 1 then 'select * from emp' when :P1_OPTION = 2 then 'select * from dept where dept_id = :P1_DEPT_ID' end );
You can also write a PL/SQL function that is stored in the database and call this function so that it can determine what is the dynamic SQL source code statement. For example
--// calling database function to return the dynamic SQL for the report region return( GetDynamicReportSQL( page => 1, option => :P1_OPTION ) );
Dynamic SQL is fully supported by Apex - but does not require that you, the developer, to create the dynamic cursor and treat the dynamic linking and dynamic recovery of this slider.
All you have to do is to provide the source code of this dynamic SQL (including the bind variable) to the Apex and it will do the whole thing from cursor for you.
-
Bulk collect into the record type
Sorry for the stupid question - I do something really simple wrong here, but can not understand. I want to choose a few rows from a table in a cursor, then in bulk it collect in a folder. I'll possibly extended the record to include additional fields that I will select return of functions, but I can't get this simple test case to run...
PLS-00497 is the main error.
Thanks in advance.create table test ( id number primary key, val varchar2(20), something_else varchar2(20)); insert into test (id, val,something_else) values (1,'test1','else'); insert into test (id, val,something_else) values (2,'test2','else'); insert into test (id, val,something_else) values (3,'test3','else'); insert into test (id, val,something_else) values (4,'test4','else'); commit; SQL> declare 2 cursor test_cur is 3 (select id, val 4 from test); 5 6 type test_rt is record ( 7 id test.id%type, 8 val test.val%type); 9 10 test_rec test_rt; 11 12 begin 13 open test_cur; 14 loop 15 fetch test_cur bulk collect into test_rec limit 10; 16 null; 17 exit when test_rec.count = 0; 18 end loop; 19 close test_cur; 20 end; 21 / fetch test_cur bulk collect into test_rec limit 10; * ERROR at line 15: ORA-06550: line 15, column 38: PLS-00497: cannot mix between single row and multi-row (BULK) in INTO list ORA-06550: line 17, column 21: PLS-00302: component 'COUNT' must be declared ORA-06550: line 17, column 2: PL/SQL: Statement ignored
You must declare an array based on your registration type.
DECLARE CURSOR test_cur IS SELECT id, val FROM test ; type test_rt IS record ( id test.id%type, val test.val%type); type test_rec_arr is table of test_rt index by pls_integer; test_rec test_rec_arr; BEGIN OPEN test_cur; LOOP FETCH test_cur bulk collect INTO test_rec limit 10; NULL; EXIT WHEN test_rec.count = 0; END LOOP; CLOSE test_cur; END; 31 / PL/SQL procedure successfully completed. Elapsed: 00:00:00.06 ME_XE?
Notice that the difference is...
type test_rec_arr is table of test_rt index by pls_integer; test_rec test_rec_arr;
-
Hello
I have a package that returns a Ref Cursor, in this procedure, I have a dynamic sql code that is built according to certain values, and the query is a select query, is it possible that I can put that dynamic sql in the ref cursor and return of the procedure.
Or y at - it no alternative better workaround.
Thanks in advance.
NaveenYes you can.
Try this...
create or replace package test_pack is type ref_cur is ref cursor; procedure just_print(ref_var ref_cur); end; / create or replace package body test_pack is procedure just_print(ref_var ref_cur) is l_var emp%rowtype; begin loop fetch ref_var into l_var; exit when ref_var%notfound; dbms_output.put_line(l_var.ename); end loop; end; end; / declare cur_var test_pack.ref_cur; dsql varchar2(100); begin dsql := 'select * from emp where deptno=10'; open cur_var for dsql; test_pack.just_print(cur_var); end; / CLARK KING MILLER PL/SQL procedure successfully completed. Elapsed: 00:00:00.00
Kind regards
Prazy -
Problem with Windows Explorer and the recorded files
When I save files from the internet and then open the folder with Windows Explorer in the backed up files, I find that WE consider not save them on my hard drive "change" more. I know it's strange, but I tend to retain files roughly in the order that I saved them, rather than by the name of the file or anything else. So now to manually change the files in a certain way, then save them to get them organized as I want. Of course, this can become very painful and long if I have a lot of files to organize this way. My question is: How can I get Windows to consider saving a file on my HDD as change? I use Firefox and updated recently, which may be the cause of the problem.
Hi Flingle,
Were you able to save and access files correctly before you update Firefox?
You can create a new folder & try to download the file inside of Internet Explorer & check if the problem persists.
Hope the helps of information. Please post back and we do know.
Concerning
Joel S
Microsoft Answers Support Engineer
Visit our Microsoft answers feedback Forum and let us know what you think. -
HelloI have a requirement of the company, where I need to return a record type (OUT parameter) for environment call based on the given input value.
Suppose that if the value is correct and corresponding record is found in the table then the return values for this key entry. If matching record is found, then return the exception to the calling environment.
To do this, I created an example of test table and populated records.
create table plch_test(dept_id number,dept_name varchar2(50),cost_centre number); insert into plch_test values(10,'SALES',1010); insert into plch_test values(20,'FINANCE',2010); insert into plch_test values(30,'MKTG',3010); SQL> select * from plch_test; DEPT_ID DEPT_NAME COST_CENTRE ---------- -------------------------------------------------- ----------- 10 SALES 1010 20 FINANCE 2010 30 MKTG 3010
I wrote a simple block and gave a valid key dept_id (10 in this case) to display costcentre for this dept_id and dept_name I said tow types of records, one for valid record and another exception
SQL> DECLARE 2 TYPE rec_dept IS RECORD(dept_name varchar2(50),cc number); 3 l_rec_dept rec_dept; 4 TYPE rec_exception IS RECORD(err_code number,error_message varchar2(300)); 5 l_rec_exception rec_exception; 6 BEGIN 7 SELECT dept_name,cost_centre 8 INTO l_rec_dept 9 FROM plch_test 10 where dept_id=10; 11 dbms_output.put_line('DEPT_NAME'||' '||l_rec_dept.dept_name||' '||'COSTCENTRE'||' '||l_rec_dept.cc); 12 EXCEPTION WHEN NO_DATA_FOUND THEN 13 l_rec_exception.err_code:=sqlcode; 14 l_rec_exception.error_message:=sqlerrm; 15 dbms_output.put_line(l_rec_exception.err_code||' '||l_rec_exception.error_message); 16 END; 17 . SQL> / DEPT_NAME SALES COSTCENTRE 1010 PL/SQL procedure successfully completed. SQL>
Now for invalid dept_id and expose the message by using exception record type I stated.
SQL> ed Wrote file afiedt.buf 1 DECLARE 2 TYPE rec_dept IS RECORD(dept_name varchar2(50),cc number); 3 l_rec_dept rec_dept; 4 TYPE rec_exception IS RECORD(err_code number,error_message varchar2(300)); 5 l_rec_exception rec_exception; 6 BEGIN 7 SELECT dept_name,cost_centre 8 INTO l_rec_dept 9 FROM plch_test 10 where dept_id=40; --Invalid --data is not present 11 dbms_output.put_line('DEPT_NAME'||' '||l_rec_dept.dept_name||' '||'COSTCENTRE'||' '||l_rec_dept.cc); 12 EXCEPTION WHEN NO_DATA_FOUND THEN 13 l_rec_exception.err_code:=sqlcode; 14 l_rec_exception.error_message:=sqlerrm; 15 dbms_output.put_line(l_rec_exception.err_code||' '||l_rec_exception.error_message); 16* END; SQL> / 100 ORA-01403: no data found PL/SQL procedure successfully completed.
Now as you can see I need to include this point in a procedure with an input parameter and output must be a record types which will return
rec_dept if it becomes a key input valid or an exception if she meets a key not valid.
CREATE PROCEDURE test_prc IS(p_in_dept_id IN plch_test.dept_id,p_output ?????? DECLARE TYPE rec_dept IS RECORD(dept_name varchar2(50),cc number); l_rec_dept rec_dept; TYPE rec_exception IS RECORD(err_code number,error_message varchar2(300)); l_rec_exception rec_exception; BEGIN BEGIN SELECT dept_name,cost_centre INTO l_rec_dept FROM plch_test where dept_id=p_ind_dept_id; RETURN l_rec_dept; EXCEPTION WHEN NO_DATA_FOUND THEN l_rec_exception.err_code:=sqlcode; l_rec_exception.error_message:=sqlerrm; RETURN l_rec_exception; END; dbms_output.put_line('DEPT_NAME'||' '||l_rec_dept.dept_name||' '||'COSTCENTRE'||' '||l_rec_dept.cc); END;
Hope that the explanation above help in imposes the requirement
Kind regards
Claudy kotekal
Return a record which can mean two things is complicated; I'm not an experienced myself pl/sql developer, but this looks like a craft.
The idea of exceptions under Sir Thomas of Kyte, is that any treatment must be stopped; You should RAISE an exception to the appellant so that he can figure out what to do with it. What you are saying, this is an exception, but is not a little, cos it's okay, I'll just keep but I will go back to the appellant in any way, but the appellant shall include this registration type is - would it be a record representing a row of the table, or it might be an exception... yuck.
(a) is it really an exception
(b) what do you do with it? You he could log into a table, you could write to a file, you can display an error message on the screen
But really, it's weird to want to pass an exception as return value.
These are all considerations of design, not really anything to do with the pl/sql language in itself.
But hard, if you send a record type a successful being found, registration-based stick to it and don't use it to return a record; do not try to do double duty with her flipping something else. Just save the message put in a table, or print it to the console, or what you want to do with; but as I said, the most important decision is, is this really an exception. And is based on the data model and the expectations of cleanliness of the data etc.
Think about how you call built-in functions. If you send garbage to a built-in function it does not return successfully, leaving you to figure out whether he succeeded or not by inspecting the return value; It goes kaboom, something bad happened. That's what your function should do if something bad happens, that is to say, if you get an exception, it should probably go kaboom.
-
Hi all
I created a table with two columns method. I am assigned two values to variable recordtype.
When inserting the record in the table, a compiler error is to be there.
ERROR on line 11:Create table emp_info(empno number(5),ename varchar2(30)); DECLARE l_rec emp_info%rowtype; BEGIN l_rec.empno := 101; l_rec.ename := 'KING'; insert into emp_info(empno,ename) values(l_rec); commit; END;
ORA-06550: line 10, column 35:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 10, column 1:
PL/SQL: SQL statement ignored
Can I insert with output Recordtype variables indicating the column names. Can someone help me?Do not list of columns. Do not put brackets in the record variable:
SQL> DECLARE 2 l_rec emp_info%rowtype; 3 BEGIN 4 l_rec.empno := 101; 5 l_rec.ename := 'KING'; 6 insert 7 into emp_info 8 values l_rec; 9 commit; 10 END; 11 / PL/SQL procedure successfully completed. SQL>
SY.
-
the associative arrays containing the record type, cannot be used first
I am having trouble with the declaration of an associative array containing the Types of records and iteration using FIRST and NEXT functions.
This problem of mine is only appear when I use that types of records, the FIRST and FOLLOWING operators work very well when you use the regular NUMBER.
Trying to get the first element of the array, I get: ORA-06550: line 22, column 40: PLS 00382: expression is of the wrong type
See the code snippet below. Anyone know if this can be done in PL/SQL?
-----------------------------------------------------------------------------------------
DECLARE
-Set the record structure that will contain information on a post
TYPE PostRec IS (RECORD
post_type VARCHAR2 (4) - maybe DEB/CRED
);
lr_charge_back_post PostRec;
TYPE post_table IS TABLE OF PostRec NOT NULL
INDEX BY VARCHAR2 (4);
assoc_posts post_table;
BEGIN
-lr_charge_back_post.post_type: = "asd";
assoc_posts('1').post_type: = '1';
lr_charge_back_post: = assoc_posts.first;
END;
/Returns the index, not the file FIRST:
SQL> declare 2 TYPE PostRec IS RECORD ( 3 post_type VARCHAR2(4) -- Can be DEB/CRED 4 ); 5 6 lr_charge_back_post varchar2(20); 7 TYPE post_table IS TABLE OF PostRec NOT NULL 8 INDEX BY VARCHAR2(4); 9 10 11 assoc_posts post_table; 12 13 BEGIN 14 15 16 --lr_charge_back_post.post_type := 'asd'; 17 assoc_posts('1').post_type := '1'; 18 19 lr_charge_back_post := assoc_posts.first; 20 dbms_output.put_line('idx='||lr_charge_back_post); 21 END; 22 / idx=1 PL/SQL procedure successfully completed. SQL> declare 2 TYPE PostRec IS RECORD ( 3 post_type VARCHAR2(4) -- Can be DEB/CRED 4 ); 5 6 lr_charge_back_post varchar2(20); 7 TYPE post_table IS TABLE OF PostRec NOT NULL 8 INDEX BY VARCHAR2(4); 9 10 11 assoc_posts post_table; 12 13 BEGIN 14 15 16 --lr_charge_back_post.post_type := 'asd'; 17 assoc_posts('idx').post_type := '1'; 18 lr_charge_back_post := assoc_posts.first; 19 dbms_output.put_line('idx='||lr_charge_back_post); 20 END; 21 / idx=idx PL/SQL procedure successfully completed.
Max
http://oracleitalia.WordPress.com -
SQL query help needed in the Clause type
Hello
I'm currently learning Clause type. How can I write in the clause type to get the result below:
I have a table like this
DEPTNO ENAME
------- ------
20 SMITH
CELINE 10
BOND 20
ALLEN 30
WARD 30
20 JONES
30 MARTIN
30 BLAKE
10 CLARK
SCOTT 20
10 KING
30 TURNER
20 ADAMS
30 JAMES
20 FORD
10 MILLER
I want the output should look like this
DEPTNO ENAME
------ -----
CELINE 10, CLARK, KING, MILLER
20 JONES, ADAMS, BOND, FORD, SCOTT, SMITH
30 MARTIN, JAMES BLAKE, WARD, TURNER, ALLEN
Hear is the script I am currently working on.
with emp_group as
(
Select 20 deptno, ename 'SMITH' Union double all the
choose 10, 'CÉLINE' from dual union all
Select 20, "BOND" of all the double union
Select 30, 'ALLEN' from dual union all
Select 30, 'WARD' of all the double union
Select 20, "JONES' from dual union all
Select 30, 'MARTIN' from dual union all
Select 30, "BLAKE" from dual union all
choose 10, 'CLARK' from dual union all
Select 20, 'SCOTT' from dual union all
choose 10, 'KING' of the dual union all
Select 30, 'TURNER' from dual union all
Select 20, 'ADAMS' from dual union all
Select 30, 'JAMES' of the dual union all
Select 20, 'FORD' Union double all the
choose 10, 'MILLER' from dual
)
Select
DEPTNO
A1
of emp_group
model
ignore the nav
partition (deptno)
dimension (ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename DESC) rn)
measures (ename, LPAD(' ', 100) A1)
rules)
A1 [rn > 0] = ename [cv ()],
A1 [0] = ename [cv ()]
)
order by deptno
;
your help is very appreciated.
Thank you in advance.
Try this.
Select
*
Of
(
with emp_group as
(
Select 20 deptno, ename 'SMITH' Union double all the
choose 10, 'CÉLINE' from dual union all
Select 20, "BOND" of all the double union
Select 30, 'ALLEN' from dual union all
Select 30, 'WARD' of all the double union
Select 20, "JONES' from dual union all
Select 30, 'MARTIN' from dual union all
Select 30, "BLAKE" from dual union all
choose 10, 'CLARK' from dual union all
Select 20, 'SCOTT' from dual union all
choose 10, 'KING' of the dual union all
Select 30, 'TURNER' from dual union all
Select 20, 'ADAMS' from dual union all
Select 30, 'JAMES' of the dual union all
Select 20, 'FORD' Union double all the
choose 10, 'MILLER' from dual
)
Select
DEPTNO
names
rn
of emp_group
model
ignore the nav
partition (deptno)
dimension of)
ROW_NUMBER() over (PARTITION BY deptno ORDER BY ename) rn
)
measures (ename, cast (null as names of varchar2 (40)))
rules
(
names [any] order by desc = ename [cv ()] rn | «, » || names [cv () + 1]
)
order by deptno
)
where rn = 1
Best regards, Andrei
-
Limit the "Record Type" options in the section search for presentation of the action bar
Is it possible to limit the options of 'Record Type' in the 'Search' section in the disposition of the action bar for users specific role.
Thank you!Mahesh, if you uncheck the box to go to step 2 of the role that you would restrict access to these types of records in the section of the application and research in the action bar.
Maybe you are looking for
-
Redirects of Safari and ransom sites
Within the last few days my iMac was prey to redirects and popups of websites of ransom. Redirects are all trying to read stories of legitimate websites like CNN.com, etc.. When I click on a link to read a story, I see the random URL displayed in t
-
HP Pavilion 15-n259TX (two Usb ports on the left does not work)
I recently lowered the windows note 8 for windows 7 and found that the two usb ports on the left side of my laptop no longer works, that is, they do not detect anything, be pendrives or external hard drive. Can you help me fixed the problem?My laptop
-
Strange behavior of Bluetooth service
Hello I have one strange problem on my other laptop manufacturing. Since yesterday everything was fine, there is no problem. Yesterday, I added a new Windows account - for my wife. Here's the problem: in his account, the TosOBEX.exe consumes about 20
-
Uninstall Upgrade Advisor under XP Pro SP3
I have somehow watered the original Upgrade Advisor on my XP SP3 machine. Using Add/Remove programs cannot remove the program. The program runs as grateful there is an update, but when I try to install the update it stops because it can not remove
-
My Packard Bell was brocken and I changed the motherboard also, I lose my home edition of windows vista.I do not have a program.What should do?