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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:22:05+00:00 2026-06-05T03:22:05+00:00

Sorry for the title, perhaps it’s not very clear. I have some SQL queries

  • 0

Sorry for the title, perhaps it’s not very clear.

I have some SQL queries in a script that depend on each other.
The script uses a temporary table in which the data is inserted (the #temp_data table).

This is the expected output:

___________________________________
| speed1 |  speed2   |  distance   |  
|   1    |   NULL    |     10      |  
|   3    |   NULL    |     40      |
|   5    |   NULL    |     90      |
|  NULL  |    1      |     10      |
|  NULL  |    3      |     40      |
|  NULL  |    5      |     90      |

Here is the query structure (I didn’t include the actual query since it’s too big):

-- First group
queryForSpeed1
queryToUpdateDistanceBasedOnSpeed1

-- Second group 
queryForSpeed2
queryToUpdateDistanceBasedOnSpeed2

If I run the first group of queries (queryForSpeed1 and queryToUpdateDistanceBasedOnSpeed1) separately from the second group then I get the expected output: only the speed1 and distance columns contain data:

___________________________________
| speed1 |  speed2   |  distance   |  
|   1    |   NULL    |     10      |  
|   3    |   NULL    |     40      |
|   5    |   NULL    |     90      |
|  NULL  |   NULL    |    NULL     |
|  NULL  |   NULL    |    NULL     |
|  NULL  |   NULL    |    NULL     |

The same happens when I run the second group:

___________________________________
| speed1 |  speed2   |  distance   |  
|  NULL  |   NULL    |    NULL     |  
|  NULL  |   NULL    |    NULL     |
|  NULL  |   NULL    |    NULL     |
|  NULL  |    1      |     10      |
|  NULL  |    2      |     40      |
|  NULL  |    3      |     90      |

BUT, when I run both groups: all the distances are NULL:

___________________________________
| speed1 |  speed2   |  distance   |  
|   1    |   NULL    |    NULL     |  
|   3    |   NULL    |    NULL     |
|   5    |   NULL    |    NULL     |
|  NULL  |    1      |    NULL     |
|  NULL  |    2      |    NULL     |
|  NULL  |    3      |    NULL     |

I believe this is somehow related to transaction management and temporary tables, although I wasn’t able to find anything relevant to solve the problem on Google.

From what I’ve read, SQL Server keeps a transaction log where it stores every update, insert and whatever… when it arrives at the end of the script it actually does all those insertions and updates.

So the update I did for the distance column finds all the speeds as being NULL because the data wasn’t yet inserted in the temporary table from the previous updates, but at the end of the query the speeds are inserted in the table so that’s why they are visible.

I played a bit with the GO statement to execute my script in batches, but no luck so far…


What am I doing wrong? Can someone point me in the right direction, please?


EDIT

Here is the actual query.

  • 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-05T03:22:08+00:00Added an answer on June 5, 2026 at 3:22 am

    The problem is not related to transactions, but rather to the way you conduct updates to #temp_speed_profile. The second pass through #temp_speed_profile retrieves all six records. Speed_new is null in first record of Voyage_Id, consequently @distance becomes null. As you retain the value of @distance in next turn, it remains null.

    Problem goes away when using different temporary tables because second pass works on second set of data only.

    A note on cursors – when defining one make sure to add local and fast_forward. Local because it is limiting cursors’ scope, and fast_forward to optimize fetches.

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

Sidebar

Related Questions

Sorry the title is not very clear. This is a follow up to my
Sorry if the title is not clear enough, I have <a> elements with the
Sorry if the title is not very clear. I was not sure about the
Sorry if the title isn't very clear, I couldn't think of anything better... I'm
First of all sorry that I could not think of a more descriptive title.
Sorry the title is not very descriptive but it is a tricky problem to
Sorry the title is very vague. Basically, I'm stuck with something. I have an
Sorry if my title is not clear, I'm not sure how to word this.
Sorry for the title, not easy to phrase it correctly. I have a table
Sorry if title is not too clear but I think it's about right. NEhow,

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.