Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

Writing to local variable from CTE Expand / Collapse
Author
Message
Posted Sunday, May 12, 2013 4:51 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:19 PM
Points: 23,243, Visits: 31,938
Which comes back to what are you trying to accomplish?

What problem are you trying to solve?



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)
Post #1451951
Posted Sunday, May 12, 2013 7:55 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 9:52 AM
Points: 147, Visits: 359
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
Post #1451960
Posted Sunday, May 12, 2013 8:09 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:19 PM
Points: 23,243, Visits: 31,938
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.



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)
Post #1451961
Posted Sunday, May 12, 2013 10:04 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 9:52 AM
Points: 147, Visits: 359
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
Post #1451966
Posted Monday, May 13, 2013 12:06 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 6:19 PM
Points: 23,243, Visits: 31,938
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.



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)
Post #1451979
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse