Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Read Lock a Table in SQL Server?

Read Lock a Table in SQL Server?

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestionannouncement
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kumar bs
    wrote on last edited by
    #1

    I want to lock a table in SQL Server, within a transaction. Unless i release lock, no other transaction should not be able to even read (execute select statement) on that table. Any Ideas? kumar

    A P 2 Replies Last reply
    0
    • K kumar bs

      I want to lock a table in SQL Server, within a transaction. Unless i release lock, no other transaction should not be able to even read (execute select statement) on that table. Any Ideas? kumar

      A Offline
      A Offline
      andyharman
      wrote on last edited by
      #2

      Hi Kumar You should be able to do the following within a transaction: SELECT @dummy = COUNT(*) FROM MyTable WITH (TABLOCKX, HOLDLOCK) The "TABLOCK" and "HOLDLOCK" keywords are locking hints. You should be able to look these up in your Books-Online. What are you trying to do while the table is locked? I would advise you to keep your transaction reeeeaaaally short. Regards Andy

      K R 2 Replies Last reply
      0
      • A andyharman

        Hi Kumar You should be able to do the following within a transaction: SELECT @dummy = COUNT(*) FROM MyTable WITH (TABLOCKX, HOLDLOCK) The "TABLOCK" and "HOLDLOCK" keywords are locking hints. You should be able to look these up in your Books-Online. What are you trying to do while the table is locked? I would advise you to keep your transaction reeeeaaaally short. Regards Andy

        K Offline
        K Offline
        kumar bs
        wrote on last edited by
        #3

        thanks Andy, Actually this is just for testing purpose, i want other transaction to wait till the current transaction releases the lock. Is there any mechanism, where in we can hold the lock on table for few seconds so that other transaction should be able to atleast execute Select command?

        K 1 Reply Last reply
        0
        • K kumar bs

          thanks Andy, Actually this is just for testing purpose, i want other transaction to wait till the current transaction releases the lock. Is there any mechanism, where in we can hold the lock on table for few seconds so that other transaction should be able to atleast execute Select command?

          K Offline
          K Offline
          kumar bs
          wrote on last edited by
          #4

          sorry, i am correcting my statement above, "so that other transaction should not be able to atleast execute Select statement"

          A 1 Reply Last reply
          0
          • K kumar bs

            sorry, i am correcting my statement above, "so that other transaction should not be able to atleast execute Select statement"

            A Offline
            A Offline
            andyharman
            wrote on last edited by
            #5

            I normally test these things by opening two sessions in Query Analyzer. However if you want to test this using a stored procedure then use the "WaitFor" T-SQL command to wait for a period of time.

            1 Reply Last reply
            0
            • K kumar bs

              I want to lock a table in SQL Server, within a transaction. Unless i release lock, no other transaction should not be able to even read (execute select statement) on that table. Any Ideas? kumar

              P Offline
              P Offline
              P_pk
              wrote on last edited by
              #6

              you can create trigger for dml staements such as select,update ,delete..So create a trigger while selecting the table.

              PPK

              1 Reply Last reply
              0
              • A andyharman

                Hi Kumar You should be able to do the following within a transaction: SELECT @dummy = COUNT(*) FROM MyTable WITH (TABLOCKX, HOLDLOCK) The "TABLOCK" and "HOLDLOCK" keywords are locking hints. You should be able to look these up in your Books-Online. What are you trying to do while the table is locked? I would advise you to keep your transaction reeeeaaaally short. Regards Andy

                R Offline
                R Offline
                Rajesh Awate
                wrote on last edited by
                #7

                hi, i want to use TABLOCKX lock on my table. i have used same query given in above post. but problem is lock does not get released after completion of transaction holding lock on table. hence another transaction keeps waiting. following are my 2 query i tried in query analyzer of sql server. Please help me... Query 1: BEGIN TRAN A SELECT * FROM alerts with (TABLOCKX, HOLDLOCK) declare @cnt bigint set @cnt = 1000000 print 'start time = '+cast(getdate() as varchar) while @cnt > 0 begin print 'Cnt = ' + cast(@cnt as varchar) set @cnt = @cnt - 1 end print 'end time = '+cast(getdate() as varchar) COMMIT TRAN A ----------------------------------------------------------------------------------- Query 2: BEGIN TRAN B print 'start time = '+cast(getdate() as varchar) SELECT * FROM alerts --with (TABLOCKX) print 'end time = '+cast(getdate() as varchar) COMMIT TRAN B Please let me know if i'm doing anything wrong. Thank in advance... Rajesh.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups