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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:44:47+00:00 2026-05-27T13:44:47+00:00

I’m using a cursor to update a single field in a table, and I’m

  • 0

I’m using a cursor to update a single field in a table, and I’m attempting to declare the cursor using an ORDER BY in the text of the select.

I’ve got the following example data:

testTable:
RecordGuid RecordID DupeParentID
---------- -------- ------------
[guid]     A        Y
[guid]     A        N
[guid]     A        N
[guid]     A        N
[guid]     B        Y
[guid]     B        N
[guid]     B        N
[guid]     C        Y
[guid]     C        N
[guid]     C        N

And script:

DECLARE @allcounter INT
SET @allcounter = 1

SELECT RecordID, count(*) as [NumberDupes]
INTO #RecordGroupCounts
FROM testTable
GROUP BY RecordID

DECLARE @temp VARCHAR(500)
DECLARE @current VARCHAR(500)

DECLARE c1 CURSOR 
FOR 
    SELECT RecordID FROM testTable WHERE RecordID IN (SELECT RecordID FROM testTable WHERE DupeParentID = 'Y') 
    ORDER BY RecordID
FOR UPDATE OF RecordID

OPEN c1

FETCH NEXT FROM c1 INTO @current
FETCH NEXT FROM c1 INTO @current

WHILE @@fetch_status = 0
BEGIN
    UPDATE testTable
    SET RecordID = RecordID + '-' + cast(@allcounter AS VARCHAR)
    WHERE CURRENT OF c1

    IF (@allcounter + 1) = (SELECT [NumberDupes] FROM #RecordGroupCounts WHERE RecordID = @current)
    BEGIN
        FETCH NEXT FROM c1 INTO @current
        SET @allcounter = 0
    END 
    SET @allcounter = @allcounter + 1

    FETCH NEXT FROM c1 INTO @current
END
CLOSE c1
DEALLOCATE c1

The desired output of all of this is:

RecordGuid RecordID DupeParentID
---------- -------- ------------
[guid]     A        Y
[guid]     A-1      N
[guid]     A-2      N
[guid]     A-3      N
[guid]     B        Y
[guid]     B-1      N
[guid]     B-2      N
[guid]     C        Y
[guid]     C-1      N
[guid]     C-2      N

I’m working with SQL Server 2000 so I don’t have ROW_NUMBER() available – I know the common way to do this is with loops, but I am by no means a DBA, and this currently works if I remove my ORDER BY RecordID in the cursor declaration.

With as small as my current test table is this seems to be working fine, but the reason I’m attempting to order this is that I’m fairly sure it’ll break if the RecordIDs aren’t in order (by RecordID ASC, DupeParentID DESC) and I intend to use this on a much larger set of records semi-regularly. Is there a way to define the order for a cursor that updates? Is the cursor ordered automatically somehow? If not, is there a simpler (or faster) way to write this for SQL Server 2000?

  • 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-27T13:44:48+00:00Added an answer on May 27, 2026 at 1:44 pm
    select recordid, max(recordguid)
    from testable
    where DupeParentID = 'N'
    group by recordID
    

    The above statement should return 1 row per recordID with the max(guID) for that recordID. heh, spell check keeps changing guid to guide. Now we can increment all of these with the 1.

    update testtable 
    set recordID = recordID + '-1'
    where recordguid in (select recordguid from (    select recordid, max(recordguid) recordguid
    from testable
    where DupeParentID = 'N'
    group by recordID) a)
    

    Get the logic here? What we’re doing is taking the first duplicate ID using the max recordguid as an identifier for the ‘first’…lil arbitrary, we could use min as well as long as it returns just one guID for each recordID. If you had some other logic as to which record was to be called the -1 vs the -2, you can include it here.

    This will create all the recordID-1 (A-1,B-1) and leave the rest alone. Now you should be able to loop this and increment the -1 as needed…or you could just repeat run it manually incrementing the -1 yourself if this is a one time fix…your call there.

    Let me know how it goes…the logic should work and will be quite quicker than going through each recordID at a time.

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

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
i got an object with contents of html markup in it, for example: string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
We're building an app, our first using Rails 3, and we're having to build

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.