Thursday, March 13, 2014

PGP Encryption/Decryption with SSIS Task

Microsoft SQL Server Integration Services provided a new component PGP task in Sql server 2008 R2.
This feature is very helpfully for Developers/DBA'S to encrypt/decrypt files with automation.


SSIS Extensions - SFTP Task, PGP Task, Zip Task




Sunday, March 2, 2014

Database Collation

It is possible to create database with different collation on the same instance of sql server with different collation.

Collation can be set at SQL Server instance Level, Database Level as well as Table Column Level. 


Let’s walk through with some examples.

I have Sqlserver 2008 R2 is running on my machine and which has default 



---Lets Create Databases with Canadian English and American English
--- check the data types by querying sys.types  table.
CREATE DATABASE EnglishCanada
COLLATE Latin1_General_CI_AS
GO
USE EnglishCanada
GO
SELECT *
FROM sys.types
GO
-- Create Case In-Sensitive Database
CREATE DATABASE EnglishUSA
COLLATE SQL_Latin1_General_CP1_CI_AS
GO
USE EnglishUSA
GO
SELECT *
FROM sys.types

GO





In the above diagram collation name Colum clearly showing Canada English and USA English. So it is possible to have different type of Collation database in the same instance.

Collation is code page to sort and compare the characters in the database.
Let’s check for Canada and USA English.



SELECT COLLATIONPROPERTY('Latin1_General_CI_AS','CODEPAGE')
GO
SELECT COLLATIONPROPERTY('SQL_Latin1_General_CP1_CI_AS','CODEPAGE')
Both are using same code page 1252 for sorting and comparing characters.
Now let’s compare characters For both Canada and USA English.
Run the following scripts and result for both says both are equal.

-- COMPARISON
USE EnglishUSA
GO
SELECT CASE
WHEN 'A' = 'a' THEN 'YES'
ELSE 'NO'
END AS USAENGLISH



USE EnglishCanada
GO
SELECT CASE
WHEN 'A' = 'a' THEN 'YES'
ELSE 'NO'
END AS CANADAENGLISH


Let’s do sorting test for both languages.
Run the following scripts and result for both has same value.

-- SORTING
USE EnglishUSA
GO
SELECT 'Aa' as example,1
UNION ALL
SELECT 'AA' as example,2
order by example desc


USE EnglishCanada
GO
SELECT 'Aa' as example,1
UNION ALL
SELECT 'AA' as example,2
order by example desc


With above test there is no absolute difference between Canada and usa English.
It’s clearly shown that you can have databases with different collation in a instance that is with different collation.

Collation can be set at Database level, Table level and column level.


Some useful commands.
--- Find the Collation at server level
select SERVERPROPERTY('Collation')
--- Find the collation at database level

GO
select DATABASEPROPERTYEX('testhari','Collation')

GO
--- Change the database collation.
USE testhari
GO
ALTER DATABASE testhari COLLATE Latin1_General_CI_AS
GO




Saturday, January 25, 2014

Find Blocking Information in Sql Server

--To Get wait stats information
SELECT * 
FROM sys.dm_os_wait_stats
GO
/*
runnable
running
suspended

sp_who2
to get the sql text 
dbcc inputbuffer(58)

 1 2 3 4
 */
 --blocking finding  query

SELECT  blocking.session_id AS blocking_session_id ,
blocked.session_id AS blocked_session_id ,
waitstats.wait_type AS blocking_resource ,
waitstats.wait_duration_ms ,
waitstats.resource_description ,
blocked_cache.text AS blocked_text ,
blocking_cache.text AS blocking_text
FROM    sys.dm_exec_connections AS blocking
INNER JOIN sys.dm_exec_requests blocked
ON blocking.session_id = blocked.blocking_session_id
CROSS APPLY sys.dm_exec_sql_text(blocked.sql_handle)
                              blocked_cache
CROSS APPLY sys.dm_exec_sql_text(blocking.most_recent_sql_handle)
                              blocking_cache
INNER JOIN sys.dm_os_waiting_tasks waitstats
ON waitstats.session_id = blocked.session_id
-- out put from blocking information query                   
--ridlock fileid=1 pageid=171 dbid=14 id=lock80126980 mode=X associatedObjectId=72057594039042048
-- to get the database name and object name 
-- find the datbase name using dbid
select * from  sys.databases
---ridlock fileid=1 pageid=171 dbid=14 id=lock82366580 mode=X associatedObjectId=72057594039042048

-- find the object id that causing blocking
use hari2
go
SELECT OBJECT_SCHEMA_NAME(object_id), OBJECT_NAME(object_id) 
FROM sys.partitions 
WHERE hobt_id IN (72057594039042048);

-- Getting blocking session id with username
SELECT  blocking.session_id AS blocking_session_id ,
blocked.session_id AS blocked_session_id ,
waitstats.wait_type AS blocking_resource ,
waitstats.wait_duration_ms ,
waitstats.resource_description ,
blocked_cache.text AS blocked_text ,
blocking_cache.text AS blocking_text,
sysp.loginame AS blockingLoginname,
sysp1.loginame as BlockedLoigname
FROM    sys.dm_exec_connections AS blocking
inner join master..sysprocesses sysp
on blocking.session_id = sysp.spid
INNER JOIN sys.dm_exec_requests blocked
inner join master..sysprocesses sysp1
on blocked.session_id = sysp1.spid
ON blocking.session_id = blocked.blocking_session_id
CROSS APPLY sys.dm_exec_sql_text(blocked.sql_handle)
                              blocked_cache
CROSS APPLY sys.dm_exec_sql_text(blocking.most_recent_sql_handle)
                              blocking_cache
INNER JOIN sys.dm_os_waiting_tasks waitstats
ON waitstats.session_id = blocked.session_id

-- Root blocking information(Root id)
select distinct blocked as [Blocking Process] from sys.sysprocesses
where  blocked != 0 and blocked not in (select spid from sys.sysprocesses where blocked != 0)