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.
| Author |
Topic |
|
RaghaSM
Yak Posting Veteran
52 Posts |
Posted - 2009-03-18 : 11:23:49
|
| Hi all,I am trying to create a Temp table inisde a stored proc taht uses a dynamic sql statement. After executing the Stored proc.. everything seems to be working fine , but when I add a select statment to the stored proc to retive the data from the temp table.. It says invalid object temptableHere is what I did :The select statment i am providing here is just a sample.. actually my sql statmnet checks for a lot of if conditions for all the parameters passed and appends to the sql statmentStored Proc written :ALTER procedure [dbo].[test]asbegindeclare @sql as varchar(1000)set @sql = 'select * into #TempTable from Employees 'print @sqlexec @sqlselect * from #TempTableDrop table #TempTableEndResult:After executing this stored proc , This is what I am getting in the messages:select * into #TempTable from EmployeesMsg 2812, Level 16, State 62, Line 13Could not find stored procedure 'select * into #TempTable from Employees '.Msg 208, Level 16, State 0, Procedure test, Line 15Invalid object name '#TempTable'.(1 row(s) affected)Can some body please help me where I am going wrong??/Your help will be very much appreciated |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-03-18 : 11:37:20
|
| Can't be done in this way, what you can do is create the temp table first (and give it a structure) and then exec your sql into it:create table #tmptble (a int, b varchar(10))insert into #tmptable exec @sql |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-03-18 : 11:43:10
|
Absolutely!A temp table created inside a block of dynamic SQL is local to that score and will no longer exist once the dynamic execution is finished.As Rick says -- you can create a temp table before executing the dynamic sql -- it will be available to dynamic sql.You don't actually need to do theINSERT <balh>EXEC <blah> syntax unless you want to -- I think that is actualy a row based operation so is likely to be slow. The table will be available for you to reference as you would normally from insode the dynamic block -- you can do something likeIF OBJECT_ID('tempdb..#foo') IS NOT NULL DROP TABLE #fooCREATE TABLE #foo ( [val] VARCHAR(50) )DECLARE @sql NVARCHAR(MAX)SET @sql = N'INSERT INTO #foo ( [val] ) SELECT ''aaa''UNION SELECT ''bbb''UNION SELECT ''ccc'''EXEC (@sql)SELECT * FROM #fooAnd it will work fine.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
RaghaSM
Yak Posting Veteran
52 Posts |
Posted - 2009-03-18 : 11:47:47
|
| Thanks for ur quick replies. Let me try that way and update you guys.Once again thanks for ur suggestions |
 |
|
|
RaghaSM
Yak Posting Veteran
52 Posts |
Posted - 2009-03-18 : 13:05:40
|
| I tried modifying he proc as follows:ALTER procedure [dbo].[test]asbegincreate table #temp( Employeeid int)declare @sql as varchar(5000)set @sql = 'Insert into #temp( Employeeid) (select Employeeid from Employee )'print @sql--insert into #temp exec @sqlexec @sqlselect * from #TempDrop table #TempEndbut still not working. Getting this result:Insert into #temp( Employeeid) (select Employeeid from Employee )Msg 2812, Level 16, State 62, Line 17Could not find stored procedure 'Insert into #temp( Employeeid) (select Employeeid from Employee)'.(0 row(s) affected)(1 row(s) affected)Please help. Very Urgent.. |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-03-18 : 13:40:21
|
try this,ALTER procedure [dbo].[test]asbegincreate table #temp( Employeeid int)declare @sql as varchar(5000)set @sql = 'Insert into #temp(Employeeid) select Employeeid from Employee '--print @sql--insert into #temp exec @sqlexec (@sql)select * from #TempDrop table #TempEnd |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2009-03-19 : 06:06:58
|
| Yeah -- you need the brackets round the @sql in EXECCharlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
|
|
|
|
|