December 18, 2012

SQL: Full and Differential back up

/*

Description: This procedure take a first full backup
             and after that differentail or partial backup each day.Path location is fixed in @filePath.

*/

alter proc [dbo].UP_Database_bkup
      @dbName varchar(100),
      @FilePath varchar(250)
as
set nocount on
Declare  @bkPath varchar(100), @FileName varchar(50)
Declare  @name varchar(40) , @fileExist int
-- set @FilePath ='E:\DatabaseBKup\DailyBKUP\'
begin try
      SELECT @bkPath = isnull(BkupPath,'') FROM ATSR_MASTER.dbo.T_GM_DatabaseDetail where DbName = @dbName
      /* Check File is exist or not is exist return 1 else 0  */
      Exec xp_fileexist @bkPath, @fileExist output
      if @fileExist = 1
       begin
            set @name = 'Differential BKUP - ' + CONVERT(varchar(40), CONVERT(datetime, GETDATE()),106)
            /* Below line take the partial or differential backup */
            BACKUP DATABASE @dbName
            TO  DISK = @bkPath
            WITH  DIFFERENTIAL
            , NOFORMAT, NOINIT
            , NAME = @name
            , SKIP, NOREWIND, NOUNLOAD,  STATS = 10
       end 
      else
       begin
           
            set @FileName = @FilePath + @DbName + '.bak'
            set @name ='Full Backup' + CONVERT(varchar(40), CONVERT(datetime, GETDATE()),106)
            /* Below line take full backup */
            -- BACKUP DATABASE @DbName    To disk = @FileName
            BACKUP DATABASE @DbName To disk = @FileName
            WITH NOFORMAT, NOINIT
                  ,  NAME = @name
                  , SKIP, NOREWIND, NOUNLOAD,  STATS = 10
           
       end
 end try
 begin catch
 
 end catch

October 3, 2012

NULLIF function


The NULLIF function compares the first parameter (variableB) with second parameter (0). If they are same, it returns a NULL .

Example :

Declare @variableA int 
Declare @variableB int
select @variableA=50, @variableB=0


select @variableA/NULLIF (@variableB,0)

Output:
NULL

February 28, 2012

Save multiple record in sql server via XML



DECLARE @index int
DECLARE @xmlString varchar(8000)
SET @xmlString =
     '<Persons>
<Person id="1">
<Name> name1 </Name>
<PhoneNo> 022-2366854 </PhoneNo>
</Person>
<Person id="2">
<Name> name2 </Name>
<PhoneNo> 011-33366854 </PhoneNo>
</Person>'

EXEC sp_xml_preparedocument @index OUTPUT, @xmlString

SELECT * FROM OPENXML(@index, 'Persons/Person')
WITH (id Varchar(10)  , Name1 varchar(100) 'Name' , PhoneNo1 Varchar(50) 'PhoneNo')

EXEC sp_xml_removedocument @index


EXAMPLE 2
=====================



DECLARE @xmlDoc NVARCHAR(200)
DECLARE @handle INT
SET @xmlDoc = N'
  <authors>
    <au_id>
<au_id1>409</au_id1>
<au_id2>56</au_id2>
<au_id3>7008</au_id3>
</au_id>
    <au_lname>Bennet</au_lname>
    <au_fname>Abraham</au_fname>
  </authors>
'
EXEC sp_xml_preparedocument @handle OUTPUT, @xmlDoc
SELECT * FROM OPENXML (@handle, '/authors',3) WITH
  (au_id3 VARCHAR(20) 'au_id/au_id3' ,
   au_lname NVARCHAR(20) ,
   au_fname NVARCHAR(20)
  )
EXEC sp_xml_removedocument @handle


OPUTPUT
------------------------------------
au_id3 | au_lname | au_fname
------------------------------------
7008   | Bennet     | Abraham