Add an auto-incrementing column ID to an existing table and assign a value to it.

Source: Internet
Author: User

Adding an auto-incrementing column ID to an existing table and assigning values to the table already contains a lot of data.

Stored Procedure

 

Declare @ ID int
Declare @ name varchar (500)
Declare Taba cursor
Select export database1.dbo. ggxxkc. ID, export database1.dbo. ggxxkc. syzy
From export database1.dbo. ggxxkc

Open Taba
Fetch next from Taba into @ ID, @ name
While (@ fetch_status = 0)
Begin
Update your database. DBO. ggxxkc set your database. DBO. ggxxkc. syzybh = @ name where your database. DBO. ggxxkc. ggxxkcid = @ ID
Fetch next from Taba into @ ID, @ name
End

Close Taba
Deallocate Taba

 

You can also compileProgram

 

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. Data. sqlclient;
Using system. drawing;
Using system. text;
Using system. Windows. forms;

Namespace windowsformsapplication1
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}

Private void button#click (Object sender, eventargs E)
{
String STR = @ "Server = 211.85.185.5; user id = sa; Password =; database = newjwc; Connect timeout = 500;
Min pool size = 4; Max pool size = 4; packet size = 3072 ";
Sqlconnection conn = new sqlconnection (STR );
Conn. open ();
String sql1 = "select * From xjbd ";
Sqldataadapter Ada = new sqldataadapter (sql1, Conn );
Dataset DS = new dataset ();
Ada. Fill (DS );
Int id = 1;
Foreach (datarow DR in DS. Tables [0]. Rows)
{

String sql2 = @ "Update xjbd Set ID =" + id. tostring () + "where xsid =" + Dr ["xsid"]. tostring () + "and xbh = '"
+ Dr ["xbh"]. tostring () + "'";
Sqlcommand comm = new sqlcommand (sql2, Conn );
Comm. executenonquery ();
Id ++;
}
}
}

Run it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.