Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 temp table

Author  Topic 

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-11-30 : 10:31:48
Hi,
Is it possible to create a temp table in query analyser, populate it with data and then build a dynamic sql which is executed in sp_executesql @sql where @sql refers to the temp table ?

i.e.
1- create table #tblMain...
2- insert into #tblMain...
3- set @sql = 'select field1 from tbl1 inner join #tblMain ...'

Is the above possible please?
Thanks

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-11-30 : 10:35:39
yes. this is possible


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-11-30 : 10:51:05
How is it done please?
Thanks
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-11-30 : 10:59:14
question is why are you doing all this?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2011-11-30 : 11:05:02
[CODE]create table #tblMain (
myColumn int
)

insert into #tblMain values (7)

select * from #tblMain

declare @sql nvarchar(100) = N'select ''Hello'', myColumn from #tblMain'

exec sp_ExecuteSQL @sql
[/CODE]

=======================================
Faced with the choice between changing one's mind and proving that there is no need to do so, almost everyone gets busy on the proof. -John Kenneth Galbraith
Go to Top of Page

arkiboys
Master Smack Fu Yak Hacker

1433 Posts

Posted - 2011-11-30 : 11:20:47
Thanks
Go to Top of Page
   

- Advertisement -