I recently managed to lock up our SQL Server database with a query like this (simplified for clarity):
SELECT *, NULL AS extraColmn
INTO #tempTable
FROM sourceTable
IF @@ROWCOUNT > 0
BEGIN
BEGIN TRANSACTION;
DELETE FROM sourceTable
INSERT INTO sourceTable
SELECT * FROM #tempTable;
COMMIT TRANSACTION;
DROP TABLE #tempTable
END
There are two obvious problems with this. First, the INSERT/SELECT statement should have the columns explicitly listed. Second, when doing that INSERT, the source and destination tables had a different number of columns. As expected, this caused an error:
Msg 213, Level 16, State 1, Line 1
Column name or number of supplied values does not match table definition.
And the query simply failed on our production server.
But strangely, when I ran this same query in SQL Server Management Studio, it gave that error, but then locked up the entire database, to the point where I had to restart the database server.
Why? Is it the transaction? I'm a newbie at those, but I understood that if an error is encountered in a transaction, it's simply rolled back—that's the point. (Note that sourceTable is fairly heavily used on this site, so there would probably have been a few concurrent attempts to access it while this was running.)
Why would this only occur in SQL Server Management Studio?