Link to home
Start Free TrialLog in
Avatar of tegronakron
tegronakron

asked on

Fetch from Temporary Table Incorrect syntax near for

I am trying to use a FETCH Statement to fetch from a temporary table and i keep getting a syntax error "incorrect syntax near for"  Can you use a FETCH statemnet on a temporary table?  Code below.

declare @field1 varchar(50)
declare @field2 varchar(50)
DECLARE @tblOrders Table  (
Table_ID INT IDENTITY,
LotNumber      varchar      (50)
)

DECLARE @mycursor CURSOR
for select * from @tblOrders

OPEN @mycursor

FETCH NEXT FROM @mycursor
INTO  @field1, @field2
WHILE @@FETCH_STATUS = 0

-- other processing here

CLOSE @mycursor
DEALLOCATE @mycursor
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sankarbha
sankarbha

replace @mycursor with mycursor
In other words:

declare @field1 varchar(50)
declare @field2 varchar(50)
DECLARE @tblOrders Table  (
Table_ID INT IDENTITY,
LotNumber     varchar     (50)
)

DECLARE mycursor CURSOR
for select * from @tblOrders

OPEN mycursor

FETCH NEXT FROM mycursor
INTO  @field1, @field2
WHILE @@FETCH_STATUS = 0

-- other processing here

CLOSE mycursor
DEALLOCATE mycursor
Also, you are not using a temporary table, but rather a variable of type table.