简体   繁体   中英

C# SQL: How can I increment an integer value in a database by one on multiple records?

I am looking for the best way to update the value I store on the following structure:

Table: Pages

Fields:

  1. id (int)
  2. bookid (int)
  3. pageorder (int)
  4. filename (string/varchar)

So I have the book order and a page number. I need to insert a page before the page number I have, lets say 25, and update all the other pages to have 1 added to their pageorder value.

Can I do this without pulling a list and cycling threw it running updates, or is that the best way to go about it?

Thanks!

declare @newpage int
set @newpage = 25
update pages set pageorder = pageorder +1 where pageorder >= @newpage and bookid = @bookid

something like that?

Strictly with SQL, Like this

update pages 
set pageorder = pageorder + 1
where bookid=@bookid
and pageorder >= @pageorder;

insert into Pages
(id,bookid,pageorder,filename)
values
(@id,@bookid,@pageorder,@filename);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM