广

MSSQL

  • MYSQL
  • MSSQL
  • Redis
  • MongoDB
  • oracle数据库
  • 数据管理

    SQLServer2005 的查询独占模拟

    2018-04-25 21:47:34 次阅读 稿源:互联网
    零七网广告
    全网推广平台,软文发布
    对于应用来说,我并不提倡人为给记录加锁,这样会惹来很多麻烦,况且锁并不能解决所有问题,如果你有这方面好的经验我们可以进一步交流。
    对于应用来说,我并不提倡人为给记录加锁,这样会惹来很多麻烦,况且锁并不能解决所有问题,如果你有这方面好的经验我们可以进一步交流。
    代码如下:

    set nocount on
    use tempdb
    go
    if (object_id ('tb' ) is not null )
    drop table tb
    go
    create table tb (id int identity (1 , 1 ), name varchar (10 ), tag int default 0 )
    insert into tb (name ) select 'a'
    insert into tb (name ) select 'b'
    insert into tb (name ) select 'c'
    insert into tb (name ) select 'd'
    insert into tb (name ) select 'e'
    go
    update top (2 ) tb with (rowlock , readpast ) set tag = 1 output inserted . id , inserted . name where tag = 0
    go
    update top (2 ) tb with (rowlock , readpast ) set tag = 1 output inserted . id , inserted . name where tag = 0
    go
    update top (2 ) tb with (rowlock , readpast ) set tag = 1 output inserted . id , inserted . name where tag = 0
    go
    set nocount off
    /*
    id name
    ----------- ----------
    1 a
    2 b
    id name
    ----------- ----------
    3 c
    4 d
    id name
    ----------- ----------
    5 e
    */

    如果你有更好的建议,我们不妨探讨一下。

    零七网部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与零七网进行文章共享合作。

    零七网广告
    零七网广告
    零七网广告
    零七网广告