Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Table variable basics 2

Status
Not open for further replies.

dukeslater

Technical User
Jan 16, 2001
87
US
My understanding is that variables of table datatype remain in scope until the completion of the stored procedure, and all the examples I've found are very similar to this piece of my procedure:


declare @v_restable table(tran_date smalldatetime, rescount int)

insert into @v_restable
select date, number from some table...

update table 2
set table2.field1 = @v_restable.rescount
from @v_restable INNER JOIN table2
on @v_restable.tran_date = table2.date

However, when it hits the line that begins "set table2.field..." I get the error message that says "Must declare the variable @v_restable."

This same syntax works fine with a temp table, but I'd like to know how to do this with a variable. What am I missing?

Thanks...
 
use an alias for your table e.g.
Code:
declare @v_restable table(tran_date smalldatetime, rescount int)

insert into @v_restable 
select date, number from some table...

update table 2
set table2.field1 = tmp.rescount
from @v_restable tmp INNER JOIN table2
on tmp.tran_date = table2.date

that should work

"I'm living so far beyond my income that we may almost be said to be living apart
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top