使用增量计数更新值

时间:2015-08-08 10:31:33

标签: sql delphi

我的表看起来像这样

select * from MyTable where Name='A';

现在我需要记录姓名=' A'比如Index Name Value 1 A[1] value A 2 B Value ... 3 A[2] Value ..... 4 C Value C... 和更新,用A [1]替换A的第一次出现等等......

解决方案应该是存储过程,因为我不想在客户端执行此操作

public class MainActivity extends AppCompatActivity {

   // SpannableString ss = new SpannableString(getResources().getString(R.string.hello_world));
   SpannableString ss = new SpannableString("Hello world!");

    final ClickableSpan clickableSpan111 = new ClickableSpan() {
        @Override
        public void onClick(View widget) {
            Toast toast = Toast.makeText(getApplicationContext(),
                    ";)",
                    Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ss.setSpan(clickableSpan111, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

        TextView textView = (TextView) findViewById(R.id.textView);
        textView.setText(TextUtils.concat("my text ", ss));
        textView.setMovementMethod(new LinkMovementMethod());
    }

}

我在MS SQL Server 2008上使用Delphi和ADO组件

1 个答案:

答案 0 :(得分:1)

您可以使用可更新的CTE在SQL Server中执行此操作:

with toupdate as (
      select t.*,
             row_number() over (partition by name order by index) as seqnum,
             count(*) over (partition by name) as cnt
      from mytable t
     ) 
update toupdate
    set name = name + '[' + cast(seqnum as varchar(255)) + ']'
    where cnt > 1;

您可以在任何地方实现此逻辑,方法是在客户端调用查询或将其放入存储过程并调用存储过程。