473,387 Members | 1,492 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,387 software developers and data experts.

Need to dynamically generate a SQL SELECT which excludes NULL columns

I have a table with 100+ columns, for which I'm trying to retrieve
only 1 specific record. For this single record, I do not know which of
the columns are NULL, and which are populated.

I would like to create a dynamically-generated SELECT--limiting the
columns to only those that are populated. If, for example, only
columns COL1, COL8, and COL93 are populated for this one record in the
MYTEST table, the generated SELECT statement would be:
select COL1, COL8, COL93
from MYTEST
where COL1='current_value';

as opposed to:
select COL1, COL2, COL3, COL4, COL5, COL6 . . .
from MYTEST
where COL1='current_value';

In PL/SQL, I've been wrangling to do this--obtaining the list of
columns from USER_TAB_COLUMNS for the table, using a temporary table
to store both the column's value and the column's name for further
analysis, etc. However, this is seemingly cumbersome. Isn't there an
easier way to efficiently identify which columns are populated or not
for a specific row in a table?

Thanks in advance.
-Tom
Jul 19 '05 #1
4 15649
Tom Urbanowicz wrote:
I have a table with 100+ columns, for which I'm trying to retrieve
only 1 specific record. For this single record, I do not know which of
the columns are NULL, and which are populated.

I would like to create a dynamically-generated SELECT--limiting the
columns to only those that are populated. If, for example, only
columns COL1, COL8, and COL93 are populated for this one record in the
MYTEST table, the generated SELECT statement would be:
select COL1, COL8, COL93
from MYTEST
where COL1='current_value';

as opposed to:
select COL1, COL2, COL3, COL4, COL5, COL6 . . .
from MYTEST
where COL1='current_value';

In PL/SQL, I've been wrangling to do this--obtaining the list of
columns from USER_TAB_COLUMNS for the table, using a temporary table
to store both the column's value and the column's name for further
analysis, etc. However, this is seemingly cumbersome. Isn't there an
easier way to efficiently identify which columns are populated or not
for a specific row in a table?

Thanks in advance.
-Tom


It depends.

What problem are you REALLY trying to solve?

What is wrong with getting the whole row; including the nulls?

Jul 19 '05 #2

It certainly is possibly what you are trying to do.
But how would you know what columns the result is refering to.
I mean if the query retruns col1, col2 and col93, how would u know what
columns are we talking about.

Though I dont understand the purpose of this query, but I could give
it a shot.

--
Posted via http://dbforums.com
Jul 19 '05 #3
To clarify, if only 3 columns in a record are populated (in a table w/
100 columns), I don't want a query to retrieve 97 NULLs with 97 column
headings. Instead, I need SQL (or PL/SQL) that:
A) Identifies which columns in the table are populated
for a specific record.
B) Creates a SELECT statement using only those populated
columns in the list of columns.

I would be able to identify the columns in SQL*Plus, because I would
have the headings on. For example, the generated SQL and the result
set would be:

set heading on
SQL> select COL1, COL8, COL93
2 from MYTEST
3 where COL1='35';

COL1 COL8 COL93
---------- ---------- ---------
35 44 21-JUL-03
Thanks for your insights.

aruneeshsalhotr <me*********@dbforums.com> wrote in message news:<30****************@dbforums.com>...
It certainly is possibly what you are trying to do.
But how would you know what columns the result is refering to.
I mean if the query retruns col1, col2 and col93, how would u know what
columns are we talking about.

Though I dont understand the purpose of this query, but I could give
it a shot.

Jul 19 '05 #4

Originally posted by Tom Urbanowicz
To clarify, if only 3 columns in a record are populated (in a table w/
100 columns), I don't want a query to retrieve 97 NULLs with 97 column
headings. Instead, I need SQL (or PL/SQL) that:
A) Identifies which columns in the table are populated
for a specific record.
B) Creates a SELECT statement using only those populated
columns in the list of columns.

I would be able to identify the columns in SQL*Plus, because I would
have the headings on. For example, the generated SQL and the result
set would be:

set heading on
SQL> select COL1, COL8, COL93
2 from MYTEST
3 where COL1='35';

COL1 COL8 COL93
---------- ---------- ---------
35 44 21-JUL-03
Thanks for your insights.

