how to create db back up jobs in sql server 2008? - sql

Microsoft SQL Server 2008 (X64) Microsoft Corporation Developer Edition (64-bit)
i need to create a job for the SQL database to backup everyday
with a different set of new files
i tried doing it and it over rites everyday
i wanted it to create new files everyday...
Plz help me as soon as...
Note:
If i want to create separate folder for each day and stored the back up...
How can i do that plz help me....

If that version include the SQL Agent you can schedule a backup with a SQL script like that:
Edit: corrected code to use dynamic SQL:
declare #sql as nvarchar(1000)
set #sql='BACKUP DATABASE YourDB TO DISK = ''D:\Backups\YourDB-'+convert(char(8),getdate(),112)+'.bak'''
exec sp_executesql #sql

You should be able to do this with a Maintenance Plan and make use of the Back Up Database Task. Taking a look at our backup schedule this generates a new file for the backup each day, with a date and time stamped filename. I believe this is in fact the default and as a consequence for our scenario we also have a cleanup task to delete older backups.

If there is no solution using SSMS, you might try my Powershell script automssqlbackup.

Related

How to create a database snapshot in SQL Server 2008 R2

I used the below command to create a database snapshot in SQL Server 2008 R2:
CREATE DATABASE "dbss" ON (
NAME = "os-file-name",
FILENAME = 'path')
AS SNAPSHOT OF "dbName";
GO
I got this error:
Database Snapshot is not supported on Standard Edition (64-bit).
Does anyone knows how can I create a database snapshot in SQL Server 2008 R2?
Database Snapshot is a feature of the Enterprise Edition and the 2008 Developer Edition.
Besides that there is only little use of Snapshots for a "common user". Most things can be done with a backup too.
Main purpose for snapshots are expensive queries on rapidly changing data.
If you got a huge database and need to execute a query for a report that takes some time there is the danger that data may change while the query / procedure fetches data for the report. In this case you need snapshots. There you can query all your data without having problems with changing data.

How to drop Stored Procedures in a SQL 2000 + SQL 2005 compatible manner?

I have a project that requires me to do development in SQL Server 2005, but do deployments to a SQL Server 2000 box.
For 99% of the SQL code, I have no problems, everything appears to be backwards compatible.
Now I am just about to start adding all the Stored Procedures (SPs) to source control, and I like the idea of doing a drop-add each time the query is executed. I.E. If the SP already exists, first drop it. Then create/re-create the SP.
How do I do this in a single script, in a manner that is compatible with both SQL 2000 and SQL 2005, so that my scripts will just work during Development (2000) AND Production (2005)? I believe the syntax is slightly different, and the SP metadata is stored in different system tables.
Please assist with a working SQL script.
This works for both SQL 2000 and SQL 2005. I have tested it right now.
USE databasename
GO
IF object_id('schema.StoredProcedureName') IS NOT NULL
DROP PROCEDURE schema.StoredProcedureName
GO
CREATE PROCEDURE schema.StoredProcedureName
.. your code
Don't use system tables: use OBJECT_ID
I would also deploy using ALTER but maintain source control using CREATE. That is, I only ever use differential deployment scripts (with ALTER) but compare to my source control folder after release (which as CREATE)
I have both code history and simpler deployments: there is no need to drop/create all procs. What if you forget a permission for example?
I use Red Gate/SVN BTW
I think
IF OBJECT_ID('your_sp_name') IS NOT NULL
will tell you if it is there, although I can't test on 2000 at the mo...
FWIW
select * from sysobjects where type = 'p'
still works in SQL 2008, so am guessing that this is still acceptable as the lowest common denominator. DMV's weren't available in 2000.
You best option is staill the compatibility views, sysobects, syscolumns, etc
Check out the following link
http://msdn.microsoft.com/en-us/library/ms187376.aspx
Many of the system tables from earlier
releases of SQL Server are now
implemented as a set of views. These
views are known as compatibility
views, and they are meant for backward
compatibility only. The compatibility
views expose the same metadata that
was available in SQL Server 2000.
It seems to me that you recreate all STORED PROCEDUREs with respect of sys.sp_refreshsqlmodule like if is described in my old answer I'm looking for a reliable way to verify T-SQL stored procedures. Anybody got one?. The code of STORED PROCEDUREs will be one more time verified inclusive off dependencies.
Using the INFORMATION_SCHEMA.ROUTINES view should work in SQL Server 2000, 2005, and 2008. The only downside is that the view is no longer a viable means of determining the object's schema.
But if that is not a concern, try a script like this:
USE YourDB
GO
IF EXISTS (
SELECT *
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_NAME = 'usp_test'
) DROP PROCEDURE usp_test
GO
CREATE PROCEDURE usp_test AS
SELECT 1 AS val
GO
EXEC usp_test
GO
In most cases, I'd try to run SQL2000 TSQL on the 2005 box, as I'd expect it to be largely backward-compatible. That said, you ought to finish upgrading your production box so you can use newer TSQL.
In cases where you can't find compatibility between the versions, you could first detect the version.
To determine which version of SQL Server 2000/2005 is running, connect to SQL Server 2000/2005 by using Query Analyzer, and then run the following code:
SELECT
SERVERPROPERTY('productversion'),
SERVERPROPERTY ('productlevel'),
SERVERPROPERTY ('edition')
The results are:
The product version (for example, 8.00.534).
The product level (for example, “RTM” or “SP2″).
The edition (for example, “Standard Edition”).
For example, the result looks similar to:
8.00.534 RTM Standard Edition
Source: http://blog.sqlauthority.com/2007/03/07/sql-server-script-to-determine-which-version-of-sql-server-2000-2005-is-running/
Once you determine the version, you can execute the proper level of code.

