Hello!
I have some code that I am trying to execute in one piece in Query Analyzer, but somehow it won't. The strange thing is that it works in 2 separate parts. The following code, Step 01 and Step 02, seems to run just fine as one piece of code:
------------- STEP 01 --------------
BEGIN
select *
into #Last4MktDays
from MstrWatchList
where transdate in
(select distinct top 4 transdate
from MstrWatchList
order by transdate)
order by ticker, transdate
END
------------- STEP 02 --------------
BEGIN
alter table #Last4MktDays ADD RowCntr INT IDENTITY NOT NULL
END
Also, when I run the following code separately, after the above code has already completed, it gives me a valid 'select', showing me the 10 rows I expect:
------------- STEP 03 --------------
BEGIN
select * from #Last4MktDays
where RowCntr <= 10
order by RowCntr
END
However, when I try to put Step 03 right after Steps 01 and 02 and run it as one piece of program code it fails in the Step 03 portion, telling me the following:
Server: Msg 207, Level 16, State 3, Line 28
Invalid column name 'RowCntr'.
Server: Msg 207, Level 16, State 1, Line 28
Invalid column name 'RowCntr'.
Can someone explain to me why this happens? I would appreciate it very much.
Many thanks!
I have some code that I am trying to execute in one piece in Query Analyzer, but somehow it won't. The strange thing is that it works in 2 separate parts. The following code, Step 01 and Step 02, seems to run just fine as one piece of code:
------------- STEP 01 --------------
BEGIN
select *
into #Last4MktDays
from MstrWatchList
where transdate in
(select distinct top 4 transdate
from MstrWatchList
order by transdate)
order by ticker, transdate
END
------------- STEP 02 --------------
BEGIN
alter table #Last4MktDays ADD RowCntr INT IDENTITY NOT NULL
END
Also, when I run the following code separately, after the above code has already completed, it gives me a valid 'select', showing me the 10 rows I expect:
------------- STEP 03 --------------
BEGIN
select * from #Last4MktDays
where RowCntr <= 10
order by RowCntr
END
However, when I try to put Step 03 right after Steps 01 and 02 and run it as one piece of program code it fails in the Step 03 portion, telling me the following:
Server: Msg 207, Level 16, State 3, Line 28
Invalid column name 'RowCntr'.
Server: Msg 207, Level 16, State 1, Line 28
Invalid column name 'RowCntr'.
Can someone explain to me why this happens? I would appreciate it very much.
Many thanks!