Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Can I store query results as variables?

Author  Topic 

psynister
Starting Member

2 Posts

Posted - 2008-10-01 : 18:14:35
System Info: SQL Server 2005, Visual Studio 2005

I have been programming for 7 years, but only recently have I had any need to delve deeply into SQL, so anything beyond Selects and Joins are pretty well beyond me.

I am using SSIS to try to accomplish this, but I have very little idea of where to start in order to make it happen and am even newer to SSIS than I am to SQL. I tried techniques I have used in other projects that have worked in the past, but they lead me to nothing but error messages.

Problem:
I have 16 bits of information that I need to collect. Eight of them come from queries, and I need to be able to stick the results into variables (I think I need to, I could be wrong). Those eight are all a single number, which is a sum of all sales during a period of time, for different departments.

The other eight come from calculations that are based on the first eight. I take the results from queries 1 & 5, for example, and add them together to get the number I need for the ninth variable, while 2 & 6 go into the tenth variable, and so on.

After I have all 16 variables, I need to store them all into a single record on a separate table.


Any help is appreciated,


~Jason Griffith

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-10-01 : 18:19:37
I don't think you need SSIS for this. T-SQL has variables.

Here's a cheap example of the use of variables:

DECLARE @s1 varchar(5), @s2 varchar(20)

SELECT @s1 = 'Tara', @s2 = 'Kizer'

SELECT @s1 + ' ' + @s2

DECLARE @i int

SELECT @i = Column1 FROM Table1 WHERE Column2 = 99

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 03:22:20
You can store them all as a resultset if they all are from single query. Then you can later use recultset and then get values from it rather than storing as individual values. how are you getting values? using Execute SQL task?
Go to Top of Page

psynister
Starting Member

2 Posts

Posted - 2008-10-07 : 09:22:51
Apparently I just had some crappy syntax in there, because I figured out how to get it to work after throwing in a few parenthesis. In the end, I went for 26 bits of information instead of 16, but it works like a charm now.


If anyone would like to see the code for it, send me an email or reply here and I will do my best to get it to you.

~Jason Griffith
Go to Top of Page
   

- Advertisement -