473,387 Members | 1,578 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.

Best way to create a table

I need to create 101 columns table and populate it with following information:
RN,C1,C2,C3,C4,C5................................. .........C100
0 1 0 0 0 0.............................................. 0
1 0 1 0 0 0...............................................0
2 0 0 1 0 0...............................................0
3 0 0 0 1 0...............................................0
.................................................. ..........................
100 0 0 0 0 .................................................1

Where RN is row number.
1 is always in RN + 1 Position
What is the best way to do it to reduce manual typing.
Thank's in advance.
Leny G.

--
Message posted via http://www.dbmonster.com

Sep 16 '08 #1
5 1978
On Sep 15, 8:36*pm, "lenygold via DBMonster.com" <u41482@uwewrote:
I need to create 101 columns table and populate it with following information:

RN,C1,C2,C3,C4,C5................................. .........C100 *
0 * * 1 * 0 * *0 * 0 * 0............................................... 0
1 * * 0 * 1 * *0 * 0 * 0................................................0
2 * * 0 * 0 * *1 * 0 * 0................................................0
3 * * 0 * 0 * *0 * 1 * 0................................................0
.................................................. ..........................
100 0 * 0 * *0 * *0 *................................................. .1

Where RN is row number.
1 is always in RN + 1 Position
What is the best way to do it to reduce manual typing.
Thank's in advance.
Leny G.

--
Message posted viahttp://www.dbmonster.com
You need to create a stored procedure to compile a dynamic update.

First, insert 100 rows with all 0,

Second, create and run SP:
DECLARE count int default 0;
DECLARE stmt STATEMENT;
REPEAT
set count = count +1;
SET txt = 'update t set C' || char(count) || '=1 where RN =' ||
char(count)';
prepare stmt from txt;
execute stmt;
UNTIL (count >= 100)
END REPEAT;
Yonglei
Sep 16 '08 #2
Thank You.
I am done with insert 100 rows with all 0.
Tommorow will try SP.

yo******@gmail.com wrote:
>I need to create 101 columns table and populate it with following information:
[quoted text clipped - 14 lines]
>--
Message posted viahttp://www.dbmonster.com

You need to create a stored procedure to compile a dynamic update.

First, insert 100 rows with all 0,

Second, create and run SP:
DECLARE count int default 0;
DECLARE stmt STATEMENT;
REPEAT
set count = count +1;
SET txt = 'update t set C' || char(count) || '=1 where RN =' ||
char(count)';
prepare stmt from txt;
execute stmt;
UNTIL (count >= 100)
END REPEAT;

Yonglei
--
Message posted via http://www.dbmonster.com

Sep 16 '08 #3
Well, I'd just write something like this in perl:

for ($i=0; $i<=100; $i++) {
print "$i,";
for ($j=0; $j<$i; $j++) {
print "0,";
}
print "1,";
for ($j=$i+1; $j<99; $j++) {
print "0,";
}
print "0\n";
}

and then load the file... I'm still better at perl than at creating
stored procedures :-)

(Last line needs editing.)
Sep 16 '08 #4
lenygold via DBMonster.com wrote:
I need to create 101 columns table and populate it with following information:
RN,C1,C2,C3,C4,C5................................. .........C100
0 1 0 0 0 0.............................................. 0
1 0 1 0 0 0...............................................0
2 0 0 1 0 0...............................................0
3 0 0 0 1 0...............................................0
.................................................. .........................
100 0 0 0 0 .................................................1

Where RN is row number.
1 is always in RN + 1 Position
What is the best way to do it to reduce manual typing.
Thank's in advance.
Leny G.
It is almost never a good practice to create a table such as the one
you have indicated.

Give serious consideration to thinking vertically rather than horizontally.

RN NUMBER
CNUM NUMBER
CVAL NUMBER
--
Daniel A. Morgan
University of Washington
da******@x.washington.edu (replace x with u to respond)
Sep 16 '08 #5
DA is right, but if you ignore him and create the table anyways,
extending the following for all 100 columns would be an easy all SQL
way to accomplish this in DB2:

with recurse(rn, c1, c2, c3, c4, c5, c6) as (
select * from table(values(0,1,0,0,0,0,0)) t0
union all
select rn+1, 0, c1, c2, c3, c4, c5
from recurse
where rn < 6)
select count(*) from final table(insert into your_table select * From
recurse) ft

Also, note that your example didn't quite work - you either start with
row1 c1=1, or you start as you did (row 0) and row 100 has all 0's.

-Chris
Sep 17 '08 #6

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

Similar topics

16
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For...
2
by: Ben | last post by:
Right now I have 1 table. The first part is the first and last name along with address etc. There is about 10-15 fields here. The second part consists of times, penalties and if they enter this...
1
by: Chris Uwins | last post by:
Hi there, i know theres a number of ways I can achieve this but want to know the best, (but still quite simple). Up until a year ago I never used Access but have designed a few databases for...
0
by: Patrick | last post by:
I'm working on a contact management application, and need a hand with one aspect... Here's what I want to create: ------------------------------------ A form split into two parts. There is a...
20
by: Keith G. Murphy | last post by:
I'm trying to get a feel for what most people are doing or consider best practice. Given a mod_perl application talking to a PostgreSQL database on the same host, where different users are...
3
by: BPDudeMan | last post by:
Hi There, I've got one table that is constantly being added to (every few seconds). I've got a bunch of users that need to report from this table. What's the best way to setup the tables? ...
10
by: rAinDeEr | last post by:
Hi, I am trying to create around 70 tablespaces for around 100 tables.. Am using DB2 UDB 8.2 in Linux environment... This is one i generated through Control centre.... CREATE REGULAR...
7
by: Tzanko | last post by:
As we all know, there is a 8060 bytes size limit on SQL Server rows. I have a table which requires a number of text fields (5 or 6). Each of these text fields should support a max of 4000...
7
by: Steve | last post by:
I am building an object library for tables in a database. What is the best practice for creating objects like this? For example, say I have the following tables in my database: User: - Id -...
4
by: trullock | last post by:
Hi, Can anyone suggest the best way to go about the following... I'm tracking clicks (mouse down x,y coordinates) on a web page by using some javascript to create an XHR which sends the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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...

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.