July 27, 2015 at 5:15 pm
Hi, are you aware of any shortcut strategies for table variables similar to shortcut strategies for temp tables? For example, the following SQL will create a temp table named #MyTable with all of the columns from student and then select the top 10 rows of student data into #MyTable:
select top 10 * into #MyTable from Student
Is there any type of similar or equivalent shortcut for table variables like:
select top 10 * into @MyTable from Student
Afaik, if I want to select rows from Student into a @MyTable table variable then I need to create the @MyTable table variable with a column structure like a normal table which is more time-consuming than using the temp table approach.
July 27, 2015 at 5:40 pm
sqlguy-736318 (7/27/2015)
Hi, are you aware of any shortcut strategies for table variables similar to shortcut strategies for temp tables? For example, the following SQL will create a temp table named #MyTable with all of the columns from student and then select the top 10 rows of student data into #MyTable:select top 10 * into #MyTable from Student
Is there any type of similar or equivalent shortcut for table variables like:
select top 10 * into @MyTable from Student
Afaik, if I want to select rows from Student into a @MyTable table variable then I need to create the @MyTable table variable with a column structure like a normal table which is more time-consuming than using the temp table approach.
You cannot do a select * into @tablevar
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
July 28, 2015 at 3:03 am
sqlguy-736318 (7/27/2015)
Afaik, if I want to select rows from Student into a @MyTable table variable then I need to create the @MyTable table variable with a column structure like a normal table which is more time-consuming than using the temp table approach.
By 'more time-consuming', you mean it'll take longer to write? Yes, but not by all that much.
Why are you using table variables over temp tables?
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
July 28, 2015 at 4:23 am
It's only more time consuming when you're writing it. I'm with Gail though, be sure you're using table variables for the right reasons.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
July 28, 2015 at 10:03 am
I'm using the SS debugger and I want to set a watch on the table variable so I can set a breakpoint, inspect and view the values of the table variable. I don't think that this is possible with a temp table is it?
July 28, 2015 at 3:46 pm
I posted a separate thread and table variables don't provide additional debugging capabilities using the SSMS debugger unfortunately.
What are the main benefits of using table variables over temp tables? I'm assuming the main benefit is that a table variable ensures an isolated scope.
In the previous posts to this thread, some forum members seemed to have some bias towards temp tables over table variables. Why is that?
July 28, 2015 at 3:51 pm
Table variables also don't have statistics. Sometimes, this is a great thing. Sometimes it's a horrible thing. But it's one of the biggest differences between table variables and temporary tables.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
July 28, 2015 at 3:58 pm
There are quite a few differences. One of the best answers I've ever seen was a comprehensive look at that by Martin Smith over at Stack Overflow.
Check out the answer here: http://dba.stackexchange.com/questions/16385/whats-the-difference-between-a-temp-table-and-table-variable-in-sql-server
One of the big ones that people often run into is that if a query using a table variable is compiled before the table is populated, the optimizer will assume 1 row in the table variable, which can cause some issues.
Cheers!
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply