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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:38:18+00:00 2026-06-04T16:38:18+00:00

UPDATE dbo.FormDetail SET FieldOrder=FieldOrder+1 WHERE WHERE DocCode IN (1,2,3) AND FieldOrder >= (SELECT FieldOrder

  • 0
UPDATE
    dbo.FormDetail
SET 
    FieldOrder=FieldOrder+1
WHERE
    WHERE DocCode IN (1,2,3)
    AND FieldOrder >= (SELECT FieldOrder FROM dbo.FormDetail
                       WHERE FieldData LIKE '%OldField%'
                       AND DocCode IN (1,2,3))

Don’t know how clear it is what I’m trying to do here but I want to increase the FieldOrder in the FormDetail table for a number of documents but only after a certain field (basically so I have a gap to insert a new field). But the obvious problem is that the FieldOrder I get in my Where will not be specific to the document that the statement may currently be updating. I was thinking it’s probably possible using partitions but I have only ever used partitions in From sections. Any help would be really appreciated.

UPDATE

dbo.FormDetail Sample Data

DocCode FieldOrder FieldData

1 1 ‘Title’

1 2 ‘OldField’

1 3 ‘Signature’

2 1 ‘Paragraph’

2 2 ‘OldField’

3 1 ‘OldField’

  • 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-04T16:38:19+00:00Added an answer on June 4, 2026 at 4:38 pm

    In your existing code your sub-query is executed once and returns all it’s results in one go to your outer query.

    What you actually appear to want is for the sub-query to be executed once for every row being processed by the outer query. The is called a correlated sub-query.

    To make it work like that I have made two changes;
    1. I’ve given the table an alias in the sub-query
    2. I’ve changed the sub-query WHERE clause to reference the table in the outer-query

    UPDATE
      dbo.FormDetail
    SET
      FieldOrder=FieldOrder+1
    WHERE
      DocCode IN (1,2,3)
      AND FieldOrder >= (SELECT lookup.FieldOrder
                           FROM dbo.FormDetail AS lookup
                          WHERE lookup.FieldData LIKE '%OldField%'
                            AND lookup.DocCode = FormDetail.DocCode
                        )
    

    Another alternative could be to join on a sub-query…

    UPDATE
      FormDetail
    SET
      FieldOrder=FieldOrder+1
    FROM
      dbo.FormDetail
    INNER JOIN
    (
      SELECT
        DocCode,
        FieldOrder
      FROM
        dbo.FormDetail
      WHERE
        DocCode IN (1,2,3)
        AND FieldData LIKE '%OldField%'
    )
      AS lookup
        ON  lookup.DocCode     = FormDetail.DocCode
        AND lookup.FieldOrder >= FormDetail.FieldOrder
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a sql script that looks like UPDATE dbo.Pictures SET Data = (SELECT
In Microsoft SQL Server 2008, I want to do the following: UPDATE dbo.Test SET
Like any other question I am facing problem whenever I am trying to UPDATE
update dbo.tblMessages set messageFlags = (messageFlags + 1) where messageId = @messageId So that's
CREATE TRIGGER dbo.updateTrigger ON dbo.Education AFTER UPDATE AS BEGIN SET NOCOUNT ON; IF NOT
I am trying to update multiple rows in one table, based on a select
SQL Server novice here. UPDATE dbo.ObjectivesApproved SET dbo.ObjectivesApproved.VAP = 'Y' WHERE ((dbo.Approved.Cri_Group In ('X01'
I'm trying to update an empty XML Tag in my Xml string on sql
I'm trying to write a SQL Server database update script. I want to test
This is kind of a part 2 to this question: Select rows and Update

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.