Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 8th, 2012, 12:23 PM
jvst jvst is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 7 jvst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 10 sec
Reputation Power: 0
Creating VB code to increment the number of fields in an MSSQL create table statement

hello everyone,

i have an SQL statement which i need to code in vb:

create table r2 (salesno bigint, prodno1 bigint ,prodno2 bigint )

insert into r2

select p.salesNo, p.prodNo, q.prodNo

from salesLog as p, salesLog as q

where q.salesNo = p.salesNo and q.prodNo>p.prodNo

notice the fields prodno1 and prodno2 in the table structure..

this is part of a number of sql statements i need to run and put inside a loop. my problem is i want to automatically use this same code such that on the next loop, this sql statement is going to be:

create table r3 (salesno bigint, prodno1 bigint ,prodno2 bigint, prodno3 bigint)

insert into r3
select p.salesNo, p.prodNo1, p.prodNo2, q.prodNo
from l2 as p, salesLog as q
where q.salesNo = p.salesNo and q.prodNo>p.prodNo2

Note that in this "2nd loop"

- the table name is now r3 from r2 in the first SQL statement
- there are now prodno1, prodno2 and prodno3 instead of just prodno1 and prodno2
- in the WHERE clause, the p.prodNo becomes p.prodNo2

and so on and so forth.. so for the 3rd loop

- there's going to be r4
- there's going to be prodno4
- in the WHERE clause i will use p.prodNo3

what should i do? is there an array or something that i can use here?

thank you in advance.

Reply With Quote
  #2  
Old May 8th, 2012, 12:36 PM
Doug G Doug G is offline
Grumpier Old Moderator
Dev Shed God 19th Plane (14000 - 14499 posts)
 
Join Date: Jun 2003
Posts: 14,257 Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level)Doug G User rank is General 52nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 4 Weeks 19 h 1 m 26 sec
Reputation Power: 4445
You should review your table structure. In my opinion it's always a bad idea to alter table structure from code, particularly in a loop.

You are probably looking for the traditional master/detail table structure, google for some table join tutorials at sites like www.w3schools.com
__________________
======
Doug G
======
It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi

Reply With Quote
  #3  
Old May 8th, 2012, 12:52 PM
jvst jvst is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 7 jvst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by Doug G
You should review your table structure. In my opinion it's always a bad idea to alter table structure from code, particularly in a loop.

You are probably looking for the traditional master/detail table structure, google for some table join tutorials at sites like


thank you for your reply, i really appreciate it. let me just clarify a few things about my problem. i don't actually intend to alter the table structure.

i would like to first create a table named r2, then on the third loop, automate the creation of another table named r3 with a little difference. so there are two tables created, the second with an additional field.

on the third loop, it will create a third table r4, then with similar structure (just a difference of one more field).. and so on...

Reply With Quote
  #4  
Old May 8th, 2012, 12:55 PM
jvst jvst is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 7 jvst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 10 sec
Reputation Power: 0
Quote:
Originally Posted by Doug G
You should review your table structure. In my opinion it's always a bad idea to alter table structure from code, particularly in a loop.

You are probably looking for the traditional master/detail table structure, google for some table join tutorials at sites like


here is the algorithm of my SQL:

k = 1
C1 = generate counts from R1
repeat
k = k + 1
INSERT INTO R!k
SELECT p.Id, p.Item1, …, p.Itemk-1, q.Item
FROM Rk-1 AS p, TransactionTable as q
WHERE q.Id = p.Id AND
q.Item > p.Itemk-1
.
.
until Rk = {}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > Creating VB code to increment the number of fields in an MSSQL create table statement

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap