May 20, 2017 at 11:13 am
Hi everyone,
I still consider myself a newbie, so please bear with me.
While going through some existing stored procedures, I've noticed that some people create temporary tables (say #MyTable), while others seem to prefer declaring them as variables (for example, DECLARE @TestTable AS TABLE), and then proceed to list the fields and the related data types.
That said, are there any differences / advantages / disadvantages between these two approaches? I've only noticed that WITH(NOLOCK) is not allowed in the latter, while it is in the former. Not sure if that is significant, but that's as far as I've gotten so far.
Any tips or ideas will be more than welcome.
Thanks in advance.
May 20, 2017 at 12:48 pm
Well, nolock's a bad idea, so you shouldn't be judging based on that.
http://www.sqlservercentral.com/articles/Temporary+Tables/66720/
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
May 20, 2017 at 1:02 pm
@GilaMonster,
Thank you so much! That was exactly what I was looking for.
May 20, 2017 at 1:21 pm
I've seen Wayne's presentation on table variables versus temp tables. It's a good one. Now I'll have to read the article. 😉 Thanks, Gail.
May 20, 2017 at 1:39 pm
1) Use Temporary Tables in EVERY situation that you can. I my 45000 or so hours of SQL Server experience and training, the situations where you cannot are pretty rare edge cases.
2) Never use Scalar or Multi-statement User Defined Functions (one place you must use Table Variables).
3) Reread items 1 and 2 😎
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
May 20, 2017 at 2:04 pm
Hot off the presses, and quite appropriate for this post:
Paul White on Temp Object Caching
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
May 20, 2017 at 5:56 pm
Thank you everyone for taking the time to share your experience and the useful links!
May 20, 2017 at 10:31 pm
In my opinion, this question already has one of the best internet SQL Server answers I've ever seen.
I'd highly recommend reading Martin Smith's answer to this at https://dba.stackexchange.com/questions/16385/whats-the-difference-between-a-temp-table-and-table-variable-in-sql-server
That should give you ample comparison/contrast to chew on.
Cheers!
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply