Monday, September 8, 2014

How to create Database and Migration in SQL Azure

Hi Friends,

As we have seen the welcome note on “SQL Azure” here. This is just a continuation of that post.
Here we’ll see how to create a Server, Database as well as how to authenticate the user to access the Database in Azure.

1.       Connect to windows.azure.com.
2.       Click Subscription >> Then “Create”.
3.       Select the location (Region where you wanted to deploy your Azure Database).
4.       Create Admin Login ID & set the password.
5.       Check “Allow other windows Azure services to access this service”.
Add the list of IP’s which can able to access the server.
6.       Click “Finish”.

Once it is done. A server name will be created, which one can use to connect and create a Database.

How to create Azure Database:

1.       Once the server is configured, connect to the server. Here we can able to see only “Master” Database (Master Database is not chargeable).
2.       Under Database >> Create >> Database_Name >> Select “Edition”.
3.       Database is created.

How to migrate Other Databases to Azure:

1.       If you are planning to migrate any other RDBMS to Azure, we need to first migrate it to MS SQL 2008 then only we can migrate to Azure.
2.       Once we have migrated to generate script of Database using Azure Tool (Reason all the features are not supported if we generate the script from SQL Server).
3.       SQL Azure >> Migration Wizard >> Generate Script.
4.       Copy the script and run it to create Azure Database.
5.       Populate data using either SSIS or SQL Azure migration wizard

Max size of DB is limited to 50 GB. We can connect to windows azure portal to view the bill.

Note: Above points are my understanding from the session, there might be some flaws. Looking forward for your note to correct me if I’m wrong anywhere.   

Thanks for reading the article. Stay Tuned.


Friday, September 5, 2014

Introduction on SQL Azure

Hi Friends,

Last week I've attended a session on “SQL Azure”. So thought of sharing the key points on the blog for our all reference. Till before attending the session, it was just a word for me “AZURE” with zero knowledge on this technology (or rather my understanding was just limited to “it is something related to Cloud”).
Following are some points I’d like to share which I learned from the session:

i.    We can consider SQL Serve Azure (SSA) just like a SQL Server (SS) Version (Cloud version). 
     It is the light version of SS.
ii.   SSA is Scalable, HA, RDBMS and Secure.
iii. SSA runs only on “Windows Azure Platform”.
iv.  One can connect the server through Azure Portal as well as SSMS.
v.    SSA has two Editions. Web Edition and Business Edition with 5 GB and 50 GB DB limit     respectively.
vi.   Data can be migrated from any other RDBMS such as Oracle, MySQL, SQL Server, etc...
vii. Pricing is based on the Usage (DB Size). Large the DB size is more your billing would be.

Difference between SQL Server (SS) and SQL Server Azure (SSA):

1.  Only SQL Authentication is possible in SSA; whereas SQL as well as Windows Authentication is also possible in SS.
2.  While making login Azure does not allow login name like sa, admin, guest, administrator and root. There is no as such restriction in SS.
3.  No CPU, CAL licensing is involved in SSA like SS; rather billing is done based on consumption (More the DB Size more the bill will be).
4.  Since SSA is a light version many features are not present in SSA compared to SS.
5.  SSA table must have Cluster Index whereas it’s not compulsory in SS.
6.  Only Master DB is present in SSA on contrary Master, Model, MSDB & Tempdb is present in SS.
7.  Max size of DB in SSA is 50 GB; whereas in SS it’s in TB’s.
8.  Transaction should stay in single DB; whereas in SS it can run adhoc queries.
9.  As it is in Azure only TCP\IP protocol is supported; where as in SS it supports many protocols.
10. In Azure, Application cannot go down unexpectedly.

Guys this is just a welcome note on SQL Azure.  There are lots of things to learn in SSA. Will keep on posting as and when I‘ll have the content along with the necessary snapshots.

Note: Above points are my understanding from the session, there might be some flaws. Looking forward for your note to correct me if I’m wrong anywhere.   

Thanks for reading the article. Stay Tuned.

Friday, August 29, 2014

Database Backup Script...

