Friday, June 14, 2024

Blocking script

 𝘞𝘐𝘛𝘏 𝘤𝘵𝘦𝘉𝘓 (𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥, 𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘵𝘩𝘦𝘴𝘦) 𝘈𝘚

(𝘚𝘌𝘓𝘌𝘊𝘛 𝘴.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥, 𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘵𝘩𝘦𝘴𝘦 = 𝘹.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘵𝘩𝘦𝘴𝘦 𝘍𝘙𝘖𝘔 𝘴𝘺𝘴.𝘥𝘮_𝘦𝘹𝘦𝘤_𝘴𝘦𝘴𝘴𝘪𝘰𝘯𝘴 𝘴
𝘊𝘙𝘖𝘚𝘚 𝘈𝘗𝘗𝘓𝘠 (𝘚𝘌𝘓𝘌𝘊𝘛 𝘪𝘴𝘯𝘶𝘭𝘭(𝘤𝘰𝘯𝘷𝘦𝘳𝘵(𝘷𝘢𝘳𝘤𝘩𝘢𝘳(6), 𝘦𝘳.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥),'') + ', ' 
       𝘍𝘙𝘖𝘔 𝘴𝘺𝘴.𝘥𝘮_𝘦𝘹𝘦𝘤_𝘳𝘦𝘲𝘶𝘦𝘴𝘵𝘴 𝘢𝘴 𝘦𝘳
       𝘞𝘏𝘌𝘙𝘌 𝘦𝘳.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥 = 𝘪𝘴𝘯𝘶𝘭𝘭(𝘴.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥 ,0)
       𝘈𝘕𝘋 𝘦𝘳.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥 <> 0
       𝘍𝘖𝘙 𝘟𝘔𝘓 𝘗𝘈𝘛𝘏('') ) 𝘈𝘚 𝘹 (𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘵𝘩𝘦𝘴𝘦)
)
𝘚𝘌𝘓𝘌𝘊𝘛 𝘴.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥, 𝘣𝘭𝘰𝘤𝘬𝘦𝘥_𝘣𝘺 = 𝘳.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥, 𝘣𝘭.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘵𝘩𝘦𝘴𝘦
, 𝘣𝘢𝘵𝘤𝘩_𝘵𝘦𝘹𝘵 = 𝘵.𝘵𝘦𝘹𝘵, 𝘪𝘯𝘱𝘶𝘵_𝘣𝘶𝘧𝘧𝘦𝘳 = 𝘪𝘣.𝘦𝘷𝘦𝘯𝘵_𝘪𝘯𝘧𝘰, *
𝘍𝘙𝘖𝘔 𝘴𝘺𝘴.𝘥𝘮_𝘦𝘹𝘦𝘤_𝘴𝘦𝘴𝘴𝘪𝘰𝘯𝘴 𝘴
𝘓𝘌𝘍𝘛 𝘖𝘜𝘛𝘌𝘙 𝘑𝘖𝘐𝘕 𝘴𝘺𝘴.𝘥𝘮_𝘦𝘹𝘦𝘤_𝘳𝘦𝘲𝘶𝘦𝘴𝘵𝘴 𝘳 𝘰𝘯 𝘳.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥 = 𝘴.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥
𝘐𝘕𝘕𝘌𝘙 𝘑𝘖𝘐𝘕 𝘤𝘵𝘦𝘉𝘓 𝘢𝘴 𝘣𝘭 𝘰𝘯 𝘴.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥 = 𝘣𝘭.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥
𝘖𝘜𝘛𝘌𝘙 𝘈𝘗𝘗𝘓𝘠 𝘴𝘺𝘴.𝘥𝘮_𝘦𝘹𝘦𝘤_𝘴𝘲𝘭_𝘵𝘦𝘹𝘵 (𝘳.𝘴𝘲𝘭_𝘩𝘢𝘯𝘥𝘭𝘦) 𝘵
𝘖𝘜𝘛𝘌𝘙 𝘈𝘗𝘗𝘓𝘠 𝘴𝘺𝘴.𝘥𝘮_𝘦𝘹𝘦𝘤_𝘪𝘯𝘱𝘶𝘵_𝘣𝘶𝘧𝘧𝘦𝘳(𝘴.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥, 𝘕𝘜𝘓𝘓) 𝘈𝘚 𝘪𝘣
𝘞𝘏𝘌𝘙𝘌 𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘵𝘩𝘦𝘴𝘦 𝘪𝘴 𝘯𝘰𝘵 𝘯𝘶𝘭𝘭 𝘰𝘳 𝘳.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥 > 0
𝘖𝘙𝘋𝘌𝘙 𝘉𝘠 𝘭𝘦𝘯(𝘣𝘭.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘵𝘩𝘦𝘴𝘦) 𝘥𝘦𝘴𝘤, 𝘳.𝘣𝘭𝘰𝘤𝘬𝘪𝘯𝘨_𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥 𝘥𝘦𝘴𝘤, 𝘳.𝘴𝘦𝘴𝘴𝘪𝘰𝘯_𝘪𝘥;

