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

  • Home
  • SEARCH
  • 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 8762935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:39:33+00:00 2026-06-13T15:39:33+00:00

Possible Duplicate: TSQL Writing into a Temporary Table from Dynamic SQL ALTER PROCEDURE [dbo].[usp_ServicesStats1](@PERIOD

  • 0

Possible Duplicate:
TSQL Writing into a Temporary Table from Dynamic SQL

 ALTER PROCEDURE [dbo].[usp_ServicesStats1](@PERIOD VARCHAR(30) )

 AS
 BEGIN
-- SET NOCOUNT ON added to prevent extra result SETs FROM
-- interfering with SELECT statements.
SET NOCOUNT ON;


 DELETE FROM servicesstats1;
 DECLARE @QUERY  NVARCHAR(MAX);

 DECLARE @mainTable VARCHAR(50)

 SET @mainTable = '[ServicesStats' + @PERIOD + ']';



 SET @QUERY = 'INSERT INTO servicesstats1(Department,StudentUsers) 
 SELECT department,COUNT(*) FROM ' +  @mainTable + 
 'GROUP BY department';

 EXEC(@QUERY); -- runs okay!


 IF OBJECT_ID('tempdb..[##tmp5]') is not null 
BEGIN 
    drop table [##tmp5] 
END

 SET @QUERY = ' SELECT studentid
 INTO [##tmp5]
 FROM ' + @mainTable + '
 GROUP BY studentid
 having COUNT(*)=1; select * from [##tmp5];';
 PRINT @QUERY;


 EXEC sp_executesql @QUERY;

 IF OBJECT_ID('tempdb..[#tmp3]') is not null 
BEGIN 
    drop table [#tmp3] 
END

 SELECT S.department,COUNT(*) AS No INTO #TMP3 --  ONLY THIS SERVICE 
 FROM servicesstats_0511_0412 s, #TMP5 T    -- this is not being replaced yet by the @mainTable
 WHERE S.STUDENTID = T.STUDENTID
 GROUP BY S.department

Error :

Invalid object name ‘#TMP5’.

The problem is temporary table #tmp5, now if I were to run this it would work but it’s not dynamic.

SELECT studentid
INTO [#tmp5]
FROM tableName
GROUP BY studentid
having COUNT(*)=1;

Basically I’m doing this because of the dynamic table name. But #tmp is throwing that error.

  • 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-06-13T15:39:34+00:00Added an answer on June 13, 2026 at 3:39 pm

    You are using a local temporary table, which only exists within the scope of your dynamic sql, so you could reference it as follows without error:

    SET @QUERY = ' SELECT studentid
    INTO [#tmp5]
    FROM ' + @mainTable + '
    GROUP BY studentid
    having COUNT(*)=1; 
    SELECT * 
    FROM [#tmp]';
    
    EXEC sp_executesql @QUERY;
    

    However, when you try and access it outside of the scope of your dynamic sql you can no more access it that you could from another window in SSMS. You’ll need to use a global temporary table, then you can access it:

    SET @QUERY = ' SELECT studentid
    INTO [##tmp5]
    FROM ' + @mainTable + '
    GROUP BY studentid
    having COUNT(*)=1;';
    
    EXEC sp_executesql @QUERY;
    
    SELECT  *
    FROM    [##Tmp5];
    

    Just be aware of possible issues in a multi user environment.

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

Sidebar

Related Questions

Possible Duplicate: How to SELECT * INTO [temp table] FROM [Stored Procedure] I am
Possible Duplicate: T-SQL Group Rows Into Columns I have a table _data that is
Possible Duplicate: T-SQL Pivot? Possibility of creating table columns from row values I've been
Possible Duplicate: TSQL: Are indexes on temporary tables deleted when the table is deleted?
Possible Duplicate: Concatenate row values T-SQL I have a table like this: ref_num name
Possible Duplicate: Create a date with T-SQL I've a data table that stores each
Possible Duplicate: C# Update Table using SqlCommand.Parameters I'm trying to update an SQL Server
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: SQL Server: Can I Comma Delimit Multiple Rows Into One Column? I
Possible Duplicate: When should I use semicolons in SQL Server? When we are writing

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.