SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Running query after query and input variables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

marykings25
Starting Member

4 Posts

Posted - 07/08/2013 :  18:19:45  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

USA
1770 Posts

Posted - 07/08/2013 :  19:07:13  Show Profile  Reply with Quote
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 - 07/09/2013 :  06:20:46  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 07/09/2013 :  07:05:28  Show Profile  Reply with Quote
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 - 07/10/2013 :  08:42:07  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 07/10/2013 :  08:48:36  Show Profile  Reply with Quote
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 - 07/11/2013 :  06:25:14  Show Profile  Reply with Quote
Okay thank you so mcuh guys!

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 07/11/2013 :  07:18:40  Show Profile  Reply with Quote
you're welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000