!!!!!!! - Wish you all a very Happy Ganesh Chaturthi - !!!!!!!

(Couple of new things happened in last few weeks. Started working with new some onshore Clients where I’ll get exposed to some new technology to learn along with SQL Server. Also my favorite Series 24 Season 5, where Jack Bauer is back to find the reason behind assassination of former President David Palmer and today is Ganesh Chaturthi as well.)

Requirement: One of my client asked to us to design a script which should take backup of all Databases including system DB, Verify those Backups from corruption & also should clean up old backups depending upon retention period.

Solution: Below you can find the script which satisfies the above requirement. This code is compatible from 2005 on wards.

Following is the detailed description of the code:

Parameters:

*@Directory => Mention the Backup location to take. It is compulsory.
*@No_of_hours => Specify how much older backup you want to delete. It is again compulsory.
@BackupType => Type of Backup Full (F), Differential (D) and Log (L) Backup. By default it will take Full Backup.
 @Verify => It will verify the Backup against corruption. By default it will not verify the backup [Y => Yes; N => No].

Example how the code will work:

E.g.  Suppose you pass SP_Backup ‘D:\Backup\’, 25, ‘D’,’Y’

1. Identify the list of Databases which Differential Backup needs to be done.
2. Databases backup will be done on D:\Backup\*.diff drive.
3. It will simultaneously verify the Backups; as the parameter passed is Y.
4.  Finally; it will delete the 25 Hours Backup Files from the same directory.


USE [master]
GO
/****** Object:  StoredProcedure [dbo].[SP_Backup]    Script Date: 8/28/2014 12:53:04 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE Procedure [dbo].[SP_Backup_ALL] --SP_Backup_ALL 'C:\Backup\',1

@Directory NVARCHAR(256), -- Mention Path to take the Backup [E.g. C:\Backup\]
@No_of_hours INT, -- Specify no of hours to clean old backups [E.g. 5 means it will delete 5 Hours old Backup]
@BackupTYpe NVARCHAR(10) ='F', -- Specify Backup Type; By default it will take "Full" Backup [F => Full; D => Differential; L => Log]
@Verify NVARCHAR(256)='N' -- Specify Verify Type; By default it will not verify the backup [Y => Yes; N => No]
As 
Begin

Declare @Name NVARCHAR(100) -- Database Name  
Declare @FileDate NVARCHAR(100) -- Used for file name
Declare @FileName NVARCHAR(256) -- Filename for Full & Diff backup  
Declare @FileName_Log NVARCHAR(256) -- Filename for Log backup 
Declare @DeleteDate NVARCHAR(100)
Declare @DeleteDateTime DATETIME

Set @FileDate = CONVERT(VARCHAR(20),GETDATE(),112) + REPLACE(CONVERT(VARCHAR(20),GETDATE(),108),':','')
Set @DeleteDateTime = DateAdd(hh, -@No_of_hours, GetDate())
Set @DeleteDate = (Select Replace(Convert(nvarchar, @DeleteDateTime, 111), '/', '-') + 'T' + Convert(nvarchar, @DeleteDateTime, 108))

/***************Start Full Backup*********************/

IF @BackupTYpe='F'  
  BEGIN
  DECLARE db_cursor CURSOR FOR  
SELECT name
FROM master.sys.databases 
WHERE state_desc='ONLINE' and name NOT IN ('tempdb')   -- Exclude these databases (Specify the list of databases which you want to exclude from backup)

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS = 0   
BEGIN   
SET @fileName = @Directory + @name + '_' + @fileDate + '.bak'  
BACKUP DATABASE @name TO DISK = @fileName with stats=25  
  
/***************Verify the backup*********************/
IF @Verify = 'Y'
BEGIN
Restore verifyonly from disk = @fileName
END  
FETCH NEXT FROM db_cursor INTO @name   
END  
 
CLOSE db_cursor   
DEALLOCATE db_cursor
END
ELSE

/***************Start Differential Backup*********************/

IF @BackupTYpe='D'  
  BEGIN

DECLARE db_cursor CURSOR FOR  

