473,609 Members | 2,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic SQL in PL/SQL, DBMS problem

22 New Member
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_c ursor; -- 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.execut e(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_t ab;
l_dummy NUMBER;
l_value NUMBER;
BEGIN
cur_hdl := dbms_sql.open_c ursor; -- 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.descri be_columns(cur_ hdl, l_dummy, l_columns);
FOR i IN 1..l_columns.co unt LOOP
dbms_sql.define _column(cur_hdl , i, l_columns(i).co l_name, l_columns(i).co l_type);
END LOOP;
--

--rows_processed := dbms_sql.execut e(cur_hdl);
l_dummy := dbms_sql.execut e_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.co unt 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 3491
cuqsy0
22 New Member
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
1842
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 after pressing "more" button. It does not work and I have no idea how to solve it. import re from WebKit.Page import Page from FormKit import Form, Fields, Validators from FormKit.FormKitMixIn import FormKitMixIn
4
10600
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 group and its experts for answers. So : - I've got a main page (main.aspx) - On this page, I've got a button (btnArchive), on whose click I dynamically add a UserControl (archive.ascx) to the main page.
1
1263
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. Also in the form is a datagrid that has some custom validation performed on values within its rows. The validation is server side but not tied into any ValidatorControls. When the form is submitted, if the datagrid contains values that are invalid...
11
3035
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
1285
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 instaciated and put it on a TabPage also created at runtime How can I achieve this goal ?
0
5767
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 future upgrade, and this module won't work (other Perl scripts in IIS6 are OK, just not this Interbase module, which is critical). The error comes from Dynaloader.pm file, which I've read through, and it says that it's a generic message about external...
1
350
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 my dll I use the invoke function. It works well. However, I would like, if there is a runtime error in my vb.net
0
1114
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 disply Column based on number of weeks in a MONTH. e.g. July have 5 weeks so total number of Columns shopuld be 5 (or if a month has 4 weeks then disply 4 columns)
0
1340
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 check the checkbox at runtime nothing happens (checkBoxObj.check is always false). Does anyone have information on how to make this work? Note: I can get it to work inside Page_Load event. I need help in getting it to work in different event...
1
6013
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 check the checkbox at runtime nothing happens (checkBoxObj.check is always false). Does anyone have information on how to make this work? Note: I can get it to work inside Page_Load event. I need help in getting it to work in a different event...
0
8131
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8573
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8543
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8223
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7005
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4023
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4089
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2535
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.