How to schedule a stored procedure?

How do I schedule a stored procedure in Sql Server 2005 that it runs once at the start of every month (and on database startup)?
You will need to create a job using the SQL Server Agent.
In SQL Server Management Studio, go expand the SQL Server Agent node under the DB server, right click the Jobs folder and select New Job...
(If the SQL Server Agent node does not appear, you may be missing the required permissions)
That will take you through a wizard to schedule a sproc to run on whatever schedule you want.
In terms of how to get a sproc to run on db startup, see this article.

How can I export data from SQL Server?

I need to export database from SQL Server 2005 to SQL scripts (like we can easily do in MySQL). So I want to get generated file with scripts like this
INSERT INTO ... [row 1]
INSERT INTO ... [row 2]
INSERT INTO ... [row 3]
...
Can anybody explain how can I do this step-by-step?
Actually, one of the easist ways to export data from a MSSQL 2005 database is to use the SQL Server Database Publishing Toolkit which is described in length on Scott Guthrie's blog.
In addition, the SQL Database Publishing toolkit was derived from the tools already builtin to SQL 2005. The article Create Script to Copy Database Schema and All The Objects – Stored Procedure, Functions, Triggers, Tables, Views, Constraints and All Other Database Objects walks you through the different steps to script out the items that make up a database.
Another tool derived from the SQL 2005 tools is the Database Publishing Wizard which is a command line tool which scripts out all items of a database.
A final link to read tells how to Use the Database Publishing Wizard to script your table data.
Good luck and hope this helps you.
If you need to take an entire database, including schema, objects and data, I find the easiest way is to create a full backup and then restore it elsewhere. SQL Server Management Studio includes lots of different options to generate backups, including options to include users and to script object level permissions etc.. Detailed instructions for creating a backup and restoring from a backup from SQL Server Management Studi are available on MSDN.
If you just want to script insert statements to copy the data somewhere else, I've had success with this stored procedure. There are detailed instructions in the comments at the top of that script (scroll down past the NOTE to the examples). Basically once you've executed the stored procedure once, you can call the proc using a command like:
EXEC sp_generate_inserts 'tableName'
SQL Server Management Studio does not have this feature. You need some third party tool.
Eg. RedGate or SQLDumper.
Or you can write your own.
Try this
SELECT 'EXEC sp_generate_inserts ' +
'[' + name + ']' +
',#owner = ' +
'[' + RTRIM(USER_NAME(uid)) + '],' +
'#ommit_images = 1, #disable_constraints = 1'
FROM sysobjects
WHERE type = 'U' AND
OBJECTPROPERTY(id,'ismsshipped') = 0
Obtained from My code library . Just go a bit down in the page and you will find
Hope this helps
I dealt with this a while ago by writing a script (VBScript) to do it. Handles most data types, including blobs.
Get it from my site.
HTH.
This feature doesn't exist in SQL 2005. However, it was added in SQL 2008, so one solution is to upgrade. You might use the free SQL Express or the low-cost SQL Developer, if they meet your requirements.
Otherwise, you would need to write a program to do it, or use a third-party solution.

Backup SQL Schema Only?

I need to create a backup of a SQL Server 2005 Database that's only the structure...no records, just the schema. Is there any way to do this?
EDIT: I'm trying to create a backup file to use with old processes, so a script wouldn't work for my purposes, sorry
Use a 3 step process:
Generate a script from the working database
Create a new database from that script
Create a backup of the new database
Why not just use SQL Management Studio to create a complete script of your database and the objects?
Toad for SQL Server does this nicely, if you're considering a commercial product.
I make heavy use of this tool:
SQLBalance for MySQL
Unfortunately; its windows only... but works like a charm to move databases around, data or no data, merge or compare.
As of SQL Server 2012 (patched), you can make a schema only clone of your database using DBCC CLONEDATABASE. Then simply backup the clone.
dbcc clonedatabase(Demo, Demo_Clone) with verify_clonedb;
alter database [Demo_Clone] set read_write;
backup database [Demo_Clone] to disk = N'C:\temp\Demo_SchemaOnly_20220821.bak';
drop database [Demo_Clone];
Read more here: Schema Only Database Backup