Solved

TSQL while loops and test data fom lookup tables

Posted on 2010-09-13
4
567 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

20 Experts available now in Live!

Get 1:1 Help Now