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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:43:18+00:00 2026-05-27T17:43:18+00:00

i am using below query to update my all records but it starts update

  • 0

i am using below query to update my all records but it starts update from second row
how can i modify it to do changes from 1’st row ?

i am using mssql 2008

i think i can not use @@FETCH_STATUS as 1’st line as it is global.

thanks in advance

use vivdb

DECLARE @empno as int;
select @empno = 10;

DECLARE Employee_Cursor CURSOR FOR select * from emp
OPEN Employee_Cursor;

FETCH NEXT from Employee_Cursor

WHILE @@FETCH_STATUS = 0
   BEGIN

      update emp set empno = @empno;
      select @empno = @empno+1;

     FETCH NEXT from Employee_Cursor
   END;

CLOSE Employee_Cursor;
DEALLOCATE Employee_Cursor;
GO
  • 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-27T17:43:19+00:00Added an answer on May 27, 2026 at 5:43 pm

    It looks like you want to assign an incremented value to empno starting with 10.

    You can use a CTE and row_number() to do that. No need for a cursor.

    ;with C as
    (
      select empno,
             9 + row_number() over(order by (select 1)) as NewEmpNo
      from emp       
    )
    update C
    set empno = NewEmpNo
    

    You cursor version could look like this to do the same.

    DECLARE @empno AS INT;
    DECLARE @CurEmpNo AS INT;
    
    SELECT @empno = 10;
    
    DECLARE employee_cursor CURSOR FOR
      SELECT empno
      FROM   emp
    
    OPEN employee_cursor;
    
    FETCH NEXT FROM employee_cursor INTO @CurEmpNo
    
    WHILE @@FETCH_STATUS = 0
      BEGIN
          UPDATE emp
          SET    empno = @empno
          WHERE  CURRENT OF employee_cursor;
    
          SELECT @empno = @empno + 1;
    
          FETCH NEXT FROM employee_cursor INTO @CurEmpNo
      END;
    
    CLOSE employee_cursor;
    
    DEALLOCATE employee_cursor;  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using a query like below you can fetch rows in which the color of
i have a query like below and i want to update all elements of
Using SQL Server and T-SQL, how can I update ALL tables tables with a
I am attempting to insert many records using T-SQL's MERGE statement, but my query
I have a Pl/SQL query that pull all records from the 3 tables. That
We have the query below. Using a LEFT OUTER join takes 9 seconds to
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID
I am using SQL Query and below are the tables. Organization OrgID Name RAOGID
I am using SQL Query and below are the tables. Organization OrgID Name RAOID
UPDATE: I found a solution. See my Answer below. My Question How can I

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.