<?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>Mon, 13 Feb 2012 15:11:24 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<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>
	<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-59704</link>
		<dc:creator><![CDATA[Pinal Dave]]></dc:creator>
		<pubDate>Wed, 13 Jan 2010 18:06:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59704</guid>
		<description><![CDATA[Hi Feroz,

Append all insert statment to variable @sql and then execute. As below
 
@sql = @sql + &#039;INSERT INTO #SalesTbl VALUES(,,,) &#039;
@sql = @sql + &#039;INSERT INTO #SalesTbl VALUES(,,,) &#039;
exec (@sql)

Regards,
Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>Hi Feroz,</p>
<p>Append all insert statment to variable @sql and then execute. As below</p>
<p>@sql = @sql + &#8216;INSERT INTO #SalesTbl VALUES(,,,) &#8216;<br />
@sql = @sql + &#8216;INSERT INTO #SalesTbl VALUES(,,,) &#8216;<br />
exec (@sql)</p>
<p>Regards,<br />
Pinal Dave</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-59686</link>
		<dc:creator><![CDATA[Feroz]]></dc:creator>
		<pubDate>Wed, 13 Jan 2010 08:09:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59686</guid>
		<description><![CDATA[hi Pinal Dave 

is there any other alternative to my problem as i&#039;m s/w developer and the global temporary table will be accessed by other user also.

i&#039;m having definition in a variable like

DeclARE @sql varchar(100)
set @sql=‘CREATE TABLE #SalesTbl
(SalesID int, SalesName varchar(10)) ;)
exec(@sql)

now i want to insert records?
this temporary table is expired
if i will use global temporary then it will be shared by other users

so Please suggest me]]></description>
		<content:encoded><![CDATA[<p>hi Pinal Dave </p>
<p>is there any other alternative to my problem as i&#8217;m s/w developer and the global temporary table will be accessed by other user also.</p>
<p>i&#8217;m having definition in a variable like</p>
<p>DeclARE @sql varchar(100)<br />
set @sql=‘CREATE TABLE #SalesTbl<br />
(SalesID int, SalesName varchar(10)) ;)<br />
exec(@sql)</p>
<p>now i want to insert records?<br />
this temporary table is expired<br />
if i will use global temporary then it will be shared by other users</p>
<p>so Please suggest me</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-59683</link>
		<dc:creator><![CDATA[Feroz]]></dc:creator>
		<pubDate>Wed, 13 Jan 2010 04:54:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59683</guid>
		<description><![CDATA[thanks Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>thanks Pinal Dave</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-59659</link>
		<dc:creator><![CDATA[Pinal Dave]]></dc:creator>
		<pubDate>Tue, 12 Jan 2010 18:08:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59659</guid>
		<description><![CDATA[Hello Feroz,

You can create temp table using EXEC statement but that would be available only in EXEC statement. For example:


EXECUTE ( &#039;CREATE TABLE #SalesTbl 
(SalesID int, SalesName varchar(10)) ;
INSERT INTO #SalesTbl VALUES(1, &#039;&#039;ss&#039;&#039;)
SELECT * FROM #SalesTbl&#039;)     -- success

SELECT * FROM #SalesTbl     -- Error

Because EXEC starts a new session and temp table created inside that session is not accessible outside the EXEC () script. If you create a global table than that would be accessible as below:

EXECUTE ( &#039;CREATE TABLE ##SalesTbl 
(SalesID int, SalesName varchar(10)) ;
INSERT INTO ##SalesTbl VALUES(1, &#039;&#039;ss&#039;&#039;)
SELECT * FROM ##SalesTbl&#039; )            -- success
SELECT * FROM ##SalesTbl               -- success

Regards,
Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>Hello Feroz,</p>
<p>You can create temp table using EXEC statement but that would be available only in EXEC statement. For example:</p>
<p>EXECUTE ( &#8216;CREATE TABLE #SalesTbl<br />
(SalesID int, SalesName varchar(10)) ;<br />
INSERT INTO #SalesTbl VALUES(1, &#8221;ss&#8221;)<br />
SELECT * FROM #SalesTbl&#8217;)     &#8212; success</p>
<p>SELECT * FROM #SalesTbl     &#8212; Error</p>
<p>Because EXEC starts a new session and temp table created inside that session is not accessible outside the EXEC () script. If you create a global table than that would be accessible as below:</p>
<p>EXECUTE ( &#8216;CREATE TABLE ##SalesTbl<br />
(SalesID int, SalesName varchar(10)) ;<br />
INSERT INTO ##SalesTbl VALUES(1, &#8221;ss&#8221;)<br />
SELECT * FROM ##SalesTbl&#8217; )            &#8212; success<br />
SELECT * FROM ##SalesTbl               &#8212; success</p>
<p>Regards,<br />
Pinal Dave</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-59628</link>
		<dc:creator><![CDATA[Feroz]]></dc:creator>
		<pubDate>Tue, 12 Jan 2010 10:36:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59628</guid>
		<description><![CDATA[hi Pinal Dave

i do have a temporary table definition in a variable.

how can i created this table ?
can i use exec @var?(var contains definition of temp table )

I need to do so because no of columns depends on user input

Please suggest me]]></description>
		<content:encoded><![CDATA[<p>hi Pinal Dave</p>
<p>i do have a temporary table definition in a variable.</p>
<p>how can i created this table ?<br />
can i use exec @var?(var contains definition of temp table )</p>
<p>I need to do so because no of columns depends on user input</p>
<p>Please suggest me</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-59099</link>
		<dc:creator><![CDATA[Pinal Dave]]></dc:creator>
		<pubDate>Tue, 29 Dec 2009 13:06:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59099</guid>
		<description><![CDATA[Hi,

All declared variables in SQL server are local variables and are available only in current session. There is nothing to specify the scope of variable. Table vriable is a variable of data type &quot;Table&quot;. Here Table does not specify the scope of variable but specify the data type of variable.

Regards,
Pinal Dave]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>All declared variables in SQL server are local variables and are available only in current session. There is nothing to specify the scope of variable. Table vriable is a variable of data type &#8220;Table&#8221;. Here Table does not specify the scope of variable but specify the data type of variable.</p>
<p>Regards,<br />
Pinal Dave</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: IYASIR</title>
		<link>http://blog.sqlauthority.com/2009/12/28/sql-server-difference-temp-table-and-table-variable-effect-of-transaction/#comment-59049</link>
		<dc:creator><![CDATA[IYASIR]]></dc:creator>
		<pubDate>Mon, 28 Dec 2009 09:49:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7643#comment-59049</guid>
		<description><![CDATA[And what is the difference between local variable and table variable please?]]></description>
		<content:encoded><![CDATA[<p>And what is the difference between local variable and table variable please?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

