I created some columns in our production database but now I need to drop one. However this seems impossible and I do not know why. I dropped the column in our development database just fine. I created a table in production and dropped that. But when I try to remove this one column I get the error. I used management studio (which fails quickly) and T-SQL (which takes longer but still fails).
ALTER TABLE VEN DROP COLUMN RI_VEN_Approved
Any idea whats going on?
I checked for locks using a query found here: https://stackoverflow.com/questions/1511675/how-to-check-if-a-table-is-locked-in-sql-server
Using this query specifically:
SELECT resource_type, resource_associated_entity_id,
request_status, request_mode,request_session_id,
resource_description, o.object_id, o.name, o.type_desc
FROM sys.dm_tran_locks l, sys.objects o
WHERE l.resource_associated_entity_id = o.object_id
and resource_database_id = DB_ID()
The table did not appear in the results.
I ran sp_who2
and did not see anything in the BlkBy
column.
Best Answer
Drop the table using T-SQL as then the query will not timeout but instead get blocked. Note down the spid of this query window (it should be visible in the bottom of SSMS, right next to username). Once this goes into "executing" stage, open another query window and do this,
Look at the blocked column and note down the spid number. Next query to see who the owner of the spid is (this will tell you who owns the conflicting lock on this table).
Once you identify this, you can use
dbcc inputbuffer(spid)
to find out what query/command it is doing that has taken a lock on this table. If you deem that query inconsequential, then you can kill it usingkill <spidnumber>.
After this your query for the drop table/Column should complete.Updated: Try to drop column
ALTER TABLE table_name DROP COLUMN column_name;
Check this link: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/77fc7e09-ed14-481f-89d5-d5e143dab7bd/trying-to-delete-a-table-in-sql-server-management-studio-and-getting-a-strange-error?forum=sqldatabaseengine