Solved

TSQL while loops and test data fom lookup tables

Posted on 2010-09-13
4
569 Views
Last Modified: 2012-05-10
I would like to create some test data for a budget table.

I would like to look in the sales rep table and then create a bunch of data for that, then it should go to the next record in the rep table and create the same data but with the Rep filed populated with this second person etc. until the whole rep table is done.

Once I have understood that,I would like to amend it so that It looks in other tables to get other information as well as rep info.

fied names are
budget_dimension_group i.e. Cost_centre, rep
budget_dimension_item - this would be the rep name, or the cost centre which is stored in the small cost centre or rep table.

The output would look like this

 example of completed table
I am not really whizzo programmer, I think there are nested while loop involved but what I can't seem to find is how to get values from other tables as the input parameters into the while loop.

apologies if this doesn't sound very clear, feel free to ask more questions.

Please help, I would be forever grateful.

0
Comment
Question by:STOCRIC
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 33663954
STOCRIC,

Greetings!

You may not need a nested while loop here is you are using the same data for each of the reps.  For example, say you are going to from budget table:

INPUT, 1, 2010, 700.00
...
OUTPUT, 6, 2010, 700.00

You could simply CROSS JOIN this to the data containing the individual reps and you will end up with all the rows of budget table repeated for each rep in the rep table(s).
0
 
LVL 4

Expert Comment

by:timexist
ID: 33668948
I think you can create a budget table with basic records.


1, budget look up table part:

When create the budget table, you can do it in an Excel file then load them into database.
It is an once-off job. I think Excel is the best way to go.


2, in report part:

In report, you return data from stored procedure through temp tables.

of course you can record your report data into a report table, if a report will take very long time to run.

What you should do is put a time-stamp on the report records.

In the stored procedure, you can based on the "budget_dimension_group","budget_dimension_item" ... as parameters to return the budget values, and join them with the sales records.



0
 

Author Comment

by:STOCRIC
ID: 33669809
Many thanks for the info, I'll take a look and let you know.
0
 

Author Closing Comment

by:STOCRIC
ID: 33669854
so obvious, but a really elegant solution, thanks very much
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now