T-SQL Script to check Powershell Version

You can write $host command on powershell prompt to check powershell version or you can execute the below T-SQL script to check the powershell version.

declare @regkeyval varchar(20), @value varchar(255), @rc int
exec @rc=master.dbo.xp_regread 
  @rootkey= 'HKEY_LOCAL_MACHINE',
  @key='SOFTWARE\Microsoft\PowerShell\1\PowerShellEngine',
  @value_name='PowerShellVersion',
  @regkeyval=@value output


set @value =isnull(@value,'-') 

if @value <> '-'
select 
 @@servername as SQLInstanceName
,case serverproperty('IsClustered') when 1 then cast(serverproperty('computernamephysicalnetbios') as varchar)
 else cast(serverproperty('machinename') as varchar)
 end as WindowsServerName
,@value as PSClientVersion 
,'PS is installed' as PSStatusCheck 
else
insert into #OUTPUT(SQLInstanceName,WindowsServerName,PSStatusCheck)
select 
 @@servername as SQLInstanceName
,case serverproperty('IsClustered') when 1 then cast(serverproperty('computernamephysicalnetbios') as varchar)
 else cast(serverproperty('machinename') as varchar)
 end as WindowsServerName
,'PS nt installed' as PSStatusCheck 

MongoDB Post Installation Configuration

On the completion of MongoDB installation http://sqldbpool.com/2015/02/24/mongodb-installation-on-windows-server/, next step is to configure the MongoDB.

Create a MongoDB config file using any text editor (e.g. NOTEPAD) and save it as MONGO.config into bin directory.

##to store data
dbpath=C:\Program Files\MongoDB 2.6 Standard\Data
 
##all output loging
logpath=C:\Program Files\MongoDB 2.6 Standard\Log\mongo.log
 
##log read and write operations
diaglog=3

Start MongoDB Server, make sure to open the command prompt as Administrator. Go to MongoDB bin directory and write the below command. It will start the MongoDB Server and keep the command prompt running, don’t close it.

mongod.exe --config mongo.config

MongoDBServer

To connect to MongoDB open another window of command prompt, browse the bin directory and type the  MONGO command.
startmongo

You can also create the window service of MongoDB by executing below command. Make sure to open the command prompt as Aadministrator, otherwise you will get the access denied error.

mongod --config mongo.config --install

mongoservice

Script to list out SQL Logins and Database User Mappings

You can use system stored procedure sp_msloginmappings to list out the SQL logins and database user mappings.

Syntax
sp_msloginmappings @Loginname , @Flags

@Loginname – Optional argument, in case if you not specify the Login name procedure will return the result for all the SQL Server logins
@Flags – You can specify value 0 or 1, 0 value will show user mapping to all databases and 1 will show the user mapping to current database only. Default value is 0

use master
go
exec sp_msloginmappings 'sa', 0

Image2

use master
go
exec sp_msloginmappings 'sa', 1

Image1

In case you want to run the sp_msloginmappings across multiple SQL Instance using either Central management server or powershell. You can use the below script.

 create table #loginmappings(  
  LoginName  nvarchar(128) NULL,  
  DBName     nvarchar(128) NULL,  
  UserName   nvarchar(128) NULL,  
  AliasName  nvarchar(128) NULL  
 )  
 
 insert into #loginmappings
 EXEC master..sp_msloginmappings
 
 select * from #loginmappings
 
 drop table #loginmappings

How to restart an Interrupted Database Restore in SQL Server?

Steps to restart an Interrupted Database Restore in SQL Server

There may scenario when you are restoring VLDB database backup and the restore database command interrupted due network failure, power failure, SQL Server Service restart or cluster failover. In this kind of scenario you can easily start the interrupted restore from the point of failure using WITH RESTART command.

Before you start restoring the database using WITH RESTART option check database is in Restoring State not in any other state.

You can check the database state either from SSMS object explorer or by executing below T-SQL command

select name,state_desc from sys.databases

Once confirmed that database is in restoring state execute the backup command WITH RESTART clause

RESTORE DATABASE [AdventureWorks]
FROM DISK ='D:\Backup\AdventureWorks.bak'
WITH RESTART, STATS = 20
GO

Database Backup WITH CHECKSUM

Backup WITH CHECKSUM

