0

i have a table and a field like ID(it has been added later) and i want to write to my table's ID field, numbers from 1 to 1000. for this i wrote a code like this

            for (int i = 0; i < 75440; i++)
            {
                baglan.Open();
                SqlCommand kmt = new SqlCommand();
                kmt.Connection = baglan;
                kmt.CommandText = "UPDATE EvrakArsiv SET ID='" + (i + 1) + "' ";
                try
                {
                    kmt.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                baglan.Close();
            }
            for (int i = 0; i < 75440; i++)
            {
                baglan.Open();
                SqlCommand kmt = new SqlCommand();
                kmt.Connection = baglan;
                kmt.CommandText = "UPDATE EvrakArsiv SET ID='" + (i + 1) + "' ";
                try
                {
                    kmt.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                baglan.Close();
            }

But it returns a value on the ID Field: all rows value are same. like 9, like 1745, like 3578 whats my wrong?

paradoxIST
  • 31
  • 7

4 Answers4

0

WHERE clause is missing in update statements

Manish Dalal
  • 1,768
  • 1
  • 10
  • 14
0

Try this..

Make your ID column as Identity in your DBMS. Like this http://stackoverflow.com, it will automatically insert values from 1 in table, and then use DBCC CHECKIDENT command to set Identity column for new entry..

Community
  • 1
  • 1
Shahid Iqbal
  • 2,095
  • 8
  • 31
  • 51
  • thanks body it gives THE ID field identy but numbers dont increase properly. such as from 1 to 157 is normal but then number gets value 213 then everything goes wrong. whats the reason of this. i have a big database contains 75000 rows. is this the reason of that? – paradoxIST Apr 29 '14 at 12:31
  • after i did your said, also must be Primary Key From Design. i did ID Primary key and numbers have been fixed. – paradoxIST Apr 29 '14 at 12:45
0

WHERE clause is missing here. What makes your row unique give those conditions in WHERE clause

Mohini Mhetre
  • 912
  • 10
  • 29
0

You can use the following query to update the column:

with cte
As
(
SELECT     <column list separated by comma>, Id, 
    ROW_NUMBER() over(order by <column name>) rno
    FROM EvrakArsiv 
)
Update cte
set Id = rno

It will virtually sort the data on the column mentioned in order by clause, and then set the row number to respective Id in table.

Roopesh
  • 279
  • 2
  • 7
  • i dont understand,can you explain with an example? – paradoxIST Apr 29 '14 at 11:50
  • Suppose I have a table favourite with following columns Favourites_ID, Game_name, Game_platform, DatePicked. Now I have added one more column New_ID to that. I want to set new_id with the record number in the table. Then I can use the following query `with cte As ( SELECT Favourites_ID, Game_name, Game_platform, DatePicked, new_id, ROW_NUMBER() over(order by Favourites_id) rno FROM Favourites ) Update cte set new_id = rno` – Roopesh Apr 29 '14 at 12:23
  • It is ok. But setting the column as primary key is not necessary. When you do select * from , by default the result comes sorted on the primary key column. So if you create the identity column without primary key and then use select * from
    order by Id, it will give you correct output. Primary key is something design decisions, which should be carefully chosen.
    – Roopesh Apr 29 '14 at 12:56
  • i know, but for me better the numbers seem properly :) – paradoxIST Apr 29 '14 at 13:06