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
 Running query after query and input variables

Author  Topic 

marykings25
Starting Member

4 Posts

Posted - 2013-07-08 : 18:19:45
Hi guys i am a complete newby on sql server but i am not able to process a very large database in excel so i have no choice.

I have 2 requests for desperate Hélio..

1) is there any way to run a query over a query without having to create a table with the results of the first query? (would drop table work? If so, how?

2) how can i define input variables the same way i do in excel? I am trying to run a couple of simulations based on 2 core inputs (in excel i would just do a data table)

Tks!
Kindest regards
Mariana

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2013-07-08 : 19:07:13
1) If I understand your need, you could use the WITH Common Table Expression facility to create a virtual table out of your initial query that the second query could then make use of.

2) Don't know Excel variables so I'd just be guessing here.

=================================================
The cure for anything is salt water -- sweat, tears, or the sea. -Isak Dinesen
Go to Top of Page

marykings25
Starting Member

4 Posts

Posted - 2013-07-09 : 06:20:46
Hi Bustaz,
1) how should i do it then? Sorry for the basic question, but i am really a beginner on SQL, so there's a lot of expresions / language that i don't even know how to use

2) The idea here would be to set a variable at the beginning of my code and then use that variable for multiple calculation steps (for instance, i'ld have a column of 1's and 0's according to the value of that variable and so on)
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-09 : 07:05:28
quote:
Originally posted by marykings25

Hi Bustaz,
1) how should i do it then? Sorry for the basic question, but i am really a beginner on SQL, so there's a lot of expresions / language that i don't even know how to use

2) The idea here would be to set a variable at the beginning of my code and then use that variable for multiple calculation steps (for instance, i'ld have a column of 1's and 0's according to the value of that variable and so on)



1, you've two options

one using CTE as Bustaz suggested
it will be like


;With CTEName (Col1,Col2,...ColN)
AS
(
YourQuery
)

SELECT
FROM CTEName
INNER JOIN ...
ON ...
WHERE ...


Another way is to use a derived table on the fly like below


SELECT columns...
FROM
(
YourQuery Here
)t
INNER JOIN ...
ON ...
WHERE ...


2, you can do like this if i understand you correctly


DECLARE @YourVariable datatype (int,varchar,...)

SELECT @Yourvariable=..
FROM... query

SELECT @yourvariable = ...some other calculation
...


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

marykings25
Starting Member

4 Posts

Posted - 2013-07-10 : 08:42:07
Tks!
1) Could i use Create View as well?
Yup, for 2) it was exactly that
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-10 : 08:48:36
quote:
Originally posted by marykings25

Tks!
1) Could i use Create View as well?
Yup, for 2) it was exactly that


View is again a persisting entity unlike CTE or derived tables.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

marykings25
Starting Member

4 Posts

Posted - 2013-07-11 : 06:25:14
Okay thank you so mcuh guys!

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-07-11 : 07:18:40
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -