Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
To my mind the fact that you cannot insert the results of a stored proceudre into a table variable is a big problem and regular temp tables are the only way I know of to get around this. In most other uses, I would choose a table varaible over a temp table.
Questions about posting. See faq183-874
Click here to learn Ways to help with Tsunami Relief
Declare @TableVar table (Col1 varchar(10), Col2 int)
update @tblStaff
set stdBudRate = isnull((select top 1 isnull(sr.rate1,0)
from tblStaffChargeRate sr
where sr.staffid = @tblStaff.staffid
and sr.date <= @endPeriodDate
order by date desc),0)
QA said:Server: Msg 137, Level 15, State 1, Line 159
Must declare the variable '@tblStaff'.
Declare @tblStaff Table (
staffID int,
periodBudget money default 0,
stdBudRate money default 0,
nextPeriodBudget money default 0,
daysWorked int)
Insert Into @tblStaff (staffID)
Select Distinct staffid
From #WIP
update @tblStaff
set stdBudRate = isnull((select top 1 isnull(sr.rate1,0)
from tblStaffChargeRate sr
where sr.staffid = @tblStaff.staffid
and sr.date <= @endPeriodDate
order by date desc),0)
select top 1 isnull(sr.rate1,0)
from tblStaffChargeRate sr
where sr.staffid = @tblStaff.staffid
and sr.date <= @endPeriodDate
order by date desc
Update @tblStaff
Set stdBudRate=isnull(SR.Rate1,0)
From
(Select max([Date]) MaxDate, StaffID From tblStaffChargeRate
Where [Date]<=@EndPeriodDate Group By StaffID) DT1
Inner Join tblStaffChargeRate SR
on SR.StaffID=DT1.StaffID and SR.[Date]=DT1.MaxDate
Inner Join @tblStaff on SR.StaffID=@tblStaff.StaffID
BOL said:Temporary tables are automatically dropped when they go out of scope, unless explicitly dropped using DROP TABLE:
A local temporary table created in a stored procedure is dropped automatically when the stored procedure completes. The table can be referenced by any nested stored procedures executed by the stored procedure that created the table. The table cannot be referenced by the process which called the stored procedure that created the table.
All other local temporary tables are dropped automatically at the end of the current session.
Global temporary tables are automatically dropped when the session that created the table ends and all other tasks have stopped referencing them. The association between a task and a table is maintained only for the life of a single Transact-SQL statement. This means that a global temporary table is dropped at the completion of the last Transact-SQL statement that was actively referencing the table when the creating session ended.