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 7861237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:37:40+00:00 2026-06-02T22:37:40+00:00

I am creating stored procedure in which I need to build a temporary table

  • 0

I am creating stored procedure in which I need to build a temporary table dynamically. I tried the following code but its not creating table. When I execute the generated Query in Query window it works fine there.

--declare query variable
DECLARE @Query nvarchar(MAX)
SET @Query = 'CREATE TABLE #final (DATE int,'   


--DECLARE @COLUMNNAME VARIABLE
DECLARE @ColName nvarchar(10)

OPEN @taCur

FETCH NEXT FROM @taCur INTO @ColName
WHILE (@@FETCH_STATUS = 0)
BEGIN
    SET @Query = @Query + 'T_' + @ColName +' int,'   
    FETCH NEXT FROM @taCur INTO @ColName
END

SET @Query = @Query + 'TOTAL int,CUMM_TOTAL int)'
print @Query
EXEC sp_executesql @Query
--SET @Query = 'INSERT INTO #final (DATE) VALUES (1)'
SET @Query = 'SELECT * FROM #final'
print @Query
EXEC(@Query)

Final generated create table Query is as follow

CREATE TABLE #final (DATE int,T_211E int,T_211G int,T_211H int,T_211J int,T_211L int,T_221F int,TOTAL int,CUMM_TOTAL int)
  • 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-02T22:37:42+00:00Added an answer on June 2, 2026 at 10:37 pm

    Object #final used in CREATE and SELECT statements is not in the same scope.

    Here is one way to structure the query.

    • Try to terminate SQL statements with semicolon. Even though it is not mandatory, it will help you differentiate the statements for readability. Note that I have included semicolon at the end of CREATE, INSERT and SELECT statements.

    • You can notice that CREATE, INSERT and SELECT are executed in the same transaction. Thereby, you don’t lose the scope of the temporary table.

    Script:

        CREATE TABLE dbo.ColumnSchema
        (
            ColName NVARCHAR(10)
        );
    
        INSERT INTO dbo.ColumnSchema (ColName) VALUES
            ('211E'),
            ('211G'),
            ('211H'),
            ('211J'),
            ('211L'),
            ('211F');
    
    
    DECLARE @Query      NVARCHAR(MAX);
    DECLARE @ColName    NVARCHAR(10);
    
    SET @Query = 'CREATE TABLE #final (DATE int,';
    
    DECLARE taCursor CURSOR FOR 
        SELECT  ColName
        FROM    dbo.ColumnSchema;
    
    OPEN taCursor
    
    FETCH NEXT FROM taCursor 
        INTO @ColName
    
        WHILE (@@FETCH_STATUS = 0)
        BEGIN
    
            SET @Query = @Query + 'T_' + @ColName + ' int, '   
    
            FETCH NEXT FROM taCursor 
                INTO @ColName
        END
    
    CLOSE       taCursor;
    DEALLOCATE  taCursor;
    
    SET @Query = @Query + 'TOTAL int,CUMM_TOTAL int); '
    SET @Query = @Query + 'INSERT INTO #final (DATE) VALUES (1); '
    SET @Query = @Query + 'SELECT * FROM #final; '
    
    EXEC (@Query);
    

    Output:

    DATE T_211E T_211G T_211H T_211J T_211L T_211F TOTAL CUMM_TOTAL
    ---- ------ ------ ------ ------ ------ ------ ----- ----------
    1    NULL   NULL   NULL   NULL   NULL   NULL   NULL  NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a stored procedure which has to create a table which name
I am creating a stored procedure to which I want to pass as variable
How can I achieve the following in oracle without creating a stored procedure? Data
I am creating a Stored procedure in which , I am passing 2 IN
I am creating an application in which i am using stored procedure where i
I want to call the Stored Procedure which returns a table in the Table
I am creating a stored procedure in MySQL 5.1.40 which needs to run a
I am creating a report with the data which calls the stored procedure and
I am creating a stored procedure in my sql work bench. I am modifying
I'm creating a stored procedure to return search results where some of the parameters

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.