Showing posts with label backup database. Show all posts
Showing posts with label backup database. Show all posts

Tuesday, May 16, 2017

Backup SQL Server DB Objects (user defined) as script

using below tool, you can backup a database, with all user defined objects, as a script and save it to the file.... without any third party/extra tool.

NOTE: download the ZIP file "DBScriptGenerator.zip"

After download, extract the zip and place it anywhere you want.
in my case, location is -- "C:\DBScriptGenerator\"

this zip file contains an EXE file named "DBScriptGenerator.exe", along with the other DLL files.

now we have to call this executable from anywhere we can. of course, we need to pass all the parameters.
in my case, I am calling from CMD...

Format :
DBScriptGenerator.exe "DatabaseServerName" "DatabaseName" "DatabaseUser" "DatabasePassword" "FullPathToSaveWithFileName"

Example:
DBScriptGenerator.exe "PRODSV" "EMPMAS" "UADMIN1" "u@Dwin!" "C:\DBScriptGenerator\EMPMAS.sql"

NOTE:::
for Advanced users only ...

There is a DLL file, DBScriptGenerator.dll, available to use programmatically. we can use this DLL from C# classes and/or SSIS scripts also.

Object List: Table, Procedure, Function, View, Trigger, Schema, assembly .. all user objects


Thursday, February 4, 2016

SQL Script/tSql to backup SQL Server Database

This script is a parameterized stored procedure, [dbo].[DB_BACKUP_TO_DISK], to backup SQL Server Database (.BAK). Everything is to call this procedure with all parameters. See below table to understand parameters.

Parameter Name
Type
Description
@DBName 
VARCHAR(500)
Put Database name
@SaveAs
VARCHAR(500)
Put output name, is a bak file name. i.e. TEST_BACKUP.BAK
@SaveTo
VARCHAR(500)
Put full path where BAK file will be created

See below how to call this procedure…

EXEC  [dbo].[DB_BACKUP_TO_DISK]
            @DBName = N'TEST_DATABASE',
            @SaveAs = N'TEST_BACKUP',
            @SaveTo = N'D:\TEST\'

Please find the below script of the described stored procedure …

CREATE PROCEDURE [dbo].[DB_BACKUP_TO_DISK] @DBName VARCHAR(500)
      , @SaveAs VARCHAR(500)
      , @SaveTo VARCHAR(500)
AS
BEGIN
      SET @DBName = LTRIM(RTRIM(@DBName))
      SET @SaveAs = LTRIM(RTRIM(@SaveAs))
      SET @SaveTo = LTRIM(RTRIM(@SaveTo))

      DECLARE @fileName VARCHAR(4000) = @SaveTo + CASE
                  WHEN right(@SaveTo, 1) <> '\\'
                        THEN '\\'
                  ELSE ''
                  END + @SaveAs + + CASE
                  WHEN right(@SaveAs, 4) <> '.BAK'
                        THEN '.BAK'
                  ELSE ''
                  END

      BACKUP DATABASE @DBName TO DISK = @fileName
END