Wednesday 30 July 2014

Prevent missing trigger while database migration

Prevent missing trigger while database migration

    While migrating scripts from one server to another server or from one environment to other environment ( from QA to Staging) the main challenge you will face is if new column is added at the middle of the table not at end. Then while migrating you have to dump the data in any temp table and then drop and recreate the table with adding new column, and bring back the data in this table.
  But here major issue is you will miss the trigger that you have on your table. So lets take one example , I have to migrate the database , in this migration script there are many drop and create table steps are there. I should have to protect the trigger , not to miss.  
    To Prevent trigger simply , before running migration script , I have dump all trigger script in one table and then after run migration script I pull the script from dump table and recreate all.

Below is the script that I used to pull all trigger script.


--Before executing migration script

SELECT object_name(t.object_id) AS TriggerNAme

,OBJECT_NAME(parent_id) parentObjectNAme

,sm.DEFINITION AS script

INTO mytriggertable

FROM sys.triggers t

INNER JOIN sys.sql_modules sm ON sm.object_id = t.object_id

WHERE object_name(t.object_id) IS NOT NULL

GO

--After migration script
SELECT script + CHAR(13) + 'GO'
FROM mytriggertable
WHERE triggername NOT IN (
SELECT object_name(t.object_id) AS TriggerNAme
FROM sys.triggers t
INNER JOIN sys.sql_modules sm ON sm.object_id = t.object_id
WHERE object_name(t.object_id) IS NOT NULL
)



Saturday 26 July 2014

Comparing Case Sensitive Data in SQL server

Comparing Case Sensitive  Data in SQL server 


How to compare case sensitive data in sql server? Lets take one example if you have a password column where you are storing password, then while retrieving result should come as per case compression. If data is "AmiT#123" then it should not equal to "amit#123" because here "A" has different case.

 To compare case sensitive data we can use the COLLATE.

CREATE TABLE bn(name VARCHAR(100))
GO

 INSERT INTO bn (name ) VALUES ('Vikas'),('Choice'),('kumar')

 SELECT * FROM bn WHERE name collate latin1_general_CS_AS ='Vikas'
--

Data Mesh

  Data Mesh is a relatively new approach to managing and organizing data within organizations, especially large enterprises. It advocates fo...