SELECT a.name
FROM master.sys.databases a
LEFT OUTER JOIN msdb..backupset b ON a.name = b.database_name 
where b.type='D' and a.name <> 'master'and a.state_desc='ONLINE'
GROUP BY a.name, b.type 
ORDER BY a.name, b.type

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS = 0   
BEGIN   
SET @fileName = @Directory + @name + '_' + @fileDate + '.diff'  
--print @fileName
BACKUP DATABASE @name TO DISK = @fileName with Differential,stats=25 
  
/***************Verify the backup*********************/
IF @Verify = 'Y'
BEGIN
Restore verifyonly from disk = @fileName
END  
FETCH NEXT FROM db_cursor INTO @name   
END  
 
CLOSE db_cursor   
DEALLOCATE db_cursor
END

/***************Start Log Backup*********************/
ELSE
IF @BackupTYpe='L'  
  BEGIN

DECLARE db_cursor CURSOR FOR  

/******* DB's with Simple Recovery Model  & DB's with no Full Backup will be skip from Log Backup *******/

SELECT a.name
FROM master.sys.databases a
LEFT OUTER JOIN msdb..backupset b ON a.name = b.database_name 
where a.recovery_model_desc <> 'SIMPLE' and b.type='D'and a.state_desc='ONLINE'
GROUP BY a.name, b.type 
ORDER BY a.name, b.type

OPEN db_cursor   
FETCH NEXT FROM db_cursor INTO @name   

WHILE @@FETCH_STATUS = 0   
BEGIN   

SET @fileName_Log = @Directory + @name + '_' + @fileDate + '.trn' 
--print @fileName_Log
BACKUP log @name TO DISK = @fileName_Log  with stats=25
  
/***************Verify the backup*********************/
IF @Verify = 'Y'
BEGIN
Restore verifyonly from disk = @fileName_Log
END  
FETCH NEXT FROM db_cursor INTO @name   
END  
 
CLOSE db_cursor   
DEALLOCATE db_cursor
END

/*********** Start Deletion of OLD Backups *****************/

IF @BackupTYpe = 'F'
BEGIN 
EXECUTE master.dbo.xp_delete_file 0,@Directory,N'bak',@DeleteDate,1 -- It will delete the "Full" Backup
END
ELSE 

IF @BackupTYpe = 'D'
BEGIN 
EXECUTE master.dbo.xp_delete_file 0,@Directory,N'diff',@DeleteDate,1 -- It will delete the "Differential" Backup
END
ELSE 
BEGIN 
EXECUTE master.dbo.xp_delete_file 0,@Directory,N'trn',@DeleteDate,1 -- It will delete the "Log" Backup
END

END
GO


Let me know if any concerns.

Ganpati Bappa Moriya!!!!!


Saturday, August 16, 2014

Farewell Mail....

Dear Team,

As most of you might know that today is my last working day. It’s been total 3.4 years in this organization now. Before leaving I would like to grab your couple of minutes to share my feelings.

I would like to take the opportunity to thanks all my colleagues who have really made the journey so memorable.
Firstly, this could never been possible without Prashant Sir & Lakshmi Mam (Interviewer) who has given break to my career.

Time spend with the batch will always be most memorable and ever green forever. Special thanks to Liz Mam for assigning me such a wonderful sites after completion of training.

It was very good learning experience with Abhijit K and Abhijit S under whom I spend most of the time. Also personally I would like to thanks Kalim K, Rohit I, Kunal A, Ritesh M, Sushil D, Jabeen (Ma)m, Chandan R, Amod B, Anirudh K, Vineet N, Rahul J, Subodh, Nilkanth D, Kishore G, Younis D, Rohit N, Mithun P, Sandeep J, Akilesh H, Muthu K, Deepali G and Palksha for helping me to grow.

As I’m very much emotionally attached to our company, so I would never say good bye. We might definitely meet at some point in life again.

Dear SQL Team,

Feel free to get in touch with me for any technical assistance. I would be glad to be of any help.

Here are my personal contact details:

Call me on: 08452910970
Drop me on ID: vikasbsahu@gmail.com

--
Thanks & Regards,
Vikas Sahu