Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Writing to local variable from CTE


Writing to local variable from CTE

Author
Message
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
Which comes back to what are you trying to accomplish?

What problem are you trying to solve?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
tshad
tshad
SSC-Enthusiastic
SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)

Group: General Forum Members
Points: 190 Visits: 454
But you can access a local variable.


DECLARE @Start int

SET @Start = 3

;WITH NumberSequence ( MonthNumber )
AS (
-- Anchor Member
SELECT @Start AS MonthNumber

UNION ALL

-- Recursive Member
SELECT MonthNumber + 1
FROM NumberSequence
WHERE MonthNumber < 12
)
-- Outer Query
SELECT *
FROM NumberSequence



I just didn't know you couldn't assign to a variable from a CTE.

I understand that a CTE only exists for the duration of the query and that would make sense if the variable were inside the CTE (I know it isn't possible) but whether the CTE is temporary doesn't really explain why you can't write to a variable that is outside the CTE, but you can access that same variable in the CTE.

Thanks,

Tom
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
This doesn't work either:



declare @TestVar int;

select
Mycol1,
@TestVar = MyCol2
from
dbo.MyTable




If you took my comment as to the visibility of the CTE, I am sorry, that was not the intent.

I am still waiting for you to explain what you are trying to accomplish. What is the problem you are trying to solve.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
tshad
tshad
SSC-Enthusiastic
SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)SSC-Enthusiastic (190 reputation)

Group: General Forum Members
Points: 190 Visits: 454
I am not sure what you meant there about visibility.


A CTE only exists for the duration of the query with which is defined. You can't assign a value in a CTE to a variable for the same reason you can't do this:


I assumed you could write to an outside variable if you can access it (i.e. select as I did).

Also, you said this doesn't work either. Are you talking about the query I sent last. It does work. If you change the @Start variable, the results are different. But it does access the outside variable.

On my intent, I think I was pretty clear there. I am just trying to find out if there is a way to write to an outside variable from within a CTE, which may not be possible.

I was trying to make the first query more efficient by using the outside variable and only building the month string once instead of the 12 times it was being built in the first query.

Thanks,

Tom
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24207 Visits: 37978
tshad (5/12/2013)
I am not sure what you meant there about visibility.


A CTE only exists for the duration of the query with which is defined. You can't assign a value in a CTE to a variable for the same reason you can't do this:


I assumed you could write to an outside variable if you can access it (i.e. select as I did).

Also, you said this doesn't work either. Are you talking about the query I sent last. It does work. If you change the @Start variable, the results are different. But it does access the outside variable.

On my intent, I think I was pretty clear there. I am just trying to find out if there is a way to write to an outside variable from within a CTE, which may not be possible.

I was trying to make the first query more efficient by using the outside variable and only building the month string once instead of the 12 times it was being built in the first query.

Thanks,

Tom


Then build it once like I showed you how with the code I had posted and then use the variable in a subsequent query. Remember, set-based doesn't always mean in one query. Sometimes you need to divide and conquer to solve a problem in the most efficient and scalable manner.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search