Solved

Creating a temp table with variables from a SP.

Posted on 2008-10-28
8
245 Views
Last Modified: 2010-04-21
I have a stored Procedure that gives me the activity for a customer for a given time frame. It returns sperate variables for each field. ie.

Month Amount
Month2 Amount
Month3 Amount
etc,,,,,,
It does this in a 12 month increment. I would like to graph this data. I need a way to turn this into only two fields. On that contains all of the months and one that contain all of the amounts. Is there a way to do this?
0
Comment
Question by:jhawk3
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 78

Expert Comment

by:arnold
ID: 22823677
Could you post an example of the returned data?
one way is to define the returned data with as
i.e.
select @variable_month as month, @variable_amount as amount.
now you have 12 rows with month and amount as the heading and the data as the value.

Using:
create table #temporary_table ...
 insert into #temporary_table (month,value) values  (@variable_month, @variable_amount)  

http://www.sqlteam.com/article/temporary-tables
0
 

Author Comment

by:jhawk3
ID: 22823814
The returned data looks as follows.

Month1 = Oct '08
Month2 =Nov '08
Month3 = Dec '08

Amount1 = 15.50
Amount 2 = 156.25
Amount3 = 456.00

If I used your suggestion, which sounds like what I am looking for, how would the data be returned?
0
 
LVL 78

Expert Comment

by:arnold
ID: 22824071
You have two options to cycle through a loop to spit the data out:
Or issue sequentially
select @Month1 as month, @Amount1 as Amount
..
select @Month12 as month, @Amount12 as Amount

Or create a temporary table, and insert the data into it either via a loop or a sequential insert of the data.

Unfortunately, I do not have an example handy nor do you wish to alter the existing sp to return the data as a table.

how many variables will you have if the period you select is more then twelve months if such an option is available?  Or is the SP is always a twelve month report from an end point?
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:jhawk3
ID: 22825098
The sp is always a 12 month report. It will never have more than 12 variables.
0
 
LVL 78

Expert Comment

by:arnold
ID: 22825230
The post at:
http://bytes.com/forum/thread81368.html

Deals to some extent with your question.  A link therein points to a method to use a temp table and populate the data.
http://www.sommarskog.se/arrays-in-sql-2005.html

Is an option available to change the results to output a comma separated list for months and amounts?
0
 

Author Comment

by:jhawk3
ID: 22825317
Yes it is possible to change the results if this will allow me to do what I want.
0
 
LVL 78

Accepted Solution

by:
arnold earned 500 total points
ID: 22825430
In this case, can the sp build a temporary table and return the contents rather then assembling the data in variables??
0
 

Author Closing Comment

by:jhawk3
ID: 31510807
I adjusted the SP so that the variables where inserted into a temp table, then I parsed the table for the info for the chart. Works great. Thanks for the help and the tip about temp tables.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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