row_number replacement

  • Hi friends,

    I have created temp table with row_number() since i need the latest loanumber with step

    Create Table #Max_step

    (

    Loan_number varchar(10),

    FID Int,

    Step int,

    Modifieddate datetime,

    ROW_id int

    )

    Create table Tableb

    (loan_number varchar(10),

    step int,

    Description varchar(20))

    Insert into #Max_step values('91746',1110,2,'2010-07-31 19:34:35.000',2)

    Insert into #Max_step values('91746',1120,1,'2010-07-31 19:30:35.000',3)

    Insert into #Max_step values('91746',1120,3,'2010-07-31 19:30:35.000',1)

    Insert into Tableb values('91746',1,'description1')

    Insert into Tableb values('91746',2,'description2')

    I am trying to update a column in TableB based on the row_id =2 from max_step

    but every time i check the target column value changes ..example

    updating Column in tableC where max_step.row_id=2 joined with tableB on step and loanumber.

    UPDATE TableC

    SET step_desc= Tableb.StepDescription

    FROM #MAX_STEPMAX_STEP WITH (NOLOCK)

    JOIN TablebB WITH (NOLOCK) ON MAX_STEP.LoanNumber = b.LoanNumber

    and MAx_step.Step=b.step

    WHERE MAX_STEP.Row_ID = 2

    Can Anyone tell me whats the best way to replace row_number() while getting data into max_step table

  • Can you ddl and sample data for TableC? Also, a clear explanation what you are trying would help. I am not sure I understand your requirements.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • I don't see what's wrong with your query other than you don't have a loannumber column. It is loan_number.

    Post the ddl and sample data for TableC and give expected output and someone should be able to help.

  • komal145 (9/26/2012)


    ...

    UPDATE TableC

    SET step_desc= Tableb.StepDescription

    FROM #MAX_STEPMAX_STEP WITH (NOLOCK)

    JOIN TablebB WITH (NOLOCK) ON MAX_STEP.LoanNumber = b.LoanNumber

    and MAx_step.Step=b.step

    WHERE MAX_STEP.Row_ID = 2...

    This is an unrestricted update. Every row in tableC will be updated to the same value of step_desc, because TableC isn't referenced in the FROM list. Since it's a trivial matter to convert a SELECT...FROM into an UPDATE...FROM, why don't you write it and post it here? What you want is a query which will return step_desc from TableC and Tableb.StepDescription.

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply