Link to home
Create AccountLog in
Microsoft SQL Server

Microsoft SQL Server

--

Questions

--

Followers

Top Experts

Avatar of eddeane
eddeane

ERROR 1222: Lock request timeout
Hi All

Hopefully someone can help me with this one. (new to TSQL, know a bit of PL/SQL).

Am trying to get an extract file created through a stored procedure as follows:


CREATE PROCEDURE PROC1
AS

BEGIN

DECLARE @v_accounts1      numeric(10,0);
DECLARE @v_accounts2     numeric(10,0);
DECLARE @ErrorCode          int;

BEGIN TRANSACTION

select a.ID, z.agreement1
into EXTRACTOUT
FROM TABLE1

EXECUTE master..xp_cmdshell 'BCP DB_NAME..EXTRACTOUT OUT C...\test3.txt -S SERVERNAME -U SA -P -t "," -c'

SELECT  @ErrorCode = @@Error;

If @ErrorCode <> 0
     BEGIN
              GOTO ERR_HANDLER  
     END

COMMIT TRANSACTION

DROP TABLE EXTRACTOUT
Return(0)
END

ERR_HANDLER:
   print 'Procedure PROC1 terminated - transaction rolledback'
   print 'Error Code: '
   print  @ErrorCode
   DROP TABLE EXTRACTOUT
   Rollback transaction

GO

But on executing this I manage to lock everything up (executing query batch runs endlessly), am unable to cancel this activity, and when i try to view the current activity for locks, i get Error 1222: Lock request time out period exceeded

It's now not possible to access this database in anyway!

Can someone shed a bit of light on this problem for me? Have I locked things at database level!? What is the correct procedure to resolve this (ideally without restarting the db)?

Many thanks in advance
Ed

Zero AI Policy

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of adatheladadathelad🇬🇧

Can you not get into query analyser?
If you can...
You can use the sp_who system stored procedure (EXECUTE sp_who) to return a list of the currently running processes. Find your process that you started, and note the spid value. Then you can try issuing a KILL <spid_value> command which will attempt to kill off that process. Note that this may take some time.

If not, you may need to restart the server.

Avatar of adatheladadathelad🇬🇧

Also, I think the syntax in your procedure is wrong.
Try these steps:

1) Remove the "BEGIN" from near the top of the procedure
2) Remove the "END" from the line after "Return(0)"

Cheers

Avatar of ispalenyispaleny🇨🇿

1. It is a deadlock of system tables2.
2. Solution

select * from master.dbo.sysprocesses
exec sp_lock
then
KILL <put spid here>

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of adatheladadathelad🇬🇧

...and remove the ";" at the end of the line that reads:
"SELECT  @ErrorCode = @@Error;"

ASKER CERTIFIED SOLUTION
Avatar of ispalenyispaleny🇨🇿

Link to home
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Create Account

Avatar of ispalenyispaleny🇨🇿

No problem with semicolons, they are standard SQL command delimiters :)

Avatar of ispalenyispaleny🇨🇿

EXECUTE master..xp_cmdshell starts new thread, outside the transaction.
You can add "no_output" option to prevent waiting for completition of task, but it cannot help.

Good luck!

Free T-shirt

Get a FREE t-shirt when you ask your first question.

We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.


Avatar of eddeaneeddeane

ASKER

Is difficult to say that one of the comments offered were the correct ones. The answer was a combination of all the comments offered. Is there a way of distributing the points across more than one anwer?

Avatar of adatheladadathelad🇬🇧

Only to create a new question saying "Points for <whoever>" and accepting the user's comment when they post a reply. Glad you got it fixed.

Avatar of ispalenyispaleny🇨🇿

Thanks. Can you post your corrected code ?

Reward 1Reward 2Reward 3Reward 4Reward 5Reward 6

EARN REWARDS FOR ASKING, ANSWERING, AND MORE.

Earn free swag for participating on the platform.


Avatar of eddeaneeddeane

ASKER

Many thanks ispaleny and adethelad for all your help, am on a fairly steep learn curve at the moment and your feedback was really useful.
It does seem a fairly odd way to write to a file though, ie going through the command line, am used to using the UTL_FILE package of PL/SQL and presumed there would be something similar for TSQL.
Also, in order to write a header/trailer to an extract, it seems the best thing to do is create an extra file for each of these and just merge the files using xp_cmdshell again. Do you think this is the best way?

Ed

Corrected code as requested:

CREATE PROCEDURE PROC1
AS

DECLARE @v_accounts1      numeric(10,0);
DECLARE @v_accounts2     numeric(10,0);
DECLARE @ErrorCode          int;

select a.ID
into EXTRACTOUT
FROM TABLE1 a

BEGIN TRANSACTION

EXECUTE master..xp_cmdshell 'BCP DB_NAME..EXTRACTOUT OUT C...\test3.txt -S SERVERNAME -U SA -P -t "," -c'

SELECT  @ErrorCode = @@Error;

If @ErrorCode <> 0
    BEGIN
             GOTO ERR_HANDLER  
    END

COMMIT TRANSACTION

DROP TABLE EXTRACTOUT
Return(0)

ERR_HANDLER:
  print 'Procedure PROC1 terminated - transaction rolledback'
  print 'Error Code: ' + @ErrorCode
 
  DROP TABLE EXTRACTOUT
  Rollback transaction

GO

Avatar of ispalenyispaleny🇨🇿

1.
DROP TABLE EXTRACTOUT
Rollback transaction

If an error occurs, you drop table and then you rollback drop table. Your SP will not work anymore.

Use

Rollback transaction
DROP TABLE EXTRACTOUT

2. To your question about ORACLE
I am not too familiar with PL/SQL and ORACLE administration.
Microsoft SQL Server

Microsoft SQL Server

--

Questions

--

Followers

Top Experts

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.