It is still a good practice to drop the temp tables when you are finished with them especially ifyou are running an sp that has more steps. THe sooner you free the resources the better.
update @tblStaff
set stdBudRate = isnull((select top 1 isnull(sr.rate1,0)
from tblStaffChargeRate sr, @tblStaff t
where sr.staffid = t.staffid
and sr.date <= @endPeriodDate
order by date desc),0)
Although it is a fact as per mr.denny "Regular tables and Temp Tables also have a max row length of 8060 bytes.", my SQL statement (inside a stored procedure) for creating the following table:
In regular tables and temp tables you can in fact create tables with a potential record size greater than the maximum. However, if you try to enter a record longer than that, it will fail. In some ways the table variable failing on creation is a better idea.(And of course there are times when it isn't, depends onthe situation.) Table variables are indeed limited compared to temp tables and clearly from the discussion not all the limitations are nicely spelled out.
I recommend always setting a Primary Key in table variables, as in:
Declare @tblStaff Table (
staffID int NOT NULL,
periodBudget money default 0,
stdBudRate money default 0,
nextPeriodBudget money default 0,
daysWorked int
PRIMARY KEY CLUSTERED (staffID))
If the linking field won't be unique, it's simple to force uniqueness.
Declare @tblStaff Table (
rowID int IDENTITY(1,1) NOT NULL,
staffID int NOT NULL,
periodBudget money default 0,
stdBudRate money default 0,
nextPeriodBudget money default 0,
daysWorked int
PRIMARY KEY CLUSTERED (staffID, rowID))
In multi-step procedures that (for example) join derived tables, you can get dramatic increases in speed (I've seen 5 minutes shrink to less than a second) by throwing the derived tables into PKeyed table variables before joining them. Derived tables don't "inherit" any indexing information from their source, so PKeying makes up for that.
This practice also tends to simplify and self-document the code which you (or your successor) will appreciate if the code has to be adjusted (or scavenged from) later.
[red] Cursors, triggers, user-defined functions and dynamic SQL are an axis of evil![/red]
[green]Life's uncertain...eat dessert first...www.deerfieldbakery.com[/green]
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.