Create PROCEDURE [dbo].[SleepedConnKill] AS SET NOCOUNT ON; Declare @StrKill Varchar(30) Declare @SPID_Cur Numeric(18,0) Declare SysProcess_Cursor Cursor LOCAL For Select SPID From sys.sysprocesses Where SPID>50 And Status='sleeping' And DateDiff(MINUTE ,Last_Batch,GetDate())>5 Open SysProcess_Cursor Fetch Next From SysProcess_Cursor Into @SPID_Cur While @@Fetch_Status = 0 Begin Set @StrKill = 'KILL '+ convert(varchar(10),@SPID_Cur) EXEC(@StrKill) Fetch Next From SysProcess_Cursor Into @SPID_Cur End Close SysProcess_Cursor Deallocate SysProcess_Cursor Return
Thursday, 17 March 2011
Kill Sleeping Connection in Sql
Tuesday, 15 March 2011
UPDATE STATISTICS Fot all tables in Database
Here is the sp which update STATISTICS for all table.
EXEC sp_updatestats
Update STATISTICS is necessary to avoid deadlock in database.
Monday, 14 March 2011
Using ROW_NUMBER() Function Fetch Exact number of Row.
Fetch Exact 10th row from table where there is no primary key.
select * from
(Select Emp_First_Name ,ROW_NUMBER() OVER(ORDER BY Emp_First_Name DESC) AS 'Emp' From Table)Qry Where Qry.emp = 10
(Select Emp_First_Name ,ROW_NUMBER() OVER(ORDER BY Emp_First_Name DESC) AS 'Emp' From Table)Qry Where Qry.emp = 10
Monday, 7 March 2011
Get how many Number Of Rows and Coulmns and datasize in all tables in particular Database.
CREATE TABLE #temp (
table_name sysname ,
row_count INT,
reserved_size VARCHAR(50),
data_size VARCHAR(50),
index_size VARCHAR(50),
unused_size VARCHAR(50))
SET NOCOUNT ON
INSERT #temp
EXEC sp_msforeachtable 'sp_spaceused ''?'''
SELECT a.table_name,
a.row_count,
COUNT(*) AS col_count,
a.data_size
FROM #temp a
INNER JOIN information_schema.columns b
ON a.table_name collate database_default
= b.table_name collate database_default
GROUP BY a.table_name, a.row_count, a.data_size
ORDER BY CAST(REPLACE(a.data_size, ' KB', '') AS integer) DESC
DROP TABLE #temp
Wednesday, 2 March 2011
Update Tables Of 2 muliple databases in bulk
UPDATE db1.dbo.tblA
SET Coulmnname = db2.DBO.tblA.Coulmnname
FROM db2.DBO.tblA INNER JOIN db1.dbo.tblA ON
db2.DBO.tblA.PKCOULMN = db1.dbo.tblA.PKCOULMN
SET Coulmnname = db2.DBO.tblA.Coulmnname
FROM db2.DBO.tblA INNER JOIN db1.dbo.tblA ON
db2.DBO.tblA.PKCOULMN = db1.dbo.tblA.PKCOULMN
Subscribe to:
Posts (Atom)