February 22, 2016

MS SQL : Use of GUID function

If you want to generate a new Guid (Unique Identifier) in SQL server the you can simply use the NEWID() function.

Example

SELECT NEWID( )
GO
-- This will return a new random uniqueidentifier e.g.
E75B92A3-3299-4407-A913-C5CA196B3CAB

To select this Guid in in a variable

--To Assign uniqueidentifier in a variable
DECLARE @EmployeeID uniqueidentifier
SET @EmployeeID = NEWID( )

You can directly use this with INSERT statement to insert new row in table.

-- Inserting data in Employees table.
INSERT INTO Employees
(EmployeeID, Name, Phone)
VALUES
(NEWID( ), 'Shyam Sundar', '123-4567')

MS SQL : Function to Convert INR Value

Function (numberVar Amount)
numbervar RmVal:=0;
numbervar Amt:=0;
numbervar pAmt:=0;
stringvar InWords :="";
stringvar ckAmt:="";
Amt := ABS(Amount);
ckAmt := CSTR(ABS(Amount));

MS SQL Server Recovery Models

One of the first things that needs to be set in order to create the correct backups is to set the proper recovery model for each database.  The recovery model basically tells SQL Server what data to keep in the transaction log file and for how long.  
Based on the recovery model that is selected, this will also determine what types of backups you can perform and also what types of database restores can be performed.

The 3 types of recovery models that you can choose from are:

  • Full
  • Simple
  • Bulk-Logged