<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; Difference Temp Table and Table Variable &#8211; Effect of Transaction</title>
	<atom:link href="http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Thu, 23 May 2013 14:22:59 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: shoaibsuleman</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-410326</link>
		<dc:creator><![CDATA[shoaibsuleman]]></dc:creator>
		<pubDate>Sun, 20 Jan 2013 16:50:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-410326</guid>
		<description><![CDATA[Please ignore the first two lines as it was mistakenly copied (temp table declaration on top) example starts from create...
Thanks]]></description>
		<content:encoded><![CDATA[<p>Please ignore the first two lines as it was mistakenly copied (temp table declaration on top) example starts from create&#8230;<br />
Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shoaibsuleman</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-410325</link>
		<dc:creator><![CDATA[shoaibsuleman]]></dc:creator>
		<pubDate>Sun, 20 Jan 2013 16:47:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-410325</guid>
		<description><![CDATA[Hi

I am following your articles from quite a while and honestly learn alot from examples you always gave in your articles. I have a situation in which I am passing table name as a parameter and want to insert the information generated from select statement into the table but I am keep getting an errors, stating must declare as a table variable. If I declare as a table variable it stores the information into table variable and not insert the data into the table I want to take a look on below example

declare @tableName Varchar(100)
set @@tableName =&#039;smtpF2.dbo.infoChange&#039;;
Create table #temp
(
change_version_state varchar(max),
change_version_status varchar(200),
uniqueid  varchar(20),
Country_code  varchar(15),
Research_date  varchar(15),
data_date varchar(14),
Make  varchar(50),
Model  varchar(50),
Versions varchar(255),
Model_year varchar(15),
doors varchar(10),
body varchar(10));
 insert into #temp select * from infoChange
select * from #temp
declare @tableNames Varchar(100)
set @tableNames =&#039;smtpF2.dbo.infoChange&#039;;
insert into  @tableNames select * from #temp

Your urgent reply / advise will be very appreciated
Thanks]]></description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I am following your articles from quite a while and honestly learn alot from examples you always gave in your articles. I have a situation in which I am passing table name as a parameter and want to insert the information generated from select statement into the table but I am keep getting an errors, stating must declare as a table variable. If I declare as a table variable it stores the information into table variable and not insert the data into the table I want to take a look on below example</p>
<p>declare @tableName Varchar(100)<br />
set @@tableName =&#8217;smtpF2.dbo.infoChange&#8217;;<br />
Create table #temp<br />
(<br />
change_version_state varchar(max),<br />
change_version_status varchar(200),<br />
uniqueid  varchar(20),<br />
Country_code  varchar(15),<br />
Research_date  varchar(15),<br />
data_date varchar(14),<br />
Make  varchar(50),<br />
Model  varchar(50),<br />
Versions varchar(255),<br />
Model_year varchar(15),<br />
doors varchar(10),<br />
body varchar(10));<br />
 insert into #temp select * from infoChange<br />
select * from #temp<br />
declare @tableNames Varchar(100)<br />
set @tableNames =&#8217;smtpF2.dbo.infoChange&#8217;;<br />
insert into  @tableNames select * from #temp</p>
<p>Your urgent reply / advise will be very appreciated<br />
Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #009 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-400678</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Weekly Series &#8211; Memory Lane &#8211; #009 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Sat, 29 Dec 2012 01:31:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-400678</guid>
		<description><![CDATA[[...] Difference Temp Table and Table Variable – Effect of Transaction [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Difference Temp Table and Table Variable – Effect of Transaction [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anusha</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-279185</link>
		<dc:creator><![CDATA[Anusha]]></dc:creator>
		<pubDate>Tue, 24 Apr 2012 10:30:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-279185</guid>
		<description><![CDATA[Thanks]]></description>
		<content:encoded><![CDATA[<p>Thanks</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Using MAXDOP 1 for Single Processor Query &#8211; SQL in Sixty Seconds #008 &#8211; Video &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-266334</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Using MAXDOP 1 for Single Processor Query &#8211; SQL in Sixty Seconds #008 &#8211; Video &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Fri, 23 Mar 2012 01:30:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-266334</guid>
		<description><![CDATA[[...] on Errors: Difference Temp Table and Table Variable – Effect of Transaction Effect of TRANSACTION on Local Variable – After ROLLBACK and After COMMIT Debate – Table [...]]]></description>
		<content:encoded><![CDATA[<p>[...] on Errors: Difference Temp Table and Table Variable – Effect of Transaction Effect of TRANSACTION on Local Variable – After ROLLBACK and After COMMIT Debate – Table [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-265574</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Wed, 21 Mar 2012 08:02:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-265574</guid>
		<description><![CDATA[Local variable can be of any type and can have only one value. Table varialbe is of type table and it just act like a table where you can have multiple columns with multiple rows]]></description>
		<content:encoded><![CDATA[<p>Local variable can be of any type and can have only one value. Table varialbe is of type table and it just act like a table where you can have multiple columns with multiple rows</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Table Variables and Transactions &#8211; SQL in Sixty Seconds #007 &#8211; Video &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-265492</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Table Variables and Transactions &#8211; SQL in Sixty Seconds #007 &#8211; Video &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Wed, 21 Mar 2012 01:31:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-265492</guid>
		<description><![CDATA[[...] on Errors: Difference Temp Table and Table Variable – Effect of Transaction Effect of TRANSACTION on Local Variable – After ROLLBACK and After COMMIT Debate – Table [...]]]></description>
		<content:encoded><![CDATA[<p>[...] on Errors: Difference Temp Table and Table Variable – Effect of Transaction Effect of TRANSACTION on Local Variable – After ROLLBACK and After COMMIT Debate – Table [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQL SERVER &#8211; Debate &#8211; Table Variables vs Temporary Tables &#8211; Quiz &#8211; Puzzle &#8211; 13 of 31 &#171; SQL Server Journey with SQL Authority</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-236859</link>
		<dc:creator><![CDATA[SQL SERVER &#8211; Debate &#8211; Table Variables vs Temporary Tables &#8211; Quiz &#8211; Puzzle &#8211; 13 of 31 &#171; SQL Server Journey with SQL Authority]]></dc:creator>
		<pubDate>Sat, 14 Jan 2012 01:31:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-236859</guid>
		<description><![CDATA[[...] Server Interview Questions and Answers ISBN: 1466405643 Page#75-76  Difference Temp Table and Table Variable – Effect of Transaction Difference TempTable and Table Variable – TempTable in Memory a Myth  ObjectID in Negative – [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Server Interview Questions and Answers ISBN: 1466405643 Page#75-76  Difference Temp Table and Table Variable – Effect of Transaction Difference TempTable and Table Variable – TempTable in Memory a Myth  ObjectID in Negative – [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yo Hal</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-195503</link>
		<dc:creator><![CDATA[Yo Hal]]></dc:creator>
		<pubDate>Mon, 14 Nov 2011 20:23:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-195503</guid>
		<description><![CDATA[http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/1016bf4b-3d42-4bcd-b9c9-d1680d1c076c/ (the last answer) claims that table variables are not being written to the log, and this is the reason why they are not participating in transactions, however as I have showed this appears to be wrong.]]></description>
		<content:encoded><![CDATA[<p><a href="http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/1016bf4b-3d42-4bcd-b9c9-d1680d1c076c/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/1016bf4b-3d42-4bcd-b9c9-d1680d1c076c/</a> (the last answer) claims that table variables are not being written to the log, and this is the reason why they are not participating in transactions, however as I have showed this appears to be wrong.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yo Hal</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-193259</link>
		<dc:creator><![CDATA[Yo Hal]]></dc:creator>
		<pubDate>Fri, 11 Nov 2011 16:18:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-193259</guid>
		<description><![CDATA[I have conducted an experiment to see it uses tempdb&#039;s log.
On a SQL Server instance that I am the sole user of it, I have first restarted SQL Server, and I have checked the tempdb log which was at that time 512 kb.
Then I have executed the following query against one of the databases on the instance:

DECLARE @i int
DECLARE @a table (i int)
SET @i = 0
WHILE @i &lt; 100000
BEGIN
	INSERT INTO @a values(@i)
  SET  @i = @i + 1
END

After which I have checked again the log file size and it was now 1536 kb. 
So this shows us, that although table variables are not being rolled back, and although the log of tempdb is not used to redo anything anyway (since at startup tempdb is recreated), still any write to table variables is being logged to tempdb&#039;s log.
And this is something that deems to be unnecessary, especially due to the fact that SQL Server has to for writes to the log, and if SQL Server would not write table variables tot he log then there would probably be a performance gain.
As such I would say that for an OLAP server, I would rather have an extra spindle for the tempdb log rather then for the database log, since for reads the tempdb log perfomance is more of a concern.]]></description>
		<content:encoded><![CDATA[<p>I have conducted an experiment to see it uses tempdb&#8217;s log.<br />
On a SQL Server instance that I am the sole user of it, I have first restarted SQL Server, and I have checked the tempdb log which was at that time 512 kb.<br />
Then I have executed the following query against one of the databases on the instance:</p>
<p>DECLARE @i int<br />
DECLARE @a table (i int)<br />
SET @i = 0<br />
WHILE @i &lt; 100000<br />
BEGIN<br />
	INSERT INTO @a values(@i)<br />
  SET  @i = @i + 1<br />
END</p>
<p>After which I have checked again the log file size and it was now 1536 kb.<br />
So this shows us, that although table variables are not being rolled back, and although the log of tempdb is not used to redo anything anyway (since at startup tempdb is recreated), still any write to table variables is being logged to tempdb&#039;s log.<br />
And this is something that deems to be unnecessary, especially due to the fact that SQL Server has to for writes to the log, and if SQL Server would not write table variables tot he log then there would probably be a performance gain.<br />
As such I would say that for an OLAP server, I would rather have an extra spindle for the tempdb log rather then for the database log, since for reads the tempdb log perfomance is more of a concern.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yo Hal</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-190422</link>
		<dc:creator><![CDATA[Yo Hal]]></dc:creator>
		<pubDate>Mon, 07 Nov 2011 23:22:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-190422</guid>
		<description><![CDATA[Hi pinal,
Since table variables are unaffected by transaction, does this means that they bypass completely the tempdb log?
The reason why I am asking is, since I make very heavy use of memory tables, so my question is if putting the tempdb log file on its own array will improve performance?]]></description>
		<content:encoded><![CDATA[<p>Hi pinal,<br />
Since table variables are unaffected by transaction, does this means that they bypass completely the tempdb log?<br />
The reason why I am asking is, since I make very heavy use of memory tables, so my question is if putting the tempdb log file on its own array will improve performance?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rahul Singi</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-149819</link>
		<dc:creator><![CDATA[Rahul Singi]]></dc:creator>
		<pubDate>Thu, 21 Jul 2011 13:09:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-149819</guid>
		<description><![CDATA[Hi pinal, 

My question is that - Is Table variable hits the performance? if yes then how and how we can manage it..]]></description>
		<content:encoded><![CDATA[<p>Hi pinal, </p>
<p>My question is that &#8211; Is Table variable hits the performance? if yes then how and how we can manage it..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mrityunjay Ravi</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-64861</link>
		<dc:creator><![CDATA[Mrityunjay Ravi]]></dc:creator>
		<pubDate>Fri, 09 Apr 2010 03:45:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-64861</guid>
		<description><![CDATA[Hi Pinal,

I want to create table permanent by the using procedure and pass it table name as variable as like below. 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[ProcInsTab]
@Emp_ID varchar(50),
@Password varchar(max),
@Emp_Name varchar(100),
@Address varchar(250),
@City varchar(50),
@Mobile_No varchar(10)
AS

CREATE TABLE @Emp_ID(
[Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Catalog Version] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Mailed] [float] NULL ,
[Sales] [money] NULL ,
[Production $] [int] NOT NULL ,
[Orders] [float] NULL ,
[Response] [float] NULL ,
[Response of Test Control] [int] NOT NULL ,
[Average Invoice] [float] NULL ,
[SMP] [float] NULL ,
[SMP of Test Control] [int] NOT NULL ,
[Catalog Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Brand] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[Drop Date] [smalldatetime] NULL
) ON [PRIMARY];
INSERT INTO [Employee_Master]
([Emp_ID]
,[Password]
,[Emp_Name]
,[Address]
,[City]
,[Mobile_No])
VALUES
(@Emp_ID, @Password,@Emp_Name, @Address,@City, @Mobile_No);
Please help me how to solved this problem.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>I want to create table permanent by the using procedure and pass it table name as variable as like below. </p>
<p>set ANSI_NULLS ON<br />
set QUOTED_IDENTIFIER ON<br />
GO</p>
<p>CREATE PROCEDURE [dbo].[ProcInsTab]<br />
@Emp_ID varchar(50),<br />
@Password varchar(max),<br />
@Emp_Name varchar(100),<br />
@Address varchar(250),<br />
@City varchar(50),<br />
@Mobile_No varchar(10)<br />
AS</p>
<p>CREATE TABLE @Emp_ID(<br />
[Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
[Catalog Version] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
[Mailed] [float] NULL ,<br />
[Sales] [money] NULL ,<br />
[Production $] [int] NOT NULL ,<br />
[Orders] [float] NULL ,<br />
[Response] [float] NULL ,<br />
[Response of Test Control] [int] NOT NULL ,<br />
[Average Invoice] [float] NULL ,<br />
[SMP] [float] NULL ,<br />
[SMP of Test Control] [int] NOT NULL ,<br />
[Catalog Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
[Brand] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
[Drop Date] [smalldatetime] NULL<br />
) ON [PRIMARY];<br />
INSERT INTO [Employee_Master]<br />
([Emp_ID]<br />
,[Password]<br />
,[Emp_Name]<br />
,[Address]<br />
,[City]<br />
,[Mobile_No])<br />
VALUES<br />
(@Emp_ID, @Password,@Emp_Name, @Address,@City, @Mobile_No);<br />
Please help me how to solved this problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-61543</link>
		<dc:creator><![CDATA[Pinal Dave]]></dc:creator>
		<pubDate>Sun, 21 Feb 2010 16:34:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-61543</guid>
		<description><![CDATA[Hi Sanjay,

Everytime we execute a batch in EXEC function a new session is created and tem tables and table variables created in earlier session becomes unaccessible. If you create a single batch then you can get the desired result as below:

DECLARE @SqlString as varchar(max)
SET @SqlString = &#039;DECLARE @TableVar TABLE(Col1 VARCHAR(100))&#039;
SET @SqlString = @SqlString + &#039;INSERT INTO @TableVar (Col1) VALUES(&#039;&#039;Table Var – Outside Tran&#039;&#039;)&#039;
EXEC (@SqlString)
SET @SqlString = @SqlString + &#039;SELECT Col1 AS TableVar_BeforeTransaction FROM @TableVar&#039;
EXEC (@SqlString)


Regards,
Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>Hi Sanjay,</p>
<p>Everytime we execute a batch in EXEC function a new session is created and tem tables and table variables created in earlier session becomes unaccessible. If you create a single batch then you can get the desired result as below:</p>
<p>DECLARE @SqlString as varchar(max)<br />
SET @SqlString = &#8216;DECLARE @TableVar TABLE(Col1 VARCHAR(100))&#8217;<br />
SET @SqlString = @SqlString + &#8216;INSERT INTO @TableVar (Col1) VALUES(&#8221;Table Var – Outside Tran&#8221;)&#8217;<br />
EXEC (@SqlString)<br />
SET @SqlString = @SqlString + &#8216;SELECT Col1 AS TableVar_BeforeTransaction FROM @TableVar&#8217;<br />
EXEC (@SqlString)</p>
<p>Regards,<br />
Pinal Dave</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sanjay Khed</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-61540</link>
		<dc:creator><![CDATA[Sanjay Khed]]></dc:creator>
		<pubDate>Sun, 21 Feb 2010 11:41:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-61540</guid>
		<description><![CDATA[Hi Pinal Dave,

I have modified the above query by adding one more variable to store the SQL Statement in SQLString,
When executed, I got the error in the @TableVar


-- Start --

DECLARE @TableVar TABLE(Col1 VARCHAR(100))
DECLARE @SqlString as varchar(max)

SET @SqlString = &#039;INSERT INTO @TableVar (Col1) VALUES(&#039;&#039;Table Var – Outside Tran&#039;&#039;)&#039;

EXEC (@SqlString)

SET @SqlString = &#039;SELECT Col1 AS TableVar_BeforeTransaction FROM @TableVar&#039;

EXEC (@SqlString)

-- End --

Msg 1087, Level 15, State 2, Line 1
Must declare the table variable &quot;@TableVar&quot;.
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable &quot;@TableVar&quot;.

--------------------------------------------------------

Please help me how to solved this problem.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal Dave,</p>
<p>I have modified the above query by adding one more variable to store the SQL Statement in SQLString,<br />
When executed, I got the error in the @TableVar</p>
<p>&#8211; Start &#8211;</p>
<p>DECLARE @TableVar TABLE(Col1 VARCHAR(100))<br />
DECLARE @SqlString as varchar(max)</p>
<p>SET @SqlString = &#8216;INSERT INTO @TableVar (Col1) VALUES(&#8221;Table Var – Outside Tran&#8221;)&#8217;</p>
<p>EXEC (@SqlString)</p>
<p>SET @SqlString = &#8216;SELECT Col1 AS TableVar_BeforeTransaction FROM @TableVar&#8217;</p>
<p>EXEC (@SqlString)</p>
<p>&#8211; End &#8211;</p>
<p>Msg 1087, Level 15, State 2, Line 1<br />
Must declare the table variable &#8220;@TableVar&#8221;.<br />
Msg 1087, Level 15, State 2, Line 1<br />
Must declare the table variable &#8220;@TableVar&#8221;.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Please help me how to solved this problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-61289</link>
		<dc:creator><![CDATA[Pinal Dave]]></dc:creator>
		<pubDate>Tue, 16 Feb 2010 18:19:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-61289</guid>
		<description><![CDATA[Hello Ravi,

Prepare the CREATE TABLE statement dynamically to create global or permanent table because temporary or table variable would not be accessible outside of execution statement.

Regards,
Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>Hello Ravi,</p>
<p>Prepare the CREATE TABLE statement dynamically to create global or permanent table because temporary or table variable would not be accessible outside of execution statement.</p>
<p>Regards,<br />
Pinal Dave</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mrityunjay Ravi</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-61218</link>
		<dc:creator><![CDATA[Mrityunjay Ravi]]></dc:creator>
		<pubDate>Tue, 16 Feb 2010 06:16:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-61218</guid>
		<description><![CDATA[Hi Pinal,

I want to create table by the using procedure and pass it table name as variable as like below. 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[ProcInsTab] 
	 @Emp_ID varchar(50),
	@Password varchar(max),
	@Emp_Name varchar(100),
	@Address varchar(250),
	@City varchar(50), 
	@Mobile_No varchar(10)
AS

	

CREATE TABLE @Emp_ID(
	[Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Catalog Version] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Mailed] [float] NULL ,
	[Sales] [money] NULL ,
	[Production $] [int] NOT NULL ,
	[Orders] [float] NULL ,
	[Response] [float] NULL ,
	[Response of Test Control] [int] NOT NULL ,
	[Average Invoice] [float] NULL ,
	[SMP] [float] NULL ,
	[SMP of Test Control] [int] NOT NULL ,
	[Catalog Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Brand] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
	[Drop Date] [smalldatetime] NULL 
	) ON [PRIMARY];
INSERT INTO [Employee_Master]
           ([Emp_ID]
           ,[Password]
           ,[Emp_Name]
           ,[Address]
           ,[City]
           ,[Mobile_No])
     VALUES
           (@Emp_ID, @Password,@Emp_Name, @Address,@City, @Mobile_No);

Please help me how to solved this problem.]]></description>
		<content:encoded><![CDATA[<p>Hi Pinal,</p>
<p>I want to create table by the using procedure and pass it table name as variable as like below. </p>
<p>set ANSI_NULLS ON<br />
set QUOTED_IDENTIFIER ON<br />
GO</p>
<p>CREATE PROCEDURE [dbo].[ProcInsTab]<br />
	 @Emp_ID varchar(50),<br />
	@Password varchar(max),<br />
	@Emp_Name varchar(100),<br />
	@Address varchar(250),<br />
	@City varchar(50),<br />
	@Mobile_No varchar(10)<br />
AS</p>
<p>CREATE TABLE @Emp_ID(<br />
	[Description] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
	[Catalog Version] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
	[Mailed] [float] NULL ,<br />
	[Sales] [money] NULL ,<br />
	[Production $] [int] NOT NULL ,<br />
	[Orders] [float] NULL ,<br />
	[Response] [float] NULL ,<br />
	[Response of Test Control] [int] NOT NULL ,<br />
	[Average Invoice] [float] NULL ,<br />
	[SMP] [float] NULL ,<br />
	[SMP of Test Control] [int] NOT NULL ,<br />
	[Catalog Title] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
	[Brand] [varchar] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,<br />
	[Drop Date] [smalldatetime] NULL<br />
	) ON [PRIMARY];<br />
INSERT INTO [Employee_Master]<br />
           ([Emp_ID]<br />
           ,[Password]<br />
           ,[Emp_Name]<br />
           ,[Address]<br />
           ,[City]<br />
           ,[Mobile_No])<br />
     VALUES<br />
           (@Emp_ID, @Password,@Emp_Name, @Address,@City, @Mobile_No);</p>
<p>Please help me how to solved this problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Raj</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-60967</link>
		<dc:creator><![CDATA[Raj]]></dc:creator>
		<pubDate>Wed, 10 Feb 2010 20:32:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-60967</guid>
		<description><![CDATA[I like the article. Most of the time I request developers to use table variables if the number of rows processing is small. However, I would like to know the benchmark of using table variable instead of temp table. Please share if any have have Idea about this.]]></description>
		<content:encoded><![CDATA[<p>I like the article. Most of the time I request developers to use table variables if the number of rows processing is small. However, I would like to know the benchmark of using table variable instead of temp table. Please share if any have have Idea about this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ponraj</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-60229</link>
		<dc:creator><![CDATA[Ponraj]]></dc:creator>
		<pubDate>Tue, 26 Jan 2010 14:41:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-60229</guid>
		<description><![CDATA[Thanks Tejas Shah... 
i got the answer]]></description>
		<content:encoded><![CDATA[<p>Thanks Tejas Shah&#8230;<br />
i got the answer</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tejas Shah</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-60179</link>
		<dc:creator><![CDATA[Tejas Shah]]></dc:creator>
		<pubDate>Mon, 25 Jan 2010 07:48:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-60179</guid>
		<description><![CDATA[Hi Ponraj,

You need to make sure that you are executing all these queries at once.

This is table variable, so we need to declare at every execution time. 

So here I think what did you do is:

First execute declare and insert statements and then you try to execute &quot;select&quot;. IN Table variable, you need to execute all statements at once then only you will get an actual result.

Scope of table variable is per execution.

Thanks,

Tejas]]></description>
		<content:encoded><![CDATA[<p>Hi Ponraj,</p>
<p>You need to make sure that you are executing all these queries at once.</p>
<p>This is table variable, so we need to declare at every execution time. </p>
<p>So here I think what did you do is:</p>
<p>First execute declare and insert statements and then you try to execute &#8220;select&#8221;. IN Table variable, you need to execute all statements at once then only you will get an actual result.</p>
<p>Scope of table variable is per execution.</p>
<p>Thanks,</p>
<p>Tejas</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ponraj</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-60161</link>
		<dc:creator><![CDATA[Ponraj]]></dc:creator>
		<pubDate>Sun, 24 Jan 2010 14:26:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-60161</guid>
		<description><![CDATA[hi Pinal Dave

as like you said that i did below that but i got the error in the @TableVar 

--Create Table Variable and insert single row

DECLARE @TableVar TABLE(Col1 VARCHAR(100))
INSERT INTO @TableVar (Col1)
VALUES(&#039;Table Var - Outside Tran&#039;);

SELECT Col1 AS TableVar_BeforeTransaction
FROM @TableVar

THis is the error

Msg 1087, Level 15, State 2, Line 2
Must declare the table variable &quot;@TableVar&quot;.


can you explain that why error will come
Thanks &amp; Regards,
D.Ponraj]]></description>
		<content:encoded><![CDATA[<p>hi Pinal Dave</p>
<p>as like you said that i did below that but i got the error in the @TableVar </p>
<p>&#8211;Create Table Variable and insert single row</p>
<p>DECLARE @TableVar TABLE(Col1 VARCHAR(100))<br />
INSERT INTO @TableVar (Col1)<br />
VALUES(&#8216;Table Var &#8211; Outside Tran&#8217;);</p>
<p>SELECT Col1 AS TableVar_BeforeTransaction<br />
FROM @TableVar</p>
<p>THis is the error</p>
<p>Msg 1087, Level 15, State 2, Line 2<br />
Must declare the table variable &#8220;@TableVar&#8221;.</p>
<p>can you explain that why error will come<br />
Thanks &amp; Regards,<br />
D.Ponraj</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Marko Parkkola</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-59758</link>
		<dc:creator><![CDATA[Marko Parkkola]]></dc:creator>
		<pubDate>Thu, 14 Jan 2010 20:14:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59758</guid>
		<description><![CDATA[I think Pinal already answered you. But here&#039;s how you can do it. Take notice that you must set the all the statements into @sql variable and then execute is as whole. You can&#039;t execute them separately because #SalesTbl is destroyed right after the exec command returns.


create procedure foo
as
begin
 declare @sql varchar(100)

 set @sql=&#039;CREATE TABLE #SalesTbl(file_no int,amount int,date datetime); &#039;
 set @sql+=&#039;SELECT * FROM #SalesTbl&#039;

 exec(@sql)
end

exec dbo.foo
drop procedure foo]]></description>
		<content:encoded><![CDATA[<p>I think Pinal already answered you. But here&#8217;s how you can do it. Take notice that you must set the all the statements into @sql variable and then execute is as whole. You can&#8217;t execute them separately because #SalesTbl is destroyed right after the exec command returns.</p>
<p>create procedure foo<br />
as<br />
begin<br />
 declare @sql varchar(100)</p>
<p> set @sql=&#8217;CREATE TABLE #SalesTbl(file_no int,amount int,date datetime); &#8216;<br />
 set @sql+=&#8217;SELECT * FROM #SalesTbl&#8217;</p>
<p> exec(@sql)<br />
end</p>
<p>exec dbo.foo<br />
drop procedure foo</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Tkatch</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-59735</link>
		<dc:creator><![CDATA[Brian Tkatch]]></dc:creator>
		<pubDate>Thu, 14 Jan 2010 14:21:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59735</guid>
		<description><![CDATA[Will this help?

SELECT &#039;day&#039; + CAST(DAY(Date) AS CHAR(2))]]></description>
		<content:encoded><![CDATA[<p>Will this help?</p>
<p>SELECT &#8216;day&#8217; + CAST(DAY(Date) AS CHAR(2))</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Feroz</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-59723</link>
		<dc:creator><![CDATA[Feroz]]></dc:creator>
		<pubDate>Thu, 14 Jan 2010 06:11:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59723</guid>
		<description><![CDATA[this table is created dynamically using 
DeclARE @sql varchar(100)
set @sql=‘CREATE TABLE #SalesTbl
(file_no int,amount int,date datetime) ;)
exec(@sql)

file_no
amount
date

is the above procedure possible]]></description>
		<content:encoded><![CDATA[<p>this table is created dynamically using<br />
DeclARE @sql varchar(100)<br />
set @sql=‘CREATE TABLE #SalesTbl<br />
(file_no int,amount int,date datetime) ;)<br />
exec(@sql)</p>
<p>file_no<br />
amount<br />
date</p>
<p>is the above procedure possible</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Feroz</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-59722</link>
		<dc:creator><![CDATA[Feroz]]></dc:creator>
		<pubDate>Thu, 14 Jan 2010 06:09:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59722</guid>
		<description><![CDATA[i have 2 temporary tables with these columns
file_no
amount
date
and another table
file_no
day1
day2
.
.
.
day31

what i have to do is select the day part of the date(suppose 12/18/2009 i have to select 18) and put the value of amount into another tables column of that day (suppose day18)
is there any procedure to overcome this problem]]></description>
		<content:encoded><![CDATA[<p>i have 2 temporary tables with these columns<br />
file_no<br />
amount<br />
date<br />
and another table<br />
file_no<br />
day1<br />
day2<br />
.<br />
.<br />
.<br />
day31</p>
<p>what i have to do is select the day part of the date(suppose 12/18/2009 i have to select 18) and put the value of amount into another tables column of that day (suppose day18)<br />
is there any procedure to overcome this problem</p>
]]></content:encoded>
	</item>
</channel>
</rss>
