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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:37:01+00:00 2026-05-30T16:37:01+00:00

I have the following sql DECLARE @tmpSelectedData TABLE — table variable (SlNo INT IDENTITY(1,1)

  • 0

I have the following sql

DECLARE @tmpSelectedData   TABLE  -- table variable
        (SlNo  INT IDENTITY(1,1) PRIMARY KEY
         ,dataID INT NULL
         ,ValID INT NULL
         ,DdrID INT NULL
         ,InrID INT NULL
         ,IprID INT NULL)

-- inserting into table variable
INSERT INTO @tmpSelectedData(dataID)  
    SELECT 
        SQ.dataID    
    FROM 
        @SelectedQuestions SQ 

    -- Update the table variable with some values

    tblData
       dataID,ValID,DdrID,InrID,IprID 
        1-    2-  3  - 4-   5
        2-    7-  4  - 5-       8
        3-    8-  2  - 4-       3
        4-    0-  1  - 2-       5

    @SelectedData  
        dataID
         2
         3
         4

    @tmpSelectedData    
        dataID,ValID,DdrID,InrID,IprID 
            2-    
            3-    
        4-    

UPDATE @tmpSelectedData   
SET IprID = D.dataID,
    DdrID = D.DdrID,
    InrID = D.InrID 
FROM tblData D 
INNER JOIN @SelectedData SD ON SD.dataID = D.dataID  

Using this query, all the rows of @tmpSelectedData will be updated with the value which is corresponding to the first row of tblData

Expected result in @tmpSelectedData:

   dataID,ValID,DdrID,InrID,IprID 
        2-    7-  4  - 5-       8
        3-    8-  2  - 4-       3
        4-    0-  1  - 2-       5

Actual result in @tmpSelectedData:

   dataID,ValID,DdrID,InrID,IprID 
        2-    7-  4  - 5-       8
        3-    7-  4  - 5-       8
        4-    7-  4  - 5-       8

it will update only with the first value

UPDATE @tmpSelectedData   
SET dataID = D.dataID,
    DdrID = D.DdrID,
    InrID = D.InrID 
FROM tblData D 
INNER JOIN @tmpSelectedData SD ON SD.IprID = D.IprID  

When I changed it to @tmpSelectedData from @SelectedData (second query), it will works and update as the expected result.

What is the difference between two queries ?

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

    This is one of the issues with the UPDATE ... FROM syntax – if it, in effect, attempts to update the same row multiple times, the end result will be one of those updates, but which one is indeterminate – and it doesn’t give an error or warning message when this happens.

    What you have in your first UPDATE query is a FROM clause that doesn’t reference the table to be updated at all – it’s effectively an uncorrelated query, such that the entire resultset generated by the FROM clause applies to all rows in the target table. One row (in this case, the "first", although that is ill defined) has been the row to "win" the update (it should be noted, equally, that there’s no guarantee that the same row will "win" against each row in the table being updated).

    In your second query, because the table to be updated is referenced in the FROM clause:

    Specifies that a table, view, or derived table source is used to provide the criteria for the update operation. For more information, see FROM (Transact-SQL).

    If the object being updated is the same as the object in the FROM clause and there is only one reference to the object in the FROM clause, an object alias may or may not be specified.

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

Sidebar

Related Questions

I have the following SQL script: DECLARE @temp table ( ID int IDENTITY(1, 1),
I have the following table variable in SQL Server 2005: DECLARE @results TABLE (id
I have the following t-sql code which generates an error Declare @table TABLE (
I have the following: declare @PrintJob TABLE ( PageNumber Int, Copies Int ) INSERT
I have the the following SQL query Declare @tempcalctbl Table ( ItemId varchar(50), ItemLocation
I have the following SQL DECLARE @StatusOK int DECLARE @StatusFailed int SELECT @StatusOK =
I have the following piece of SQL that does not work: declare @id INT;
I have the following SQL query: Declare @Total_SysDown as int, @Login_SysDown as int Set
I have the following SQL table - Date StoreNo Sales 23/4 34 4323.00 23/4
I have got the following SQL statement (SQL Server): DECLARE @atr nvarchar(255), @con nvarchar(1000),

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.