Tuesday, October 20, 2015
Generate Script for all Non-Cluster Indexes in a database
Thursday, May 21, 2015
Install SQL Server 2012 in Stand Alone Machine
Difference between DateTime and DateTime2 DataType
SQL Server Job history
Using below script you can fetch the specific job history like executing date and durations
SQL Script to Change the database to read only and read write
Below script is used to change the database into single user mode. To kill the current connections change the database to single user mode then change it to single user mode.Replace the database name from MyDATABASE to the database name need to change to READ ONLY
USE [master]
GO
ALTER DATABASE [MyDATABASE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [MyDATABASE] SET READ_ONLY WITH NO_WAIT
GO
ALTER DATABASE [MyDATABASE] SET MULTI_USER
GO
Below script is used to change the database into read/write user mode.
ALTER DATABASE [MyDATABASE] SET READ_WRITE WITH NO_WAIT
Rename the sql server database
There are couple of options are available to rename the database.
Option 1 :
Rename the "MyDATABASE" to "MyDATABASE_NEW" using sp_renamedb command
EXEC sp_renamedb 'MyDATABASE', 'MyDATABASE_NEW'
Option 2:
Rename the "MyDATABASE" to "MyDATABASE_NEW" using ALTER DATABASE Commnad
USE [master]
GO
ALTER DATABASE [MyDATABASE] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [MyDATABASE] MODIFY NAME = MyDATABASE_NEW
GO
ALTER DATABASE [MyDATABASE_NEW] SET MULTI_USER
GO
SQL Server installation methods
TDSSNIClient initialization failed with error 0x80092004, status code 0x1. Reason: Initialization failed with an infrastructure error. Check for previous errors. Cannot find object or property.
2> Check the following registry key
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10.SQL2008\MSSQLServer\SuperSocketNetLib\Certificate
and clear the Certificate values.
Wednesday, August 13, 2014
Shrink Database Log file in SQL Server
Method 1 : This script will shrink the all log files of specific database by change the recovery modle to simple and again it rever back the recovery model
DECLARE @tbl TABLE
(
ID INT IDENTITY(1,1),
DBNAME NVARCHAR(1000),
[FileName] NVARCHAR(1000)
)
INSERT INTO @tbl
SELECT DB_NAME(DbID),st.NAme from sys.sysaltfiles ST INNER JOIN Sys.databases SB on ST.dbid = sb.database_id where sb.name = 'TEST2008' AND FileName LIKE '%ldf' and sb.state = 0
DECLARE @MinID INT , @MaxID INT,@DBName NVARCHAR(1000),@FileName NVARCHAR(1000),@RecoveryModel NVARCHAR(1000),@SQL NVARCHAR(MAX)
SELECT @MinID = MIN(ID),@MaxID = MAX(ID) FROM @tbl
WHILE(@MinID <=@MaxID)
BEGIN
SELECT @DBName = DBNAME,@FileName=[FileName] FROM @tbl where ID = @MinID
SELECT @RecoveryModel = recovery_model_desc FROM sys.databases where name = @DBName
SELECT @SQL= N'USE ['+ @DBName+']'+CHAR(10)+CHAR(10)
+CASE WHEN @RecoveryModel <> N'SIMPLE' THEN N'ALTER DATABASE ['+@DBName+'] SET RECOVERY SIMPLE WITH NO_WAIT'ELSE N''END+CHAR(10)+
+N'DBCC SHRINKFILE('+@FileName+',1)'+CHAR(10)
+CASE WHEN @RecoveryModel <> N'SIMPLE' THEN N'ALTER DATABASE ['+@DBName+'] SET RECOVERY '+@RecoveryModel+N' WITH NO_WAIT'+CHAR(10) ELSE N'' END
+CHAR(10)
PRINT @SQL
EXEC SP_EXECUTESQL @SQL
SELECT @MinID = @MinID +1
END
Method 2: This script will shrink the all databases log files, depends on the size of log file by change the recovery modle to simple and again it rever back the recovery model
DECLARE @tbl TABLE
(
ID INT IDENTITY(1,1),
DBNAME NVARCHAR(1000),
[FileName] NVARCHAR(1000)
)
INSERT INTO @tbl
SELECT DB_NAME(DbID),st.NAme from sys.sysaltfiles ST INNER JOIN Sys.databases SB on ST.dbid = sb.database_id where Size > 100000 AND FileName LIKE '%ldf' and sb.state = 0
DECLARE @MinID INT , @MaxID INT,@DBName NVARCHAR(1000),@FileName NVARCHAR(1000),@RecoveryModel NVARCHAR(1000),@SQL NVARCHAR(MAX)
SELECT @MinID = MIN(ID),@MaxID = MAX(ID) FROM @tbl
WHILE(@MinID <=@MaxID)
BEGIN
SELECT @DBName = DBNAME,@FileName=[FileName] FROM @tbl where ID = @MinID
SELECT @RecoveryModel = recovery_model_desc FROM sys.databases where name = @DBName
SELECT @SQL= N'USE ['+ @DBName+']'+CHAR(10)+CHAR(10)
+CASE WHEN @RecoveryModel <> N'SIMPLE' THEN N'ALTER DATABASE ['+@DBName+'] SET RECOVERY SIMPLE WITH NO_WAIT'ELSE N''END+CHAR(10)+
+N'DBCC SHRINKFILE('+@FileName+',1)'+CHAR(10)
+CASE WHEN @RecoveryModel <> N'SIMPLE' THEN N'ALTER DATABASE ['+@DBName+'] SET RECOVERY '+@RecoveryModel+N' WITH NO_WAIT'+CHAR(10) ELSE N'' END
+CHAR(10)
PRINT @SQL
EXEC SP_EXECUTESQL @SQL
SELECT @MinID = @MinID +1
END
Kill all user SPIDs in a SQL Server Database
Using this script you can kill all the SPID connected to TEST2008 database
DECLARE @DB SYSNAME
SET @DB = 'Test2008' -- Here you need to give the database name where you need to kill all connection
DECLARE @SPID VARCHAR(4), @cmdSQL VARCHAR(10)
IF OBJECT_ID('tempdb..#KilledSpidsInfo ') IS NOT NULL
BEGIN
DROP TABLE #KilledSpidsInfo
END
SELECT * into #KilledSpidsInfo FROM master.dbo.sysprocesses
WHERE SPID > 50
AND SPID != @@SPID
AND DBID = DB_ID(@DB)
select * from #KilledSpidsInfo
DECLARE cCursor CURSOR
FOR SELECT CAST(SPID AS VARCHAR(4)) FROM master.dbo.sysprocesses
WHERE SPID > 50
AND SPID != @@SPID
AND DBID = DB_ID(@DB)
OPEN cCursor
FETCH NEXT FROM cCursor INTO @SPID
WHILE @@FETCH_STATUS = 0
BEGIN
SET @cmdSQL = 'KILL ' + @SPID
EXEC (@cmdSQL)
FETCH NEXT FROM cCursor INTO @SPID
END
CLOSE cCURSOR
DEALLOCATE cCURSOR
Monday, August 11, 2014
change server level collation for a SQL Server Instance
Transfer logins and passwords between instances of SQL Server(revlogin)
Cannot access the specified path or file on the server. Verify that you have the necessary security privileges and that the path or file exists
If you received below error while attaching a .mdf file in cluster environment please follow below steps to resolve the issue ERROR Ca...
-
Problem Cannot drop the user 'dbo'. Rename failed for User 'dbo'. (Microsoft.SqlServer.Smo) Cannot alter the user &...
-
If you received below error while attaching a .mdf file in cluster environment please follow below steps to resolve the issue ERROR Ca...
-
If SQL Server agent not started and if you find below error in event viewer Error Details Log Name : Applicatio...