Microsoft SQL Server
--
Questions
--
Followers
Top Experts
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.
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.
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
2. Solution
select * from master.dbo.sysprocesses
exec sp_lock
then
KILL <put spid here>






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
"SELECT @ErrorCode = @@Error;"
You can add "no_output" option to prevent waiting for completition of task, but it cannot help.
Good luck!

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.






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
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
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
--
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.