Friday, May 31, 2024

Azure QA

 1. What is the best way to migrate the On prim database to Azure cloud.

2.     Do you have experience on deploying the iaas, paas services.
3.     Have you knowledge on Azure storage options.
4.     Can we replicate the databases into azure cloud.
5.     Do you have experience on log shipping, mirroring and replication on hybrid cloud method.
6.     How can we move the 2TB database from on prim to azure cloud.
7.     Can we migrate windows and SQL logins to azure SQL VM.
8.     Cluster rebalancing.
9.     How can we move the TDE enabled database into cloud.
10.  They are asking need some experience and knowledge about azure administration.

Wednesday, May 29, 2024

Common SQL Server Intereview Q & A

 What is query optimizer fix setting in database scoped configuration. 
This was introduced in SQL 2016 version , this actually will take care of the query optimizer improvements while the hotfix or CU is updated when the setting is ON depending  on the db compatability option , when the db compatability is 130 (sql 2016) then it doesn't matter  if the setting is ON or OFF.  The trace flag 4199 needs to enabled to get the advantage of this configuration after hotfix is updated. 
WHAT IS OPTIMIZE FOR ADHOC WORKLOAD
When this setting is set to 1 the plan cache stores the stub plan for the query in the buffer cache instead the entire plan , when the same query is executed it  reuses the stub plan . 
hashtagTransparent Data Encryption (TDE):
Encrypts the entire database at rest.
Data is automatically encrypted and decrypted as it is read from and written to disk.
Provides encryption at the file level, securing backups and data files.
Key management is simplified, as TDE handles key encryption with a database encryption key (DEK) and a certificate stored in the master database.
Column-level Encryption:
Allows encryption of specific columns within a table, providing granular control over data security.
Different columns within the same table can have different encryption keys.
Offers protection for sensitive data while allowing non-sensitive data to remain unencrypted for easier querying and processing.
Requires explicit encryption and decryption functions to access encrypted data.

Always Encrypted:
Enables client-side encryption of sensitive data before it is sent to the database.
Data remains encrypted during transmission and while stored in the database, ensuring end-to-end encryption.
Encryption keys are managed outside of SQL Server, typically by client applications, enhancing security.
Supports deterministic and randomized encryption schemes, allowing for different levels of security and search capabilities.

Backup Encryption:
Provides the ability to encrypt database backups to protect sensitive data during storage and transmission.
Backup encryption can be applied at the time of backup creation using a certificate or asymmetric key.
Protects backups stored locally or in the cloud, preventing unauthorized access to data in case of backup theft or interception.
Decryption is required to restore encrypted backups, ensuring data integrity and security.

