How to solve SQL Server Error 1222 i.e Unlock a SQL Server table

In the SQL Server Management Studio, to find out details of the active transaction, execute following command DBCC opentran() You will get the detail of the active transaction, then from the SPID of the active transaction, get the detail about the SPID using following commands exec sp_who2 <SPID> exec sp_lock <SPID> For example, if SPID … Read more

How do I find the data directory for a SQL Server instance?

It depends on whether default path is set for data and log files or not. If the path is set explicitly at Properties => Database Settings => Database default locations then SQL server stores it at Software\Microsoft\MSSQLServer\MSSQLServer in DefaultData and DefaultLog values. However, if these parameters aren’t set explicitly, SQL server uses Data and Log … Read more

Is a single SQL Server statement atomic and consistent?

I’ve been operating under the assumption that a single statement in SQL Server is consistent That assumption is wrong. The following two transactions have identical locking semantics: STATEMENT BEGIN TRAN; STATEMENT; COMMIT No difference at all. Single statements and auto-commits do not change anything. So merging all logic into one statement does not help (if … Read more

Rebuild all indexes in a Database

Try the following script: Exec sp_msforeachtable ‘SET QUOTED_IDENTIFIER ON; ALTER INDEX ALL ON ? REBUILD’ GO Also I prefer(After a long search) to use the following script, it contains @fillfactor determines how much percentage of the space on each leaf-level page is filled with data. DECLARE @TableName VARCHAR(255) DECLARE @sql NVARCHAR(500) DECLARE @fillfactor INT SET … Read more

Select query to remove non-numeric characters

See this blog post on extracting numbers from strings in SQL Server. Below is a sample using a string in your example: DECLARE @textval NVARCHAR(30) SET @textval=”AB ABCDE # 123″ SELECT LEFT(SUBSTRING(@textval, PATINDEX(‘%[0-9.-]%’, @textval), 8000), PATINDEX(‘%[^0-9.-]%’, SUBSTRING(@textval, PATINDEX(‘%[0-9.-]%’, @textval), 8000) + ‘X’) -1)

Cannot use UPDATE with OUTPUT clause when a trigger is on the table

Visibility Warning: Don’t the other answer. It will give incorrect values. Read on for why it’s wrong. Given the kludge needed to make UPDATE with OUTPUT work in SQL Server 2008 R2, I changed my query from: UPDATE BatchReports SET IsProcessed = 1 OUTPUT inserted.BatchFileXml, inserted.ResponseFileXml, deleted.ProcessedDate WHERE BatchReports.BatchReportGUID = @someGuid to: SELECT BatchFileXml, ResponseFileXml, … Read more