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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:58:40+00:00 2026-06-01T20:58:40+00:00

Within my sql procedure I create an temporary table and then pivot it. This

  • 0

Within my sql procedure I create an temporary table and then pivot it. This is dynamic so in the end I don’t know how many columns there are. Lets make this table:

equipmentID   |   Name1   |   Name2   | Name3
1             |   34.54   |   21.23   | 213.65
2             |   334     |   23.421  | 23.12
3             |   43.4    |   2.34    | 23.41

The query used to generate this is stored in a string and is executing by calling execute(@stringQuery).

My other table is fetched with a normal sql query and generates another set of information (including the equipmentID that is included in the pivot table.

What I want to do is join these two tables (based on the equipmentID so that the stored procedure returns a single table instead of two but I can’t figure out how to do this.

So far I have tried several things:

  • Keep the main (second) query and add the execute line to in at the end of the FROM section so after all the tables are joined I have:

    LEFT JOIN execute(@stringQuery) as strQ on strQ.equipmentID = mainQ.equipmentID

  • Made the second query a string so they are both fetched with the execute command OR created another stored procedure that contained the main sql so (again) they both use the execute command. This does fetch the information it just doesn’t combine it. In this case I tried to combine these two executes with:

    execute('select * from (' + @query1 + ') as tbl2 left join ' + @query2 + ' as tbl1 on tbl2.equipmentID= tbl1.equipmentID')

  • Convert the main query into a string and then combined them using a LEFT JOIN so it becomes one big query.

All of these gave me issues. I figured this was a common thing and wasn’t sure how to approach solving it other than with what I tried already.

EDIT
When I try to create a temporary table and the insert into (or select from) I get told the temporary table name is an invalid object.

  • 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-01T20:58:42+00:00Added an answer on June 1, 2026 at 8:58 pm

    I got this working. What I did was use a global temporary table. That way I can use it in multiple execute statement rather than if I used a normal temporary table. This way I was also able to join on the global temporary table. Just keep in mind to use a unique id per procedure so that you can tell what rows you are currently working with as this can be seen over several procedures. This way I am also able to delete the rows (based on the id) at the end.

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

Sidebar

Related Questions

i have this sp: using sql server 2008 create procedure SelectTopCounts @Id bigint =
Within SQL Server 2005, I need to create a table dynamically based upon the
My stored procedure is called as below from an SQL instegartion package within SQL
I have a result set in MS-SQL within a stored procedure, and lets say
How to get the dynamic select results of EXECUTE within PL/SQL from Oracle sqlplus
I've written a PL/SQL procedure that would benefit if indexes were first disabled, then
I'm trying to execute an inline SQL statement within a stored procedure. I'm working
T-SQL gurus, what is causing the following procedure to return this error Select statements
Using SQL Server 2008, I have two stored procedures - create procedure [dbo].[SH_Export_data] (@unit
I'm try to figure how to create sql server stored procedure that when a

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.