Transport Layer Security (TLS):
Encrypts data transmitted between SQL Server and client applications.
Provides secure communication over the network, preventing eavesdropping and data interception.
Supports various TLS protocols and cipher suites for encryption and authentication.
Configuration options in SQL Server allow for enabling and enforcing TLS encryption to protect data in transit.


COLUMN LEVEL ENCRYPTION
Lets understand with and example for column-level encryption:

-- Creating a master key
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'StrongPassword123';

-- Creating a certificate
CREATE CERTIFICATE MyCertificate WITH SUBJECT = 'My Certificate';

-- Creating a symmetric key
CREATE SYMMETRIC KEY MySymmetricKey WITH ALGORITHM = AES_256 ENCRYPTION BY CERTIFICATE MyCertificate;

-- Encrypting data in a table
OPEN SYMMETRIC KEY MySymmetricKey DECRYPTION BY CERTIFICATE MyCertificate;
UPDATE dbo.YourTable
SET EncryptedColumn = EncryptByKey(Key_GUID('MySymmetricKey'), 'SensitiveData');

-- Decrypting data
SELECT CONVERT(varchar, DecryptByKey(EncryptedColumn)) AS DecryptedData
FROM dbo.YourTable;
CLOSE SYMMETRIC KEY MySymmetricKey;


Connect SQL SERVER instance when there is no logins working.
Sometimes we may miss password or logins not working to connect sql server in this case follow the below steps to connect the instance. 
1. Stop the instance.
2. Open CMD in Adminstator mode- Root to SQL Server bin folder  with -m which will start the sql server in single user mode.
3. Open another CMD in adminstrator mode- Run the script to set the new login or create login .
4. Exit  both CMD and start the instance.   

Why the query is running slow in application and fast in SSMS.
This can be due to below factors.
1. The parameters or variables are not same between Application and SSMS.
2. Check execution time on server.
execution time on server can be checked by
SET STATISTICS TIME ON <YourQuery> SET STATISTICS TIME OFF
The time  may vary due to network speed between the servers or hardware resource different. 
3. SET options
Some options like artithabort ,nulls,force_plan settings may different in server and application.
For eg. by default arithabort is ON in ssms and OFF in application, this may affect the performance.  

How to migrate SSRS from one server to another.
https://datasteve.com/2024/04/03/migrating-ssrs-from-one-server-to-another.

Difference between Full and tail log backup ?


Is it possible to take backup when db is corrupted ?

what parameter we need to use whenever we are taking the backup  and db is  corrupted ?


Scenario: You have an on-premises Microsoft SQL Server (SQL1) hosting five databases. The goal is to migrate these databases to an Azure SQL Managed Instance with minimal downtime and no data loss. What tool should you use?

Options:
A) Always On Availability Groups
B) Backup and Restore
C) Log Shipping
D) Database Migration Assistant

Answer: B) Backup and Restore
Explanation:
While the Database Migration Assistant (DMA) is valuable for assessment and conversion tasks, it doesn’t directly support migrations to Azure SQL Managed Instance. The recommended approach for database migration is the Azure SQL Migration extension for Azure Data Studio, which supports both online and offline migrations. However, this option isn’t listed here. Log shipping isn’t viable for Azure SQL Managed Instance as it only supports full backups for restoration.

AZURE DB HELP

 Script to check the db space of all databases on azure db


Courtesy : https://www.sqlservercentral.com/articles/check-azure-sql-db-space-used

SELECT
    [database_name],
    start_time AS 'LastCollectionTime',
    storage_in_megabytes AS 'CurrentSize(MBs)',
    allocated_storage_in_megabytes AS 'AllocatedStorage(MBs)'
  FROM (
            SELECT
                ROW_NUMBER() OVER(PARTITION BY [database_name] ORDER BY start_time DESC) AS rn,
                [database_name],
                start_time,
                storage_in_megabytes,
                allocated_storage_in_megabytes
            FROM sys.resource_stats
        ) rs
WHERE rn = 1