July 3, 2016 at 1:15 am
hi,
i have one dynamic sql and in need one temp table like
declare @db varchar(200)
declare @tablename varchar(200)
declare @sql varchar(max)
@sql='create table @temp1(a int null)
insert into @temp1(a)
select abcid from '+@db+@tablename +'
.........
.........
'
exec(@sql)
Q1) i can declare temp table out side also , so please tel me in this situation where i need the table in side the dyanmic @sql , will it be any benefite in declareing outside or inside?
yours sincerely
July 3, 2016 at 8:12 am
If the structure of the temp table is fixed I always declare it outside the dynamic sql simply because then if I need to use it outside I know it will be available.
Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
July 3, 2016 at 10:46 pm
rajemessage 14195 (7/3/2016)
hi,i have one dynamic sql and in need one temp table like
declare @db varchar(200)
declare @tablename varchar(200)
declare @sql varchar(max)
[font="Arial Black"]@sql='create table @temp1(a int null)[/font]
insert into @temp1(a)
select abcid from '+@db+@tablename +'
.........
.........
'
exec(@sql)
Q1) i can declare temp table out side also , so please tel me in this situation where i need the table in side the dyanmic @sql , will it be any benefite in declareing outside or inside?
yours sincerely
Either way, that's not how you create a Temp Table. Also, if it IS supposed to be a Temp Table, you don't need to name it dynamically for any reason what so ever because even if a 1,000 different session all tried to create the same named Temp Table, they would ALL be uniquely named because of the hidden extension added to every one.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply