I am using Microsoft SQL Server 2008 I would like to save the result of the subquery in order to reuse it in the next subquery. Is it possible? What is the best way to do this? (I am very new to SQL)
My query looks like this:
INSERT INTO [dbo].[TestTable] ( [a] ,[b] ) SELECT ( SELECT TOP 1 MAT_WS_ID FROM
My question is:
This is right what I did. I replaced the second SELECT statement in the WHERE-Clause for [b] (which is commented out and exactly the same as for [a]), with the result of the first SELECT statement from [a] (= MAT_WS_ID). This seems to give the correct results. But I do not understand why!
I mean, MAT_WS_ID is part of both X_ALIAS and Y_ALIAS temporary tables. Therefore, in the SELECT statement for [b], in the query area of ββthe [b] -select query, MAT_WS_ID can only be known from the Y_ALIAS table. (Or am I mistaken, I'm more C ++, maybe the sphere objects in SQL and C ++ are completely different)
I just don't know how best to use SQL Server to reuse the result of selecting a scalar. Or do I just not need to copy the selection for each column, and the sql server optimizes it myself?
source share