SQL Server中的序列编号

时间:2019-11-20 08:02:47

标签: sql sql-server sql-server-2017

我有一个表(始终按ID升序排列),其中包含5条记录:

ID  Sequence
1   1
2   2
3   3
4   4
8   3
9   3

所需的输出是:

ID  Sequence
1   1
2   2
3   3
4   6
8   4
9   5

2 个答案:

答案 0 :(得分:1)

对我来说,好像有3个直接更新,没什么让它复杂化的了:

module Main exposing (..)

-- Press buttons to increment and decrement a counter.
--
-- Read how it works:
--   https://guide.elm-lang.org/architecture/buttons.html
--


import Browser
import Html exposing (Html, button, div, text, table, tbody, tr, td, input)
import Html.Events exposing (onClick, onInput)
import Html.Attributes exposing (contenteditable)



-- MAIN


main =
  Browser.sandbox { init = init, update = update, view = view }



-- MODEL


type alias Model = Int


init : Model
init =
  0



-- UPDATE


type Msg
  = Increment
  | Decrement


update : Msg -> Model -> Model
update msg model =
  case msg of
    Increment ->
      model + 1

    Decrement ->
      model - 1


testOnInputHandler : String -> Msg
testOnInputHandler str =
  let
    log = Debug.log "in testOnInputHandler" "here"
  in
    Increment

-- VIEW


view : Model -> Html Msg
view model =
  div []
    [ button [ onClick Decrement ] [ text "-" ]
    , div [] [ text (String.fromInt model) ]
    , button [ onClick Increment ] [ text "+" ]
    , table []
      [ tbody []
        [ tr [] 
          [ td [ contenteditable True, onInput testOnInputHandler] [ text "editable!" ]
          ]
        ]
      ]
    , input [ onInput testOnInputHandler ] []
    ]

答案 1 :(得分:0)

如果您想一步一步做到这一点:

update t
    set sequence = v.sequence
    from t join
         (values (4, 6), (8, 4), (9, 5)
         ) v(id, sequence)
         on t.id = v.id;

如果您必须进行许多此类更新,则分别调用update会产生额外的开销。