English 中文(简体)
更新许多记录。 要我使用光标吗?
原标题:Update many records.should i use cursor?

i have a problem with Update in Ms Sql Server(TSQL)
suppose that i have a table Person with Description and ID fields and inserted 1000 record to this table with this value

  1      Descript1
  2      Descript2
  3      Descript3
  ..       ......
  ..      ......
  1000   Descript1000

我怎样才能改变这1000个记录 在记录下相同的记录?

1   Description1
2    Description2
3     Description3
......
......
1000      Description1000

should i use cursor? i wrote this query but it doesnt work

    while @Counter<=1000000
      begin
          update Person set Description= Descripton +CONVERT(nvarchar(15),@Counter) where ID>=1
     set @Counter=@Counter+1
      end
最佳回答

不需要 cursor ,只需简单的 更新 :

update Person
set Description = "Description" + convert(varchar(10), ID)
问题回答
UPDATE  Person
SET     Discription = SPACE(Z.n)+ Z.Discription
FROM (  SELECT  ID, 
                Description , 
                ROW_NUMBER() OVER (ORDER BY ID)n
        FROM Person
    )Z




相关问题
Performance impact of indexed view in MS SQL Server 2008

Does anyone have experience with using indexed view in MS SQL Server 2008? I am trying to find out how does indexed view affect performance of insert / update statements, that are adding / updating ...

Lock Escalation - What s happening here?

While altering a table (removing a column) in SQL Server 2008, I clicked the Generate Change Script button and I noticed that the change script it generated drops the column, says "go" and then runs ...

Round to nearest 5 in SQL Server

I have a Money column in my SQL Server 2008 table. In my below query how can I round it to nearest 5$ select FineAmount from tickets Thanks

热门标签