aruneeshsalhotr wrote in message news:news:...
It certainly is possibly what you are trying to do.
But how would you know what columns the result is refering

to.
I mean if the query retruns col1, col2 and col93, how would u

know what
columns are we talking about.

Though I dont understand the purpose of this query, but I could

give
it a shot.

You want to do this for a SINGLE record, not a set of records?

If so then you can use DBMS_SQL to query the record and return each
column in turn. If it is NULL, skip it, otherwise output the column
name and value.

This procedure (based loosely on Tom Kyte's print_table procedure)
will do that:

create or replace procedure no_nulls( p_sql in varchar2 ) is
v_sql varchar2(32767) := p_sql;
v_cursor integer := dbms_sql.open_cursor;
v_value varchar2(4000);
v_status integer;
v_desctab dbms_sql.desc_tab;
v_numcols integer;
v_header1 varchar2(4000);
v_header2 varchar2(4000);
v_record varchar2(4000);
v_length integer;
begin

dbms_sql.parse( v_cursor, v_sql, dbms_sql.native );
dbms_sql.describe_columns( v_cursor, v_numcols, v_desctab );

for i in 1 .. v_numcols loop
dbms_sql.define_column(v_cursor, i, v_value, 4000);
end loop;

v_status := dbms_sql.execute(v_cursor);

while ( dbms_sql.fetch_rows(v_cursor) > 0 ) loop
v_header1 := '';
v_header2 := '';
v_record := '';
for i in 1 .. v_numcols loop
dbms_sql.column_value( v_cursor, i, v_value );
IF v_value IS NOT NULL THEN
IF v_desctab(i).col_type = 1 THEN -- Varchar2
v_length := v_desctab(i).col_max_len;
ELSIF v_desctab(i).col_type = 12 THEN -- Date
v_length := 11;
ELSE -- Assumes number!
v_length := v_desctab(i).col_precision+2;
END IF;
v_header1 := v_header1 || RPAD( v_desctab(i).col_name, v_length ) || ' ';
v_header2 := v_header2 || RPAD( '-', v_length, '-' ) || ' ';
v_record := v_record || RPAD( v_value, v_length ) || ' ';
END IF;
end loop;
dbms_output.put_line( v_header1 );
dbms_output.put_line( v_header2 );
dbms_output.put_line( v_record );
end loop;
end;
/

For example:

SQL> exec no_nulls('select * from emp where ename=''KING''')
EMPNO ENAME JOB HIREDATE SAL DEPT
------ ---------- --------- ----------- --------- ----
7839 KING PRESIDENT 17-NOV-1981 5000 10

PL/SQL procedure successfully completed.

The above code only handles VARCHAR2, DATE and NUMBER.

--
Posted via http://dbforums.com
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: muesliflakes | last post by:
I'm trying to generate a normalized XML document out of SQL server that reflects the data structure of a table. Eg. This is what I would like to get <table name='MtFeedback'> <field...
7
by: nicholas | last post by:
Hello, Got a kind of e-commerce site. There are products with product options, such as color, size, etc All are defined a table: tbl_products: the table with the products. tbl_options: the...
4
by: serge calderara | last post by:
Dear all, I need to build a web application which collects data from an SQL server database. SQL server database tables fields can be dynamically created or extended depending on my customer...
0
by: ward | last post by:
Greetings. Ok, I admit it, I bit off a bit more than I can chew. I need to complete this "Generate Report" page for my employer and I'm a little over my head. I could use some additional...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
6
by: Twobridge | last post by:
I hope someone can help me out with my problem. I have found a sql statement that basically pulls all bills filed within a certain time period and the payments made on those bills with in the...
4
by: Rob Meade | last post by:
Hi all, I played with my first bit of AJAX the other week and was pleasantly surprised that I achieved my goal..now I'd like to try something else.. Question... If I have an updatePanel,...
1
by: GayatriSharma1 | last post by:
I m having some tables in the database...depending upon the columns present in each table the grid should generate rows dynamically to display that columns in grid...how to generate columns...
4
by: Tom Urbanowicz | last post by:
I have a table with 100+ columns, for which I'm trying to retrieve only 1 specific record. For this single record, I do not know which of the columns are NULL, and which are populated. I would...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.