Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7632225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:34:18+00:00 2026-05-31T06:34:18+00:00

Using SQL Server 2008, I have two stored procedures – create procedure [dbo].[SH_Export_data] (@unit

  • 0

Using SQL Server 2008, I have two stored procedures –

create procedure [dbo].[SH_Export_data] (@unit varchar(5)) as
  declare @sqlquery varchar(max) = 'select check_dt from chk_data where unit = ' 
    + @unit

  IF object_id('tempdb..#TempTbl') IS NOT NULL
    DROP TABLE #TempTbl
  create TABLE #TempTbl (
    col1 varchar(max), col2 varchar(max)
  )

  insert #TempTbl(col1)
  exec(@sqlquery)



create procedure [dbo].[SH_Export] as
  DECLARE @unit varchar(5), @sql varchar(max) = '', @file_name = 'c:\export.xls'

  DECLARE crsr CURSOR for
    select unit
    from communities

  OPEN crsr
  FETCH NEXT FROM crsr
  into @unit

  while @@FETCH_STATUS = 0 
  BEGIN
    set @sql = 'exec master..xp_cmdshell ''bcp "exec dbo.SH_Export_data ' +
      @unit + '" queryout "' + @file_name + '" -c -T "''
  END

Note that there is one stored procedure calling another. The stored procedure being called creates a temp table and uses it within itself.

when I run –

exec dbo.SH_Export

I get this error message –

Error = [Microsoft][SQL Server Native Client 10.0][SQL Server] 
Invalid object name '#TempTbl'.

BUT I can run, without error –

exec dbo.SH_Export_data 63058

what causes that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T06:34:19+00:00Added an answer on May 31, 2026 at 6:34 am

    You need a global temp table or a permanent table.

    create TABLE ##TempTbl (
        col1 varchar(max), col2 varchar(max)
      )
    

    or

    create TABLE dbo.TempTbl (
        col1 varchar(max), col2 varchar(max)
      )
    

    They both accomplish the same thing – a table that your external processes can access (a local #temp table is restricted to the scope of the caller). The difference is that the ##global temp table doesn’t need to be explicitly dropped, but the permanent one does.

    What this means, though, is that if two users call this stored procedure at the same time, one of them will generate an error (or completely undo what the other one started doing). If you can do all of the #temp table magic in dynamic SQL, you may consider adding the current session’s spid or some other uniqueifier to the table name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2008. I have an interesting scenario where a stored procedure
Using SQL Server 2008 (R2). I have two tables in different databases (with same
I am using SQL Server 2008 and Visual Studio 2008 I have two databases.
I am using SQL Server 2008 R2. I have two tables, ShoppingList and ShoppingListFood
Using Microsoft SQL server 2008 I have a query that is in need of
I'm using SQL Server 2008 and I have a table with three columns: Length
I am using SQL Server 2008 and I have a situation where I have
I'm using SQL Server 2008 and I have a table that has a 'Status'
I am using T/SQL in SQL Server 2008 I have a table MyTable with
I am using SQL Server 2008 Enterprise. I have created a very simple test

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.