开发者

TSQL Increment Cursor (row_number) used instead

开发者 https://www.devze.com 2023-03-02 16:38 出处:网络
So I have the following table: ID|Product_Image 300|/300-01.jpg 300|/300-02.jpg 301|/301.jpg 302|/302.jpg

So I have the following table:

ID | Product_Image

300 | /300-01.jpg

300 | /300-02.jpg

301 | /301.jpg

302 | /302.jpg

There could be an unlimited number of images per ID. I need to increment a position id based on the the number of references, and I am having trouble generating the following output:

开发者_Go百科ID | Position | Product Images

300 | 1 | /300-01.jpg

300 | 2 | /300-02.jpg

301 | 1 | /301.jpg

Currently I am using a cursor but having trouble with it


You could use row_number() for that:

select  ID
,       row_number() over (partition by ID order by Product_Image) as Position
,       Product_Image
from    YourTable


drop the cursor and use the T-SQL ROW_NUMBER with an OVER clause. here is a reference

http://msdn.microsoft.com/en-us/library/ms189461.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消