Posts

Showing posts from 2018

Restore Database backup from network

First you need to share that network folder with you user name, Then Remote to the server on which you want to restore database, Go to My computer and map network drive after mapping execute the below script by changing path, username and password according to your server  EXEC sp_configure 'show advanced options', 1 GO -- To update the currently configured value for advanced options. RECONFIGURE GO -- To enable the feature. EXEC sp_configure 'xp_cmdshell', 1 GO -- To update the currently configured value for this feature. RECONFIGURE GO EXEC xp_cmdshell 'NET USE Z: \\SystemName\FolderName PasswordofUser /USER:YourUserName@domain.com' if some drive is already mapped for the same user then you have to delete first one by below script. EXEC xp_cmdshell 'net use Y: /delete'

SQL Server Encryption Example

USE master; GO SELECT * FROM sys.symmetric_keys WHERE name = '##MS_ServiceMasterKey##'; GO -- Create database Key USE VAS_SeedScriptTest; GO CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'Password123'; GO -- Create self signed certificate USE VAS_SeedScriptTest; GO CREATE CERTIFICATE Certificate1 WITH SUBJECT = 'Protect Data'; GO -- Create symmetric Key USE VAS_SeedScriptTest; GO CREATE SYMMETRIC KEY SymmetricKey1  WITH ALGORITHM = AES_128  ENCRYPTION BY CERTIFICATE Certificate1; GO USE VAS_SeedScriptTest; GO ALTER TABLE CompsType ADD CompsTypeNameEncrypted varbinary(MAX) NULL GO -- Populating encrypted data into new column USE VAS_SeedScriptTest; GO -- Opens the symmetric key for use OPEN SYMMETRIC KEY SymmetricKey1 DECRYPTION BY CERTIFICATE Certificate1; GO UPDATE CompsType SET CompsTypeNameEncrypted = EncryptByKey (Key_GUID('SymmetricKey1'),CompsTypeName) FROM dbo.CompsType; GO -- Closes the symmetric...

How to Create and Configure a linked Server on SQL SERVER

Detailed article on how to create and configure a linked server in sql server https://www.sqlshack.com/how-to-create-and-configure-a-linked-server-in-sql-server-management-studio/

The database principal owns a schema in the database, and cannot be dropped. SQL SERVER Error: 15138

The database principal owns a schema in the database, and cannot be dropped. SQL SERVER Error: 15138 SELECT s.name FROM sys.schemas s WHERE s.principal_id = USER_ID('YourUserName'); ALTER AUTHORIZATION ON SCHEMA::DB_Owner TO dbo

Automated Process to Shrink database log files

-- Automated Process to Shrink database log files of all online databases whose size is Greater than 10000 DECLARE @loopStart INT = 1 ,     @LoopEnd INT; DECLARE @t TABLE     (       idx INT IDENTITY(1, 1) ,       DBName VARCHAR(MAX) ,       FileType VARCHAR(150) ,       Location VARCHAR(MAX) ,       LogicalFileName VARCHAR(MAX) ,       FileSize BIGINT     ); INSERT  INTO @t         ( DBName ,           FileType ,           Location ,           LogicalFileName ,           FileSize         )         SELECT  db.name ,                 type_desc ,                 physical_name ,       ...

To check the backup restore history on database server

SELECT [rs].[destination_database_name], [rs].[restore_date], [bs].[backup_start_date], [bs].[backup_finish_date], [bs].[database_name] as [source_database_name], [bmf].[physical_device_name] as [backup_file_used_for_restore] FROM msdb..restorehistory rs INNER JOIN msdb..backupset bs ON [rs].[backup_set_id] = [bs].[backup_set_id] INNER JOIN msdb..backupmediafamily bmf ON [bs].[media_set_id] = [bmf].[media_set_id] ORDER BY [rs].[restore_date] DESC

To get the Maximum on some field without where clasue

WITH cte AS ( SELECT home , MAX ( year ) AS year FROM Table1 GROUP BY home ) SELECT * FROM Table1 a INNER JOIN cte ON a . home = cte . home AND a . year = cte . year Example: WITH cte AS ( SELECT MasterLoanLinksID , MAX(Version) AS VersionNumber FROM @L GROUP BY MasterLoanLinksID ) INSERT INTO @LFinal ( LFLoanLinksId , LFMasterLoanLinksID ) SELECT a.LoanLinksId , a.MasterLoanLinksID FROM LoanBorrowerLinks a INNER JOIN cte ON a.MasterLoanLinksID = cte.MasterLoanLinksID AND a.VersionNumber = cte.VersionNumber WHERE a.IsActive = 1 AND IsPublished = 0;