473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Dynamic SQL in PL/SQL, DBMS problem

22
Hi Experts,

I need to develop a dynamic SQL in PL SQL to query an unknown number of columns. Let me take a sample query here:
SELECT FIRST_NAME, LAST_NAME FROM VENDOR_CONTACTS
With "known number of column" method (in this case we have known that we are querying two columns: "FIRST_NAME" and "LAST_NAME"), I write a code based on the template in table 8-2 of http://docs.oracle.com/cd/B10500_01/...9dyn.htm#26586. The query works fine.
--------------------------------------------------------------------------
DECLARE
stmt_str varchar2(200);
cur_hdl int;
rows_processed int;
FIRST_NAME varchar2(200);
LAST_NAME varchar2(200);
BEGIN
cur_hdl := dbms_sql.open_cursor; -- open cursor
stmt_str := 'SELECT FIRST_NAME, LAST_NAME FROM VENDOR_CONTACTS';

dbms_sql.parse(cur_hdl, stmt_str, dbms_sql.native);

-- describe defines
dbms_sql.define_column(cur_hdl, 1, FIRST_NAME, 200);
dbms_sql.define_column(cur_hdl, 2, LAST_NAME, 200);

rows_processed := dbms_sql.execute(cur_hdl);
--execute

LOOP
-- fetch a row
IF dbms_sql.fetch_rows(cur_hdl) > 0 then

-- fetch columns from the row
dbms_sql.column_value(cur_hdl, 1, FIRST_NAME);
dbms_sql.column_value(cur_hdl, 2, LAST_NAME);

dbms_output.put_line('First Name' ||' = '||FIRST_NAME || '; Last Name' ||' = '||LAST_NAME);


-- <process data>

ELSE
EXIT;
END IF;
END LOOP;

dbms_sql.close_cursor(cur_hdl); -- close cursor
END;
/
--------------------------------------------------------------------------

However, for "unknown number of columns" method, following http://stackoverflow.com/questions/1...l-from-sqlplus, my code for the same query has error.

--------------------------------------------------------------------------
DECLARE
stmt_str varchar2(200);
cur_hdl int;
rows_processed int;

l_columns dbms_sql.desc_tab;
l_dummy NUMBER;
l_value NUMBER;
BEGIN
cur_hdl := dbms_sql.open_cursor; -- open cursor
stmt_str := 'SELECT FIRST_NAME, LAST_NAME FROM VENDOR_CONTACTS';

dbms_sql.parse(cur_hdl, stmt_str, dbms_sql.native);

-- describe defines
--dbms_sql.define_column(cur_hdl, 1, FIRST_NAME, 200);
--dbms_sql.define_column(cur_hdl, 2, LAST_NAME, 200);
--
dbms_sql.describe_columns(cur_hdl, l_dummy, l_columns);
FOR i IN 1..l_columns.count LOOP
dbms_sql.define_column(cur_hdl, i, l_columns(i).col_name, l_columns(i).col_type);
END LOOP;
--

--rows_processed := dbms_sql.execute(cur_hdl);
l_dummy := dbms_sql.execute_and_fetch(cur_hdl, TRUE);
--execute

LOOP
-- fetch a row
IF dbms_sql.fetch_rows(cur_hdl) > 0 then

-- fetch columns from the row
-- dbms_sql.column_value(cur_hdl, 1, FIRST_NAME);
-- dbms_sql.column_value(cur_hdl, 2, LAST_NAME);

FOR i IN 1..l_columns.count LOOP
dbms_sql.column_value(cur_hdl, i, l_value);

-- dbms_output.put_line('First Name' ||' = '||FIRST_NAME || '; Last Name' ||' = '||LAST_NAME);
dbms_output.put_line(l_columns(i).col_name ||' = '||l_value);
END LOOP;

-- <process data>

ELSE
EXIT;
END IF;
END LOOP;

dbms_sql.close_cursor(cur_hdl); -- close cursor
END;
/
--------------------------------------------------------------------------
Thank for help in advance.

-Cuq
Jun 1 '12 #1
1 3483
cuqsy0
22
It is a very simple job to do this in Microsoft T-SQL, but it seems to be a very hard or impossible work in Oracle PL/SQL.
Jun 3 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: JZ | last post by:
I use Webware and FormKit. I have a problem with dynamic added field to the form. The following code creates one input field and two submit buttons. I would like to add more (up to 4) input fields...
4
by: Fueled | last post by:
Hi everyone! I've made quite a lot of research on this, and I've tried a couple of proposed solutions. Nothing has worked for me, but I feel there's not much I'm missing. So I'm turning to this...
1
by: Elliot Rodriguez | last post by:
I have a dropdown list that defines the number of textboxes that appear within a particular panel. The boxes are created when the dropdownlist's SelectedIndexChanged event fires on postback. ...
11
by: toton | last post by:
Hi, I have little confusion about static memory allocation & dynamic allocation for a cluss member. I have class like class Bar{ public: explicit Bar(){ cout<<"bar default"<<endl; }
1
by: Guillaume BRAUX | last post by:
Hello, Here is exactly what I want to do in my app : The main problem is that my app will work with a totaly dynamic GUI, so buttons, label and so on have to be created at runtime. not yet...
0
by: OverTheTop | last post by:
All the usual Newbie stuff applies... I have a Windows2000 Server with ActivePerl and a module (DBD-Interbase) that worked fine. Trying to duplicate the environment on Windows2003 Server for...
1
by: alexis.meilland | last post by:
Hello, Well, I have a problem with a dynamic debuging. My program is winform program written in Managed c++. I compile dynamically a dll in vb.net. It works well. To execute a function in...
0
by: palgre | last post by:
HI All, I want to disply dynamic Columns and Dynamic Header to that column in a GRID VIEW. The application is kind of TimeSheet for entering weekly hrs for a month. 1. First of all I want to...
0
by: =?Utf-8?B?SWRlcm9ja3M=?= | last post by:
Hi All, I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and I...
1
by: iderocks | last post by:
Hi All, I created a dynamic checkbox in ASP .Net inside a Button1_Click event method (outside the page_load event) and performed the event handling method for the CheckedChanged event and when I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.