When WITH CHECKSUM option is used while taking backup, the backup process will verify each page for checksums and torn page. In case bad page checksum found, the backup will stop. Using backup checksums may affect workload and backup throughput.

If you want to continue the backup, you have to write CONTINUE_AFTER_ERROR in backup WITH clause.
In case backup completes successfully it means no bad checksums.

Example

backup database sqldba to disk = 'c:\jspace\sqldba1.bak'
with checksum

You can also use the 3023 trace flag for the check sum option, when 3023 trace flag is on WITH CHECKSUM option will be applied all the backups by default.

DBCC TRACEON(3023)
GO                              

backup database sqldba to disk = 'c:\jspace\sqldbatraceflag.bak'

DBCC TRACEOFF(3023)
GO                              

Let’s check if the 3023 trace flag applied the page checksum to above backup command or not. Execute below query for it.

RESTORE HEADERONLY
FROM DISK = 'c:\jspace\sqldbatraceflag.bak'
GO  

HasBackupCheckSums value is 1, it means that WITH CHECKSUM option applied to backup using 3023 trace flag.
checksum

OpenDataSouce function – to query OLEDB Data Source

OPENDATASOURCE
OpenDataSouce function helps you to get ad hoc connection information as part of a four-part object name as an one time alternative of linked server. You don’t have to specify or create the linked server to query other data sources (i.e. MS Excel, MS Access, MSSQL Older version to newer version etc.) if you are querying it infrequently.

You can use OPENDATASOURCE for the OLEDB data sources those are accessed infrequently, for several time use linked server as it provides more functionality.

You can get more information about the arguments of OpenDataSource function on MSDN site.

To use the OPENDATASOURCE you have to enable the ad hoc distributed queries. You required to have execute permission to use OPENDATASOURCE fucntion.

Execute below query to enable it

EXEC sp_configure 'show advanced options', 1
RECONFIGURE WITH OVERRIDE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE WITH OVERRIDE
GO

Make sure Provider AllowInProcess and DynamiceParameters value is checked. For example lets enable it for SQLNCLI10 provider.

USE [master]
GO
EXEC master.dbo.sp_MSset_oledb_prop N'SQLNCLI10', N'AllowInProcess', 1
EXEC master.dbo.sp_MSset_oledb_prop N'SQLNCLI10', N'DynamicParameters', 1
GO

OpenDataSource Examples

-- SQL Server 2000/2005/2008
-- You can use SQLNCLI10 provider for SQL Server 2008 as well
SELECT
    * FROM
OPENDATASOURCE (
   'SQLNCLI', 
   'Data Source=SQLInstanceName;Catalog=DBName;User ID=SQLLogin;Password=Password;').DBName.SchemaName.TableName
   
   
SELECT *
FROM OPENROWSET('SQLNCLI',
   'DRIVER={SQL Server};SERVER=SQLInstanceName;UID=SQLLogin;PWD=Password',
   'select * from DBName..TableName')  
   

-- SQL Server 2012
SELECT
    * FROM
OPENDATASOURCE (
   'SQLNCLI11', 
   'Data Source=SQLInstanceName;Catalog=DBName;User ID=SQLLogin;Password=Password;').DBName.SchemaName.TableName
   
   
SELECT *
FROM OPENROWSET('SQLNCLI11',
   'DRIVER={SQL Server};SERVER=SQLInstanceName;UID=SQLLogin;PWD=Password',
   'select * from DBName..TableName')  

--Access DB
SELECT * FROM OPENDATASOURCE ('Microsoft.ACE.OLEDB.12.0', 
                              'Data Source=D:\MyDB\MyAccessDB.accdb')...TableName   

Common Errors
Error 1#
Msg 7403, Level 16, State 1, Line 1
The OLE DB provider “SQLNCLI11” has not been registered.
Solution: You will get above error if you have mentioned SQLNCLI11 while running OPENDATASOURCE query on SQL Server 2008 or lower version, it will work fine on SQL Server 2011. You can check list of registered provider by browsing Server Objects -> Linked Servers -> Provider in SSMS
Error 2#
Msg 15281, Level 16, State 1, Line 2
SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more information about enabling ‘Ad Hoc Distributed Queries’, see “Surface Area Configuration” in SQL Server Books Online.

Solution: Enable the ad hoc distributed queries by executing above SP_CONFIGURE query.