Groups | Blog | Home
all groups > sql server programming > september 2004 >

sql server programming : Using temporary table in in dynamic SQL


Prasad Koukuntla
9/9/2004 4:50:04 PM
Temporary table is different from temporary variable.
As Vishal said, we can not use temporary tables in dynamic SQL if the table
variable is defined outside of the dynamic SQL scope.


[quoted text, click to view]

JT Lovell
9/9/2004 5:39:02 PM
Try this:

DECLARE @sql nvarchar(800)
DECLARE @mytable nvarchar(50)
SET @mytable = 'dbo.author'
SET @sql = 'SELECT * FROM ' + @mytable
EXEC sp_executesql @sql


--
JT Lovell


[quoted text, click to view]

Jonathan Blitz
9/9/2004 11:58:08 PM
I have declared a temporary table (@mytable) in a sp and want to use it
within a dynamic SQL statement.
It doesn't seem to like it.
Complains that the vaiable @mytable is not defined.

What is wrong?

--
Jonathan Blitz
AnyKey Limited
Israel

Tel: (972) 8 9790365

"When things seem bad
Don't worry and shout
Just count up the times
Things have worked themselves out."

Vishal Parkar
9/10/2004 2:53:20 AM
you can not reference a table variable inside dynamic sql , if the variable
is defined ouside the scope of dynamic sql statement.

ex:
declare @var table (idd int)

exec ('select * from @var') --error out, because @var is defined outside the
scope.

exec ('declare @var table (idd int) insert into @var values (1) select *
from @var') --successful

--
Vishal Parkar
vgparkar@yahoo.co.in | vgparkar@hotmail.com

AddThis Social Bookmark Button