In This Article i am going to describes how to create a SQL Server Agent job step that executes SQL scripts in SQL Server 2012 by using SQL Server Management Studio,I am giving you a example to take backup (.BAK) file of sql server database on daily basis at some specific time which is given in job scheduler.
SQL Server Management Studio can be used to create a database backup job to take backup of an user database. Just following some steps and UI work process to create a simple backup job, run the job and you can see the results on screen.
Step 1:- SQL Server Management Studio and login with your credential.
Step 2:- After connect to sql server,Expand object explorer=>sql server agent=>Exapnd Job=>Right click=>select in menu "New job".see image..
Step 3:- Select General=>type name of sql agent job,you can write description also. see image....
Step 4:- Select Steps=>Click "New"=>"OK" on bottom of screen.see image....
After click on new open new tab see in next screen.
Step 5:- Select General=>type Step name=>Command ,In command panel you can use sql query.see image...
Here my query is use to take backup of sql database.I have created a cursor to take backup file of database.
- DECLARE @name VARCHAR(50) -- database name
- DECLARE @path VARCHAR(256) -- path for backup files
- DECLARE @fileName VARCHAR(256) -- filename for backup
- DECLARE @fileDate VARCHAR(20) -- used for file name
- -- specify database backup directory
- SET @path = 'C:\Backup\'
- -- specify filename format
- SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112)
- DECLARE db_cursor CURSOR FOR
- SELECT name
- FROM master.dbo.sysdatabases
- WHERE name IN ('TestDB')
- OPEN db_cursor
- FETCH NEXT FROM db_cursor INTO @name
- WHILE @@FETCH_STATUS = 0
- BEGIN
- SET @fileName = @path + @name + '_' + @fileDate + '.BAK'
- BACKUP DATABASE @name TO DISK = @fileName
- FETCH NEXT FROM db_cursor INTO @name
- END
- CLOSE db_cursor
- DEALLOCATE db_cursor
Step 8:- After click on "Start job at step" your job is running and performing according your query.see image....
Also you can check status of job in this panel.
Step 9:- After job completion you can see the "success" status for this job,see image....
After that you can check your back file (.BAK) file has been created on specified path.
Step 10:- After job completion you can see the history of job.see image..
After that you can see whole information and history of your job.see image..
You have just read an article that categorized by title SQL
by title Create sql server database backup(.BAK) file using sql server job agent.. You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/09/part-33create-sql-server-database.html. Thank You!
Author:
Bikesh Srivastava - Friday, September 2, 2016
Thanks for providing this informative information you may also refer.
ReplyDeletehttp://www.s4techno.com/blog/2016/07/27/introduction-to-sql/
valuable information.. Nice Blog
ReplyDeleteSQL Azure Online Training
Azure SQL Training
SQL Azure Training