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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:02:35+00:00 2026-05-28T16:02:35+00:00

I am using the exec(query_string) command on sql server. The matter is that the

  • 0

I am using the exec(“query_string”) command on sql server. The matter is that the changes executed by this command cannot seen outside.

I mean that, I try to create a temp table with dynamic columns in a exec command an it runs ok, but when I try to make a select statement over created table I receive a error message that says that the table does not exists.

I know that is a context problem and my cuestion is how I make to exec command runs on a same context just like other sql statements???

CREATE TABLE #Test ( TheDate datetime, Harvest int )
INSERT INTO #Test(TheDate, Harvest) VALUES ('2011/10/11', 50)
INSERT INTO #Test(TheDate, Harvest) VALUES ('2012/10/11', 100)
INSERT INTO #Test(TheDate, Harvest) VALUES ('2011/10/01', 20)
INSERT INTO #Test(TheDate, Harvest) VALUES ('2011/12/11', 50)
INSERT INTO #Test(TheDate, Harvest) VALUES ('2011/11/11', 50)
INSERT INTO #Test(TheDate, Harvest) VALUES ('2011/11/12', 150)

DECLARE @listCol2 varchar(max)
SET @listCol2 = 
    (SELECT DISTINCT ('[' + CONVERT(varchar,YEAR(TheDate)) + '-' + RIGHT('0'+CONVERT(varchar,MONTH(TheDate)),2) +'] date,')
     FROM #Test
     ORDER BY ('[' + CONVERT(varchar,YEAR(TheDate)) + '-' + RIGHT('0'+CONVERT(varchar,MONTH(TheDate)),2) +'] date,') ASC
     FOR XML PATH(''))
SET  @listCol2 = LEFT(@listCol2,LEN(@listCol2)-1)
SELECT  @listCol2

EXEC ('CREATE TABLE #Test2 ('+@listCol2+')') 

SELECT * FROM #Test2 --In this line I got this message: the name of the object #Test2 isn't valid, why???

DROP TABLE #Test2

DROP TABLE #Test

Thanks

  • 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-28T16:02:37+00:00Added an answer on May 28, 2026 at 4:02 pm

    you can have following options.

    EXEC ('CREATE TABLE #Test2 ('+@listCol2+'); Select * from #test2') 
    
    
    
    EXEC ('CREATE TABLE ##Test2 ('+@listCol2+')') 
    
    SELECT * FROM ##Test2 --it will not throws the name of the object isn't valid
    
    DROP TABLE ##Test2
    
    
    EXEC ('CREATE TABLE Test2 ('+@listCol2+')') 
    
    SELECT * FROM Test2 --it will not throws the name of the object isn't valid
    
    DROP TABLE Test2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create a pipe in a ksh script (using exec) that
How can I implement chmod command on file by using exec? I would appreciate
We're using the Maven exec:java goal to run a custom java application that configures
I am using this php code: exec(unrar e file.rar,$ret,$code); and getting an error code
I am working on sql server monitoring product and i have database query that
I'm trying to access a large Oracle database through SQL Server using OPENROWSET in
This is the command I've been using for finding matches (queryString) in php files,
I create a database using ADO.NET SQL queries like this: create database mydatabase create
I have a SQL Server 2005 database that stores data for multiple users. Each
Based on getting Query Execution Statistics using this extremely useful piece of SQL obtained

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.