|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 1:10 PM
Points: 2,673,
Visits: 2,418
|
|
| Got it right for the wrong reason, learned something. Thanks.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 11:21 AM
Points: 1,500,
Visits: 1,512
|
|
Oleg Netchaev (12/9/2010)
... In other words, if create temp table part were moved out then the temp table would be visible to the exec batch and after, i.e. if object_id('tempdb.dbo.#tmp') is not null drop table #tmp;
create table #tmp (id int);
declare @sql varchar(2000); set @sql = 'insert into #tmp(id) values (1);'; exec (@sql);
select * from #tmp;results in id ----------- 1 Oleg Another way to get it to work is to put it all on the inside of the dynamic SQL like this:
Declare @strSql varchar(2000); Set @strSql = ''; Set @strSql = @strSql + 'Create table #tmp (id int);'; Set @strSql = @strSql + 'Insert into #tmp(id) values (1);'; Set @strSql = @strSql + 'Select * From #tmp;'; Exec (@strSql);
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 12:02 PM
Points: 5,243,
Visits: 7,055
|
|
cengland0 (12/10/2010) I thought I found the trick and it was that a space was missing. The lack of the space actually had nothing to do with the error but I thought it did so I selected the error message I thought would have been generated and got it right but for the wrong reason. I'll admit that I expected the missing space to be the issue too, when I saw the question in the daily mail. But when I went to the sit, saw the answer options, and missed any option that I could relate to this missing space issue, I realized that apparently either the missing space is not a problem, or the author accidentally forgot the space and didn't realize. So I answered the question, then copied and executed the code to check that indeed, no space is required here.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, January 31, 2013 8:01 AM
Points: 1,232,
Visits: 1,046
|
|
Hugo Kornelis (12/10/2010)
da-zero (12/10/2010) (but it surprises me that apparently 78% -for the moment- know the exact error message!)Well, I don't know about others, but for me this is one of the more common errors I see when developing. Or when copying and pasting code snippets from internet support forums.
Hugo makes a good point. I am sure more than 78% of us have submitted or executed code from this very web site that has returned this error.
<shhhhhh> But I still always check my work by running the code.
|
|
|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, April 17, 2013 10:57 PM
Points: 1,491,
Visits: 3,008
|
|
Since the error is due to the #tmp table no longer being in scope once the EXEC() is done, one could fix this to work by making the table global and adding an explicit DROP after its last use. This leaves the table available to the SELECT.
Declare @strSql varchar(2000) Set @strSql = '' Set @strSql = @strSql + 'Create table ##tmp (id int)' -- Now "##tmp', not "#tmp' Set @strSql = @strSql + 'Insert into ##tmp(id) values (1)' Exec (@strSql) Select * from ##tmp drop table ##tmp Of course, in practice, one must be careful in naming global temp tables to avoid possible collisions with unrelated processes.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Monday, May 13, 2013 11:21 AM
Points: 2,163,
Visits: 2,148
|
|
| Thanks for the question, I have run into this before so I knew it right away.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 5:11 PM
Points: 7,104,
Visits: 7,168
|
|
Hugo Kornelis (12/10/2010) (plus, that one is extremely unlikely, as the database for temporary object is called tempdb, not TempDB - but I guess only people who are in the habit of developing on a case sensitive instance would know that). No, the case sensitivity makes no difference to the message displayed so even people like me who never use a case-sensitive instance are used to seeing "tempdb" in messages, and never "TempDB".
Tom Que conclure à la fin de tous mes longs propos? C'est que les préjugés sont la raison des sots. (Voltaire, 1756)
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Sunday, May 19, 2013 10:45 AM
Points: 1,786,
Visits: 1,006
|
|
Good question. Sometimes it is easy to miss small things and this type of questions will help us to keep up with the tips and tricks.
|
|
|
|
|
Old Hand
      
Group: General Forum Members
Last Login: 2 days ago @ 5:50 AM
Points: 302,
Visits: 339
|
|
good basic question.
Declare @strSql varchar(2000) Set @strSql = '' Set @strSql = @strSql + 'Create table #tmp (id int)' Set @strSql = @strSql + 'Insert into #tmp(id) values (1) Select * from #tmp' Exec (@strSql)
--- Raaj
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Monday, May 20, 2013 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|