Showing posts with label time. Show all posts
Showing posts with label time. Show all posts

Thursday, March 29, 2012

Database comparision

Language : VB.NET
Database : MSSQLserver2000
i have developed a tool to compare two databases, but it is taking long time to compare. First it will take the whole table into main memory(order by primary key). Same procedure for table2 of second database. Then it will start comparison by taking row by row. i tested with one third party tool, which is taking only 2 min to compare 800000 records of a table. My tool is taking 1 hour,40 mins to do that. Is there any other optimized method?

Regards,

Loka

Did you consider that instead of checking column by column, that you 'might' be able to compare a checksum() of the column, or even of the entire row?|||

Hi,

The checksum idea is quite good - it should help you speed up the process. Just a couple of more thoughts on the subject:

(a) Any implementation with a client application would require an access to the data in some form - even if you use a server-side cursor, you would have to fetch the data for comparison and it will travel (maybe even through network if you're not running the app local to SQL Server) to the application. Thus the best approach is to implement the comparison inside SQL itself - as stored procedures, queries, views, etc. Be careful though - the comparison may consume SQL resources and starve existing processes and clients. You should be able to find a ballance between the server impact and the comparison task performance.

(b) You could take advantage of some of the new T-SQL enhancements in SQL 2005 like "intersect" and "except".

(c) If in the end, a standalone application proves to be needed, use OLEDB or ODBC directly with SQL Native Client to achieve maximum performance. Plan carefully for the types of cursors you need (ideally a simple firehose should be the fastest), the potential for blocking or other isolation issues, the proximity and network utilization between client and server, etc.

HTH,

Jivko Dobrev - MSFT

--
This posting is provided "AS IS" with no warranties, and confers no rights.

|||

Some other random ideas:

1. bcp out the tables and do a file compare of the resulting output files, if nothing is different this should be really fast.

2. Comparing rows in bcp out might actually be faster than scanning records using a recordset for sparse differences, hard to tell. For example, say you bcp out the tables into c:\foo1.txt and c:\foo2.txt. Then you can scan a single line at a time from each file and just memcmp the entire line in one command. If there is a missing pkey, they you have to scan each file to resync the keys but in general it should work pretty fast I think.

3. Computing a checksum on the server side should be faster than trying to download the data and checksum downloaded data I think.

Database compare

Language : VB.NET
Database : MSSQLserver2000
i have developed a tool to compare two databases, but it is taking long time to compare. First it will take the whole table into main memory(order by primary key). Same procedure for table2 of second database. Then it will start comparison by taking row by row. i tested with one third party tool, which is taking only 2 min to compare 800000 records of a table. My tool is taking 1 hour,40 mins to do that. Is there any other optimized method?

Regards,
Lokait is likely that you wrote inefficient code. without providing us the information in Brett's sticky at the top of this page, you are unlikely to get any replies you find helpful.|||Are you taking advantage of the CHECKSUM and BINARY_CHECKSUM functions for comparing records?|||BINARY_CHECKSUM rocks! We use it for this very same situation, but use a stored proc that does a full outer join (to grab missing rows from both tables also, in addition to catching data differences with the BINARY_CHECKSUM) and this morning's run took 4.26 minutes to pass/compare two tables on two different servers each with 12,224,072 rows.

yep, we bad, we bad...|||...as long as you don't need ABSOLUTE EXACT ACCURACY, binary_checksum is very efficient.|||I seem to recall some trouble with it, but your comment irritates the hell out of...err...*blush* I mean, piques my interest :)

How can you be more absolute, exactly accurate than a binary comparison? Guess I need to go look at BOL again ;0

I was tripped up a time or two because it actually seems to be a byte-by-byte comparison. As I recall, if you are comparing two columns named "price", and one is defined as a FLOAT, and one as a MONEY, and both contain 2.45, the BINARY COMPARE will fail.

Also, even if both your columns DATE contain '12/21/2006', but one is defined as a datetime, and the other a smalldatetime, the BINARY COMPARE will fail.

In both cases, if you think about it, it makes sense. from the BINARY perspective the bits involved are going to be different because the variable size is different. But I still got tripped up with them early on.|||Thanks for your quick response... We implemented a tool to compare the databases in VB.NET and we should not put any stored procedure in the any of databases. Is there any other way to compare long datatypes in VB.NET.
if there is 10 lakh records in each database then main emory is not at all sufficient to take the whole table at a time. Please suggest me a better approach to solve this issue..|||How can you be more absolute, exactly accurate than a binary comparison? Because it is possible for two different strings to yield the same binary checksum value. In some scenarios, it is almost likely to happen.

We implemented a tool to compare the databases in VB.NET and we should not put any stored procedure in the any of databases.Your system could create a temporary stored procedure in the database and then delete it upon completion.|||Binary Checksum does exactly what the name implies, it computes a binary checksum. You can compute a sum from a row that is almost 8 Kb long, and even longer if you use TEXT or IMAGE columns. The checksum is a whole bunch smaller than the actual data being summed, so there are some combinations of data that will produce the same checksum value.

Binary Checksum is good, but it is a long way from being perfect (no checksum can be perfect in that sense).

-PatP|||Is it possible to create temporary stored procedure in the remote server?
"Binary check sum" is database function or .Net function? what is the syntax?
If you have sample stored procedure to compare two databases, please give it to me.

Regards,
Loka|||First it will take the whole table into main memory
Very bad idea. What do you do if the two tables do not fit into memory?|||Yes that's a problem, how to overcome this?|||Is it possible to create temporary stored procedure in the remote server?
"Binary check sum" is database function or .Net function? what is the syntax?
If you have sample stored procedure to compare two databases, please give it to me.

Regards,
Loka
BINARY_CHECKSUM is a SQLSVR database function.
You can create a stored procedure on the database at the start of your process using a simple CREATE PROCEDURE statement, and then drop it when you are finished with DROP PROCEDURE. The login used will need to have sufficient permissions to create procedures.|||Yes that's a problem, how to overcome this?I would recommend a cursor.

...just funnin' ya guys.

That's the purpose of the stored procedure, which can just select and (outer) join the two tables to find any differences.

And yup, you guys that dis' the checksum, binary or otherwise...I suppose you gotta know thy data, and thy checksum algorithm, and see if the risk is worth the benefit. As Mr. Pat pointed out, as soon as you start using a checksum, you introduce the possibility/probability that a checksum comparison will not catch a difference in data. There is some thought out there that the more data you scrunch into your checksum fields/components, the less likely to have this problem show up, but I am not sure I buy that at face value.

As with most things in life, it depends.

...and that is as close to philosophical as I am willing to get on Christmas Eve Eve Eve.

Tuesday, March 27, 2012

Database coming up suspect.

Let me tell you the scenario. We are doing the following to ensure we have
the fastest recovery time possible if Windows updates applied to the box
crash it.
When we get ready to apply updates to a box we want to break the driver
mirror by pulling the second drive. That way if the updates we are about to
apply break something we can just put the other drive in. But when we do this
on our test boxes the SQL databases come up suspect. Can anyone help me?
These are the steps we take.
Shut SQL down > Turn off box > Pull drive > turn PC on > log onto windows >
â'Database is now Suspectâ'."Rhild" <Rhild@.discussions.microsoft.com> wrote in message
news:EFF4313C-521D-42F1-895C-5DA19E006470@.microsoft.com...
> Let me tell you the scenario. We are doing the following to ensure we have
> the fastest recovery time possible if Windows updates applied to the box
> crash it.
> When we get ready to apply updates to a box we want to break the driver
> mirror by pulling the second drive. That way if the updates we are about
> to
> apply break something we can just put the other drive in. But when we do
> this
> on our test boxes the SQL databases come up suspect. Can anyone help me?
> These are the steps we take.
> Shut SQL down > Turn off box > Pull drive > turn PC on > log onto windows
> >
> "Database is now Suspect".
>
In theory, if this is all you're doing, it should work. I've done similiar
things.
However, it sounds like you're either pulling the wrong drive, or Windows is
somehow changing the drive letter(s) when you do this.
Check the errorlog for more details.
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com

Database coming up suspect.

Let me tell you the scenario. We are doing the following to ensure we have
the fastest recovery time possible if Windows updates applied to the box
crash it.
When we get ready to apply updates to a box we want to break the driver
mirror by pulling the second drive. That way if the updates we are about to
apply break something we can just put the other drive in. But when we do this
on our test boxes the SQL databases come up suspect. Can anyone help me?
These are the steps we take.
Shut SQL down > Turn off box > Pull drive > turn PC on > log onto windows >
“Database is now Suspect”.
"Rhild" <Rhild@.discussions.microsoft.com> wrote in message
news:EFF4313C-521D-42F1-895C-5DA19E006470@.microsoft.com...
> Let me tell you the scenario. We are doing the following to ensure we have
> the fastest recovery time possible if Windows updates applied to the box
> crash it.
> When we get ready to apply updates to a box we want to break the driver
> mirror by pulling the second drive. That way if the updates we are about
> to
> apply break something we can just put the other drive in. But when we do
> this
> on our test boxes the SQL databases come up suspect. Can anyone help me?
> These are the steps we take.
> Shut SQL down > Turn off box > Pull drive > turn PC on > log onto windows
> "Database is now Suspect".
>
In theory, if this is all you're doing, it should work. I've done similiar
things.
However, it sounds like you're either pulling the wrong drive, or Windows is
somehow changing the drive letter(s) when you do this.
Check the errorlog for more details.
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com

Database coming up suspect.

Let me tell you the scenario. We are doing the following to ensure we have
the fastest recovery time possible if Windows updates applied to the box
crash it.
When we get ready to apply updates to a box we want to break the driver
mirror by pulling the second drive. That way if the updates we are about to
apply break something we can just put the other drive in. But when we do thi
s
on our test boxes the SQL databases come up suspect. Can anyone help me?
These are the steps we take.
Shut SQL down > Turn off box > Pull drive > turn PC on > log onto windows >
“Database is now Suspect”."Rhild" <Rhild@.discussions.microsoft.com> wrote in message
news:EFF4313C-521D-42F1-895C-5DA19E006470@.microsoft.com...
> Let me tell you the scenario. We are doing the following to ensure we have
> the fastest recovery time possible if Windows updates applied to the box
> crash it.
> When we get ready to apply updates to a box we want to break the driver
> mirror by pulling the second drive. That way if the updates we are about
> to
> apply break something we can just put the other drive in. But when we do
> this
> on our test boxes the SQL databases come up suspect. Can anyone help me?
> These are the steps we take.
> Shut SQL down > Turn off box > Pull drive > turn PC on > log onto windows
> "Database is now Suspect".
>
In theory, if this is all you're doing, it should work. I've done similiar
things.
However, it sounds like you're either pulling the wrong drive, or Windows is
somehow changing the drive letter(s) when you do this.
Check the errorlog for more details.
Greg Moore
SQL Server DBA Consulting
sql (at) greenms.com http://www.greenms.com

Database become inaccessible

Hi,
log file of my database become inaccessible after some time.
After restarting mssqlserver, few hours it is working fine and then it happe
n again.
In the serverlog I can see:
2004-01-28 04:18:21.65 spid55 udopen: Operating system error 32(The proce
ss cannot access the file because it is being used by another process.) duri
ng the creation/opening of physical device C:\Program Files\Microsoft SQL Se
rver\MSSQL\Data\Replication
DB_log.LDF.
2004-01-28 04:18:21.67 spid55 FCB::Open failed: Could not open device C:\
Program Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF for virt
ual device number (VDN) 2.
2004-01-28 04:18:21.78 spid55 Attempting to rebuild primary log file for
database ReplicationDB.
2004-01-28 04:18:21.78 spid55 FCB::CreateFile() failed with error 80 for
file C:\Program Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF.
regards
janCheck eventlog etc for OS and HW errors. Also, check if you have autoclose o
n, that can result in
the file being closed that the file is used by some other program (anti-viru
s, defrag etc).
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=...ls
erver
"Jan Zatko" <jan.zatko@.frequentis.com> wrote in message
news:855E0933-1BE8-4B7F-94D1-BA214CBADDB7@.microsoft.com...
quote:

> Hi,
> log file of my database become inaccessible after some time.
> After restarting mssqlserver, few hours it is working fine and then it hap
pen again.
> In the serverlog I can see:
> 2004-01-28 04:18:21.65 spid55 udopen: Operating system error 32(The process cannot acce
ss the

file because it is being used by another process.) during the creation/openi
ng of physical device
C:\Program Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF.
quote:

> 2004-01-28 04:18:21.67 spid55 FCB::Open failed: Could not open device C:\Program[/color
]

Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF for virtual device number (VDN) 2.[QUO
TE]
> 2004-01-28 04:18:21.78 spid55 Attempting to rebuild primary log file for database[/colo
r][/QUOTE]
ReplicationDB.
quote:

> 2004-01-28 04:18:21.78 spid55 FCB::CreateFile() failed with error 80 for file C:\Progra
m

Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF.
quote:

> regards
> jan

Database become inaccessible

Hi
log file of my database become inaccessible after some time.
After restarting mssqlserver, few hours it is working fine and then it happen again
In the serverlog I can see
2004-01-28 04:18:21.65 spid55 udopen: Operating system error 32(The process cannot access the file because it is being used by another process.) during the creation/opening of physical device C:\Program Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF
2004-01-28 04:18:21.67 spid55 FCB::Open failed: Could not open device C:\Program Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF for virtual device number (VDN) 2
2004-01-28 04:18:21.78 spid55 Attempting to rebuild primary log file for database ReplicationDB.
2004-01-28 04:18:21.78 spid55 FCB::CreateFile() failed with error 80 for file C:\Program Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF.
regard
janCheck eventlog etc for OS and HW errors. Also, check if you have autoclose on, that can result in
the file being closed that the file is used by some other program (anti-virus, defrag etc).
--
Tibor Karaszi, SQL Server MVP
Archive at: http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Jan Zatko" <jan.zatko@.frequentis.com> wrote in message
news:855E0933-1BE8-4B7F-94D1-BA214CBADDB7@.microsoft.com...
> Hi,
> log file of my database become inaccessible after some time.
> After restarting mssqlserver, few hours it is working fine and then it happen again.
> In the serverlog I can see:
> 2004-01-28 04:18:21.65 spid55 udopen: Operating system error 32(The process cannot access the
file because it is being used by another process.) during the creation/opening of physical device
C:\Program Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF.
> 2004-01-28 04:18:21.67 spid55 FCB::Open failed: Could not open device C:\Program
Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF for virtual device number (VDN) 2.
> 2004-01-28 04:18:21.78 spid55 Attempting to rebuild primary log file for database
ReplicationDB.
> 2004-01-28 04:18:21.78 spid55 FCB::CreateFile() failed with error 80 for file C:\Program
Files\Microsoft SQL Server\MSSQL\Data\ReplicationDB_log.LDF.
> regards
> jansql

Sunday, March 25, 2012

database backup-overwrites old backupfile?

Hi just wondering when you set up a scheluled database backup, does the old
backup file get written over each time the scheduled backup takes place as
this is what I would like it to do?
thanks.
Paul G
Software engineer.
The BACKUP command does not overwrite by default. Defile is NOINIT which is append. To overwrite,
specify INIT. If you schedule using some type if GUI instead of typing the command, we need to know
more about that GUI and what checkboxes etc you have checked.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> Hi just wondering when you set up a scheluled database backup, does the old
> backup file get written over each time the scheduled backup takes place as
> this is what I would like it to do?
> thanks.
> --
> Paul G
> Software engineer.
|||Paul,
The default is NOINIT which appends the backup to the device. You can use
WITH INIT to overwrite the contents of the backup device.
See BACKUP in the SQL BOL for more details.
HTH
Jerry
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> Hi just wondering when you set up a scheluled database backup, does the
> old
> backup file get written over each time the scheduled backup takes place as
> this is what I would like it to do?
> thanks.
> --
> Paul G
> Software engineer.
|||ok thanks did use the gui to set up the schedule.
Paul G
Software engineer.
"Jerry Spivey" wrote:

> Paul,
> The default is NOINIT which appends the backup to the device. You can use
> WITH INIT to overwrite the contents of the backup device.
> See BACKUP in the SQL BOL for more details.
> HTH
> Jerry
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
>
>
|||looks like the gui has a selection do not keep longer than 4 weeks which will
work for what I needed. I tried to start the server agent service and get
the following error,
error 5-access is denied. I am able to access everything on the dbase
server so not sure why I am getting this error. Do you know where I need to
check the permissions? thanks.
Paul G
Software engineer.
"Tibor Karaszi" wrote:

> The BACKUP command does not overwrite by default. Defile is NOINIT which is append. To overwrite,
> specify INIT. If you schedule using some type if GUI instead of typing the command, we need to know
> more about that GUI and what checkboxes etc you have checked.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
>
|||By GUI, I assume you mean the Maintenance Plan Wizard. Yes, it has an option to delete old backup
files. It creates a new file each time you do backup, including a time stamp in the file name. As
for the access denied errors, we need more information. First, understand that the service account
for SQL Server Agent and for the SQL Server service need permissions on whatever you need to do
(like accessing the backup share). I suggest you define a report file for your maint plan, as that
will probably give more details information about exactly what it is that access is denied for etc.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:4AD647D2-C767-4B8B-B02D-F1752D96D0C6@.microsoft.com...[vbcol=seagreen]
> looks like the gui has a selection do not keep longer than 4 weeks which will
> work for what I needed. I tried to start the server agent service and get
> the following error,
> error 5-access is denied. I am able to access everything on the dbase
> server so not sure why I am getting this error. Do you know where I need to
> check the permissions? thanks.
> --
> Paul G
> Software engineer.
>
> "Tibor Karaszi" wrote:

database backup-overwrites old backupfile?

Hi just wondering when you set up a scheluled database backup, does the old
backup file get written over each time the scheduled backup takes place as
this is what I would like it to do?
thanks.
--
Paul G
Software engineer.The BACKUP command does not overwrite by default. Defile is NOINIT which is
append. To overwrite,
specify INIT. If you schedule using some type if GUI instead of typing the c
ommand, we need to know
more about that GUI and what checkboxes etc you have checked.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> Hi just wondering when you set up a scheluled database backup, does the ol
d
> backup file get written over each time the scheduled backup takes place as
> this is what I would like it to do?
> thanks.
> --
> Paul G
> Software engineer.|||Paul,
The default is NOINIT which appends the backup to the device. You can use
WITH INIT to overwrite the contents of the backup device.
See BACKUP in the SQL BOL for more details.
HTH
Jerry
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> Hi just wondering when you set up a scheluled database backup, does the
> old
> backup file get written over each time the scheduled backup takes place as
> this is what I would like it to do?
> thanks.
> --
> Paul G
> Software engineer.|||ok thanks did use the gui to set up the schedule.
--
Paul G
Software engineer.
"Jerry Spivey" wrote:

> Paul,
> The default is NOINIT which appends the backup to the device. You can use
> WITH INIT to overwrite the contents of the backup device.
> See BACKUP in the SQL BOL for more details.
> HTH
> Jerry
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
>
>|||looks like the gui has a selection do not keep longer than 4 weeks which wil
l
work for what I needed. I tried to start the server agent service and get
the following error,
error 5-access is denied. I am able to access everything on the dbase
server so not sure why I am getting this error. Do you know where I need to
check the permissions? thanks.
--
Paul G
Software engineer.
"Tibor Karaszi" wrote:

> The BACKUP command does not overwrite by default. Defile is NOINIT which i
s append. To overwrite,
> specify INIT. If you schedule using some type if GUI instead of typing the
command, we need to know
> more about that GUI and what checkboxes etc you have checked.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
>|||By GUI, I assume you mean the Maintenance Plan Wizard. Yes, it has an option
to delete old backup
files. It creates a new file each time you do backup, including a time stamp
in the file name. As
for the access denied errors, we need more information. First, understand th
at the service account
for SQL Server Agent and for the SQL Server service need permissions on what
ever you need to do
(like accessing the backup share). I suggest you define a report file for yo
ur maint plan, as that
will probably give more details information about exactly what it is that ac
cess is denied for etc.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:4AD647D2-C767-4B8B-B02D-F1752D96D0C6@.microsoft.com...[vbcol=seagreen]
> looks like the gui has a selection do not keep longer than 4 weeks which w
ill
> work for what I needed. I tried to start the server agent service and get
> the following error,
> error 5-access is denied. I am able to access everything on the dbase
> server so not sure why I am getting this error. Do you know where I need
to
> check the permissions? thanks.
> --
> Paul G
> Software engineer.
>
> "Tibor Karaszi" wrote:
>

database backup-overwrites old backupfile?

Hi just wondering when you set up a scheluled database backup, does the old
backup file get written over each time the scheduled backup takes place as
this is what I would like it to do?
thanks.
--
Paul G
Software engineer.The BACKUP command does not overwrite by default. Defile is NOINIT which is append. To overwrite,
specify INIT. If you schedule using some type if GUI instead of typing the command, we need to know
more about that GUI and what checkboxes etc you have checked.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> Hi just wondering when you set up a scheluled database backup, does the old
> backup file get written over each time the scheduled backup takes place as
> this is what I would like it to do?
> thanks.
> --
> Paul G
> Software engineer.|||Paul,
The default is NOINIT which appends the backup to the device. You can use
WITH INIT to overwrite the contents of the backup device.
See BACKUP in the SQL BOL for more details.
HTH
Jerry
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> Hi just wondering when you set up a scheluled database backup, does the
> old
> backup file get written over each time the scheduled backup takes place as
> this is what I would like it to do?
> thanks.
> --
> Paul G
> Software engineer.|||ok thanks did use the gui to set up the schedule.
--
Paul G
Software engineer.
"Jerry Spivey" wrote:
> Paul,
> The default is NOINIT which appends the backup to the device. You can use
> WITH INIT to overwrite the contents of the backup device.
> See BACKUP in the SQL BOL for more details.
> HTH
> Jerry
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> > Hi just wondering when you set up a scheluled database backup, does the
> > old
> > backup file get written over each time the scheduled backup takes place as
> > this is what I would like it to do?
> > thanks.
> > --
> > Paul G
> > Software engineer.
>
>|||looks like the gui has a selection do not keep longer than 4 weeks which will
work for what I needed. I tried to start the server agent service and get
the following error,
error 5-access is denied. I am able to access everything on the dbase
server so not sure why I am getting this error. Do you know where I need to
check the permissions? thanks.
--
Paul G
Software engineer.
"Tibor Karaszi" wrote:
> The BACKUP command does not overwrite by default. Defile is NOINIT which is append. To overwrite,
> specify INIT. If you schedule using some type if GUI instead of typing the command, we need to know
> more about that GUI and what checkboxes etc you have checked.
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
> Blog: http://solidqualitylearning.com/blogs/tibor/
>
> "Paul" <Paul@.discussions.microsoft.com> wrote in message
> news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
> > Hi just wondering when you set up a scheluled database backup, does the old
> > backup file get written over each time the scheduled backup takes place as
> > this is what I would like it to do?
> > thanks.
> > --
> > Paul G
> > Software engineer.
>|||By GUI, I assume you mean the Maintenance Plan Wizard. Yes, it has an option to delete old backup
files. It creates a new file each time you do backup, including a time stamp in the file name. As
for the access denied errors, we need more information. First, understand that the service account
for SQL Server Agent and for the SQL Server service need permissions on whatever you need to do
(like accessing the backup share). I suggest you define a report file for your maint plan, as that
will probably give more details information about exactly what it is that access is denied for etc.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
Blog: http://solidqualitylearning.com/blogs/tibor/
"Paul" <Paul@.discussions.microsoft.com> wrote in message
news:4AD647D2-C767-4B8B-B02D-F1752D96D0C6@.microsoft.com...
> looks like the gui has a selection do not keep longer than 4 weeks which will
> work for what I needed. I tried to start the server agent service and get
> the following error,
> error 5-access is denied. I am able to access everything on the dbase
> server so not sure why I am getting this error. Do you know where I need to
> check the permissions? thanks.
> --
> Paul G
> Software engineer.
>
> "Tibor Karaszi" wrote:
>> The BACKUP command does not overwrite by default. Defile is NOINIT which is append. To overwrite,
>> specify INIT. If you schedule using some type if GUI instead of typing the command, we need to
>> know
>> more about that GUI and what checkboxes etc you have checked.
>> --
>> Tibor Karaszi, SQL Server MVP
>> http://www.karaszi.com/sqlserver/default.asp
>> http://www.solidqualitylearning.com/
>> Blog: http://solidqualitylearning.com/blogs/tibor/
>>
>> "Paul" <Paul@.discussions.microsoft.com> wrote in message
>> news:D8C9F159-8933-4EF8-85E8-2873E9815B22@.microsoft.com...
>> > Hi just wondering when you set up a scheluled database backup, does the old
>> > backup file get written over each time the scheduled backup takes place as
>> > this is what I would like it to do?
>> > thanks.
>> > --
>> > Paul G
>> > Software engineer.
>>

Thursday, March 22, 2012

Database backup time

I have database of 17GB. Avg. time taken for backup was around 40min on disk.
For last 2 days it is taking 2hrs. Nothing have changed.
Any ideas how to troubleshoot why backup is taking soo much time.
Thanks...Hi
Have you checked physical fragmentation on your discs?
Are these physical discs used for other things?
Are you on a SAN?
What other processes are being run at that time?
Have the file sizes for data/log files changed?
John
"Satwinder" wrote:
> I have database of 17GB. Avg. time taken for backup was around 40min on disk.
> For last 2 days it is taking 2hrs. Nothing have changed.
> Any ideas how to troubleshoot why backup is taking soo much time.
> Thanks...|||Hi John,
The database & log files are on SAN disk. Files size of neither one has
changed.
Only change i observed is one process which runs a SP is running for last 2
days.
Thanks..
"John Bell" wrote:
> Hi
> Have you checked physical fragmentation on your discs?
> Are these physical discs used for other things?
> Are you on a SAN?
> What other processes are being run at that time?
> Have the file sizes for data/log files changed?
> John
>
> "Satwinder" wrote:
> > I have database of 17GB. Avg. time taken for backup was around 40min on disk.
> > For last 2 days it is taking 2hrs. Nothing have changed.
> >
> > Any ideas how to troubleshoot why backup is taking soo much time.
> >
> > Thanks...|||Hi
Log files and data files should be separated even if they are on a SAN.You
may want to get the SAN engineer to look at the disc stats over the period. I
assume that the backup is to a different drive/LUN? Have you checked the LUNs
to make sure they are not conflicting? Also check out the perfmon counters.
You may want to find out what the SP that has been running for 2 days is
doing! Have you checked disc fragmentation?
John
"Satwinder" wrote:
> Hi John,
> The database & log files are on SAN disk. Files size of neither one has
> changed.
> Only change i observed is one process which runs a SP is running for last 2
> days.
> Thanks..
> "John Bell" wrote:
> > Hi
> >
> > Have you checked physical fragmentation on your discs?
> > Are these physical discs used for other things?
> > Are you on a SAN?
> > What other processes are being run at that time?
> > Have the file sizes for data/log files changed?
> >
> > John
> >
> >
> > "Satwinder" wrote:
> >
> > > I have database of 17GB. Avg. time taken for backup was around 40min on disk.
> > > For last 2 days it is taking 2hrs. Nothing have changed.
> > >
> > > Any ideas how to troubleshoot why backup is taking soo much time.
> > >
> > > Thanks...|||Hi
The SP was doing multiple Select one by one on a table having 67+million
rows. When i checked locks by that SP process i could see only 1 lock - Sch-S
lock on the Table on which the select query were being run. Select query had
nolock option specified.
After i killed the SP, backup completed in 45min.
Do you known if a Select can cause backup to take more time.
Do we have any blog by SQL Server backup team were i can post this issue and
get more information on this.
Thanks..
"John Bell" wrote:
> Hi
> Log files and data files should be separated even if they are on a SAN.You
> may want to get the SAN engineer to look at the disc stats over the period. I
> assume that the backup is to a different drive/LUN? Have you checked the LUNs
> to make sure they are not conflicting? Also check out the perfmon counters.
> You may want to find out what the SP that has been running for 2 days is
> doing! Have you checked disc fragmentation?
> John
>
> "Satwinder" wrote:
> > Hi John,
> >
> > The database & log files are on SAN disk. Files size of neither one has
> > changed.
> > Only change i observed is one process which runs a SP is running for last 2
> > days.
> >
> > Thanks..
> >
> > "John Bell" wrote:
> >
> > > Hi
> > >
> > > Have you checked physical fragmentation on your discs?
> > > Are these physical discs used for other things?
> > > Are you on a SAN?
> > > What other processes are being run at that time?
> > > Have the file sizes for data/log files changed?
> > >
> > > John
> > >
> > >
> > > "Satwinder" wrote:
> > >
> > > > I have database of 17GB. Avg. time taken for backup was around 40min on disk.
> > > > For last 2 days it is taking 2hrs. Nothing have changed.
> > > >
> > > > Any ideas how to troubleshoot why backup is taking soo much time.
> > > >
> > > > Thanks...|||Hi
Your SP sounds like it is using a cursor so you may want to see if you can
improve it. Does this SP take a lot of resources? You may have conflicting
I/O between the SP reading data and your backups reading/writing.
For blogs you may want to look at:
http://searchsqlserver.techtarget.com/general/0,295582,sid87_gci1132175,00.html?track=NL-464
John
"John Bell" wrote:
> Hi
> Have you checked physical fragmentation on your discs?
> Are these physical discs used for other things?
> Are you on a SAN?
> What other processes are being run at that time?
> Have the file sizes for data/log files changed?
> John
>
> "Satwinder" wrote:
> > I have database of 17GB. Avg. time taken for backup was around 40min on disk.
> > For last 2 days it is taking 2hrs. Nothing have changed.
> >
> > Any ideas how to troubleshoot why backup is taking soo much time.
> >
> > Thanks...|||Satwinder wrote:
> Hi
> The SP was doing multiple Select one by one on a table having 67+million
> rows. When i checked locks by that SP process i could see only 1 lock - Sch-S
> lock on the Table on which the select query were being run. Select query had
> nolock option specified.
> After i killed the SP, backup completed in 45min.
> Do you known if a Select can cause backup to take more time.
>
Well of course it can! Running a backup produces load on the server.
Running a query produces load on the server. The greater the load, the
slower things run.
--
Tracy McKibben
MCDBA
http://www.realsqlguy.com|||Thanks everyone for there useful inputs.
"Satwinder" wrote:
> I have database of 17GB. Avg. time taken for backup was around 40min on disk.
> For last 2 days it is taking 2hrs. Nothing have changed.
> Any ideas how to troubleshoot why backup is taking soo much time.
> Thanks...

database backup not clearing the transaction log...

Hi
I have a 150Gb 'time series' type database running in SQL 2000. every sunday
@. 0230 it re-indexes using 'dbcc dbreindex'. this takes several hours and
creates a big fat transaction log.
At 0030 Monday morning the database is backed up using
BACKUP DATABASE [timeseries] TO [Backup_timeseries] WITH INIT , NOUNLOAD ,
NAME = N'timeseries backup - init BAK file', NOSKIP , STATS = 10,
NOFORMAT
which DOES NOT CLEAR THE SIZE OF THE TRANSACTION LOG. The trans log is only
cleared at 0800 Monday morning when the trans log is backed up (and is
continually backed up every 10 minutes throughout the working day all week
until Saturday) using the following syntax:
surely the database backup at 0030 should clear down the size of the trans
log?

> which DOES NOT CLEAR THE SIZE OF THE TRANSACTION LOG. The trans log is
> only
> cleared at 0800 Monday morning when the trans log is backed up (and is
> continually backed up every 10 minutes throughout the working day all week
> until Saturday) using the following syntax:
Sure , it does not. You need to BACKUP LOG file in order to truncate
inactive portions in the LOG
"Methodology" <Methodology@.discussions.microsoft.com> wrote in message
news:11993BB5-D220-4AFF-99D1-F0C7FAE045F6@.microsoft.com...
> Hi
> I have a 150Gb 'time series' type database running in SQL 2000. every
> sunday
> @. 0230 it re-indexes using 'dbcc dbreindex'. this takes several hours and
> creates a big fat transaction log.
> At 0030 Monday morning the database is backed up using
> BACKUP DATABASE [timeseries] TO [Backup_timeseries] WITH INIT , NOUNLOAD
> ,
> NAME = N'timeseries backup - init BAK file', NOSKIP , STATS = 10,
> NOFORMAT
> which DOES NOT CLEAR THE SIZE OF THE TRANSACTION LOG. The trans log is
> only
> cleared at 0800 Monday morning when the trans log is backed up (and is
> continually backed up every 10 minutes throughout the working day all week
> until Saturday) using the following syntax:
> surely the database backup at 0030 should clear down the size of the trans
> log?
>
|||Methodology wrote:
> Hi
> I have a 150Gb 'time series' type database running in SQL 2000. every sunday
> @. 0230 it re-indexes using 'dbcc dbreindex'. this takes several hours and
> creates a big fat transaction log.
> At 0030 Monday morning the database is backed up using
> BACKUP DATABASE [timeseries] TO [Backup_timeseries] WITH INIT , NOUNLOAD ,
> NAME = N'timeseries backup - init BAK file', NOSKIP , STATS = 10,
> NOFORMAT
> which DOES NOT CLEAR THE SIZE OF THE TRANSACTION LOG. The trans log is only
> cleared at 0800 Monday morning when the trans log is backed up (and is
> continually backed up every 10 minutes throughout the working day all week
> until Saturday) using the following syntax:
> surely the database backup at 0030 should clear down the size of the trans
> log?
>
A database backup DOES NOT, repeat, DOES NOT truncate (flush out) the
transaction log, only a log backup will do that (assuming Full or
Bulk-Logged modes). Also, truncation != SHRINKING. The physical size
of the log file will not change when truncating. Shrinking is a
separate process, and there are many reasons why you don't want to do it.
Tracy McKibben
MCDBA
http://www.realsqlguy.com
|||Here is an idea. I did this once at one job I had. I had a similarlarly
large sized databases whose transaction logs "blew out" on a reindex
(weekly). I had a lot of ugly clustered indexes that had to be rebuilt
and took a lot of space. SInce the database was not heavily used during
that period, I did an alter database and put the db on simple mode.
Then it won't blow out the log. I did the re-index on all necessary
tables and then did a full backup. Note, this is dangerous as you break
the transaction log sequence at that time, so you must do an immediate
full backup. In my case, this was ok as it was a mainly Mon-Sat db.
KR
Tracy McKibben wrote:
> Methodology wrote:
> A database backup DOES NOT, repeat, DOES NOT truncate (flush out) the
> transaction log, only a log backup will do that (assuming Full or
> Bulk-Logged modes). Also, truncation != SHRINKING. The physical size
> of the log file will not change when truncating. Shrinking is a
> separate process, and there are many reasons why you don't want to do it.
>
> --
> Tracy McKibben
> MCDBA
> http://www.realsqlguy.com
sql

Wednesday, March 21, 2012

Database Backup

We need to create a back up at a specific time for a client.
Now they have specified that the timings must be exact. The question I
have is this.
Say the back up takes 20 minutes to perform. Will any transactions
that take place in that 20 mins be logged in the database? Or is the
database locked for those transactions and then added to the database
when the backup has finished?
We can't unfortunately take the server off-line while it is in the
back up process.
Many Thanks in advance for your help.
MarkMark,
Go ahead. SQL Server backups are good for all committed transactions at
the time the backup completes. Note: open transactions are not included
in the backup file at the time the backup completes.
The database is not locked during the backup, so you can modify the data
in there and it will still be captured, if it is committed by the time
the backup finishes. There are some restrictions like you can't grow or
shrink the data files during a backup.
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Mark wrote:
> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark|||Hi
The database dump is consistent and point in time correct at the end of the
dump as the dump always copies in the un-truncated commited log transactions
.
In effect, the dump process takes the pages off the database, writes them to
the dump, without locking, and once the DB is dumped, takes the transaction
log and adds it's contents to the same file.
So, a restore is a restore of the Db and a transaction log replay.
The dump is a copy of the database at 10:20 and not at 10:00 if the dump
takes 20 minutes.
Regards
Mike
"Mark" wrote:

> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark
>|||In addition tot he other posts (in case it isn't obvious already):
If they have a need to have a backup from a certain point in time, say 2 pm:
Do the db backup at a some time (perhaps 2pm). After the db backup, do a log
backup. You can now
restore the db backup and then the log backup in which you stop at 2 pm.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mark" <mroffey@.hotmail.com> wrote in message
news:1da0fab5.0410210430.6c03d311@.posting.google.com...
> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark|||Does this mean that for the duration of the backup, committed data is never
flushed to the data file(s)? Thus, the longer the backup, the larger the
trx log file will grow, regardless of the recovery model?
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backups? Use MiniSQLBackup Lite, free!

Monday, March 19, 2012

Database Backup

We need to create a back up at a specific time for a client.
Now they have specified that the timings must be exact. The question I
have is this.
Say the back up takes 20 minutes to perform. Will any transactions
that take place in that 20 mins be logged in the database? Or is the
database locked for those transactions and then added to the database
when the backup has finished?
We can't unfortunately take the server off-line while it is in the
back up process.
Many Thanks in advance for your help.
Mark
Mark,
Go ahead. SQL Server backups are good for all committed transactions at
the time the backup completes. Note: open transactions are not included
in the backup file at the time the backup completes.
The database is not locked during the backup, so you can modify the data
in there and it will still be captured, if it is committed by the time
the backup finishes. There are some restrictions like you can't grow or
shrink the data files during a backup.
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Mark wrote:
> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark
|||Hi
The database dump is consistent and point in time correct at the end of the
dump as the dump always copies in the un-truncated commited log transactions.
In effect, the dump process takes the pages off the database, writes them to
the dump, without locking, and once the DB is dumped, takes the transaction
log and adds it's contents to the same file.
So, a restore is a restore of the Db and a transaction log replay.
The dump is a copy of the database at 10:20 and not at 10:00 if the dump
takes 20 minutes.
Regards
Mike
"Mark" wrote:

> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark
>
|||In addition tot he other posts (in case it isn't obvious already):
If they have a need to have a backup from a certain point in time, say 2 pm:
Do the db backup at a some time (perhaps 2pm). After the db backup, do a log backup. You can now
restore the db backup and then the log backup in which you stop at 2 pm.
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mark" <mroffey@.hotmail.com> wrote in message
news:1da0fab5.0410210430.6c03d311@.posting.google.c om...
> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark
|||Does this mean that for the duration of the backup, committed data is never
flushed to the data file(s)? Thus, the longer the backup, the larger the
trx log file will grow, regardless of the recovery model?
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backups? Use MiniSQLBackup Lite, free!

Database Backup

We need to create a back up at a specific time for a client.
Now they have specified that the timings must be exact. The question I
have is this.
Say the back up takes 20 minutes to perform. Will any transactions
that take place in that 20 mins be logged in the database? Or is the
database locked for those transactions and then added to the database
when the backup has finished?
We can't unfortunately take the server off-line while it is in the
back up process.
Many Thanks in advance for your help.
MarkMark,
Go ahead. SQL Server backups are good for all committed transactions at
the time the backup completes. Note: open transactions are not included
in the backup file at the time the backup completes.
The database is not locked during the backup, so you can modify the data
in there and it will still be captured, if it is committed by the time
the backup finishes. There are some restrictions like you can't grow or
shrink the data files during a backup.
--
Mark Allison, SQL Server MVP
http://www.markallison.co.uk
Looking for a SQL Server replication book?
http://www.nwsu.com/0974973602m.html
Mark wrote:
> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark|||Hi
The database dump is consistent and point in time correct at the end of the
dump as the dump always copies in the un-truncated commited log transactions.
In effect, the dump process takes the pages off the database, writes them to
the dump, without locking, and once the DB is dumped, takes the transaction
log and adds it's contents to the same file.
So, a restore is a restore of the Db and a transaction log replay.
The dump is a copy of the database at 10:20 and not at 10:00 if the dump
takes 20 minutes.
Regards
Mike
"Mark" wrote:
> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark
>|||In addition tot he other posts (in case it isn't obvious already):
If they have a need to have a backup from a certain point in time, say 2 pm:
Do the db backup at a some time (perhaps 2pm). After the db backup, do a log backup. You can now
restore the db backup and then the log backup in which you stop at 2 pm.
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"Mark" <mroffey@.hotmail.com> wrote in message
news:1da0fab5.0410210430.6c03d311@.posting.google.com...
> We need to create a back up at a specific time for a client.
> Now they have specified that the timings must be exact. The question I
> have is this.
> Say the back up takes 20 minutes to perform. Will any transactions
> that take place in that 20 mins be logged in the database? Or is the
> database locked for those transactions and then added to the database
> when the backup has finished?
> We can't unfortunately take the server off-line while it is in the
> back up process.
> Many Thanks in advance for your help.
> Mark|||Does this mean that for the duration of the backup, committed data is never
flushed to the data file(s)? Thus, the longer the backup, the larger the
trx log file will grow, regardless of the recovery model?
--
Peter Yeoh
http://www.yohz.com
Need smaller SQL2K backups? Use MiniSQLBackup Lite, free!|||I want to add a datetime stamp on my backup file names but what I have is not
adding to the filename.
Does anyone know how to do this?|||It would help if you gave a little more background or at least showed what
you were doing now. Here is an example of adding a datetime to a backup
device name but I don't know hot it fits your situation.
DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name NVARCHAR(150)
DECLARE cur_DBs CURSOR STATIC LOCAL
FOR SELECT Catalog_Name
FROM Information_Schema.Schemata
-- WHERE [Catalog_Name] NOT IN ('MASTER','MODEL','MSDB','TEMPDB')
WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
OPEN cur_DBs
FETCH NEXT FROM cur_DBs INTO @.DBName
WHILE @.@.FETCH_STATUS = 0
BEGIN
SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
SET @.Name = @.DBName + N' Full Backup'
PRINT 'Backing up database ' + @.DBName
BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
PRINT '--- '
FETCH NEXT FROM cur_DBs INTO @.DBName
END
CLOSE cur_DBs
DEALLOCATE cur_DBs
Andrew J. Kelly SQL MVP
"Sonya" <Sonya@.discussions.microsoft.com> wrote in message
news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
>I want to add a datetime stamp on my backup file names but what I have is
>not
> adding to the filename.
> Does anyone know how to do this?|||I also want to do this for the transaction log files.
"Andrew J. Kelly" wrote:
> It would help if you gave a little more background or at least showed what
> you were doing now. Here is an example of adding a datetime to a backup
> device name but I don't know hot it fits your situation.
> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name NVARCHAR(150)
>
> DECLARE cur_DBs CURSOR STATIC LOCAL
> FOR SELECT Catalog_Name
> FROM Information_Schema.Schemata
> -- WHERE [Catalog_Name] NOT IN ('MASTER','MODEL','MSDB','TEMPDB')
> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>
> OPEN cur_DBs
> FETCH NEXT FROM cur_DBs INTO @.DBName
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
> SET @.Name = @.DBName + N' Full Backup'
> PRINT 'Backing up database ' + @.DBName
> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> PRINT '--- '
> FETCH NEXT FROM cur_DBs INTO @.DBName
> END
> CLOSE cur_DBs
> DEALLOCATE cur_DBs
>
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >I want to add a datetime stamp on my backup file names but what I have is
> >not
> > adding to the filename.
> >
> > Does anyone know how to do this?
>
>|||Sorry. Currently I am performing complete backups to a network drive. I want
to modify the backups by appending the date and time stamp to the backup
filenames. Then run either a batch file or DTS package to clean-up the folder
by deleting SQL backups after 7 days.
"Andrew J. Kelly" wrote:
> It would help if you gave a little more background or at least showed what
> you were doing now. Here is an example of adding a datetime to a backup
> device name but I don't know hot it fits your situation.
> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name NVARCHAR(150)
>
> DECLARE cur_DBs CURSOR STATIC LOCAL
> FOR SELECT Catalog_Name
> FROM Information_Schema.Schemata
> -- WHERE [Catalog_Name] NOT IN ('MASTER','MODEL','MSDB','TEMPDB')
> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>
> OPEN cur_DBs
> FETCH NEXT FROM cur_DBs INTO @.DBName
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
> SET @.Name = @.DBName + N' Full Backup'
> PRINT 'Backing up database ' + @.DBName
> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> PRINT '--- '
> FETCH NEXT FROM cur_DBs INTO @.DBName
> END
> CLOSE cur_DBs
> DEALLOCATE cur_DBs
>
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >I want to add a datetime stamp on my backup file names but what I have is
> >not
> > adding to the filename.
> >
> > Does anyone know how to do this?
>
>|||Andrew,
Thanks worked. How can I add the time to it as well. I am going to create a
DTS package to run this query.
"Andrew J. Kelly" wrote:
> It would help if you gave a little more background or at least showed what
> you were doing now. Here is an example of adding a datetime to a backup
> device name but I don't know hot it fits your situation.
> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name NVARCHAR(150)
>
> DECLARE cur_DBs CURSOR STATIC LOCAL
> FOR SELECT Catalog_Name
> FROM Information_Schema.Schemata
> -- WHERE [Catalog_Name] NOT IN ('MASTER','MODEL','MSDB','TEMPDB')
> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>
> OPEN cur_DBs
> FETCH NEXT FROM cur_DBs INTO @.DBName
> WHILE @.@.FETCH_STATUS = 0
> BEGIN
> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
> SET @.Name = @.DBName + N' Full Backup'
> PRINT 'Backing up database ' + @.DBName
> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> PRINT '--- '
> FETCH NEXT FROM cur_DBs INTO @.DBName
> END
> CLOSE cur_DBs
> DEALLOCATE cur_DBs
>
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >I want to add a datetime stamp on my backup file names but what I have is
> >not
> > adding to the filename.
> >
> > Does anyone know how to do this?
>
>|||Just change the CONVERT to use an different format and make the variable
large enough to handle the extra characters. You will have to look at
CONVERT in BOL to see which suites your requirement best. The only thing is
that when you add the time portion you usually have to use a format that
inserts dashs or slashes. See if this works for you:
LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
GETDATE(),120),'-',''),':',''),' ',''),12)
Andrew J. Kelly SQL MVP
"Sonya" <Sonya@.discussions.microsoft.com> wrote in message
news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
> Andrew,
> Thanks worked. How can I add the time to it as well. I am going to create
> a
> DTS package to run this query.
> "Andrew J. Kelly" wrote:
>> It would help if you gave a little more background or at least showed
>> what
>> you were doing now. Here is an example of adding a datetime to a backup
>> device name but I don't know hot it fits your situation.
>> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name NVARCHAR(150)
>>
>> DECLARE cur_DBs CURSOR STATIC LOCAL
>> FOR SELECT Catalog_Name
>> FROM Information_Schema.Schemata
>> -- WHERE [Catalog_Name] NOT IN
>> ('MASTER','MODEL','MSDB','TEMPDB')
>> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>>
>> OPEN cur_DBs
>> FETCH NEXT FROM cur_DBs INTO @.DBName
>> WHILE @.@.FETCH_STATUS = 0
>> BEGIN
>> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
>> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
>> SET @.Name = @.DBName + N' Full Backup'
>> PRINT 'Backing up database ' + @.DBName
>> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
>> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
>> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
>> PRINT '--- '
>> FETCH NEXT FROM cur_DBs INTO @.DBName
>> END
>> CLOSE cur_DBs
>> DEALLOCATE cur_DBs
>>
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
>> >I want to add a datetime stamp on my backup file names but what I have
>> >is
>> >not
>> > adding to the filename.
>> >
>> > Does anyone know how to do this?
>>|||That worked thank you.
"Andrew J. Kelly" wrote:
> Just change the CONVERT to use an different format and make the variable
> large enough to handle the extra characters. You will have to look at
> CONVERT in BOL to see which suites your requirement best. The only thing is
> that when you add the time portion you usually have to use a format that
> inserts dashs or slashes. See if this works for you:
> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
> GETDATE(),120),'-',''),':',''),' ',''),12)
>
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
> > Andrew,
> >
> > Thanks worked. How can I add the time to it as well. I am going to create
> > a
> > DTS package to run this query.
> >
> > "Andrew J. Kelly" wrote:
> >
> >> It would help if you gave a little more background or at least showed
> >> what
> >> you were doing now. Here is an example of adding a datetime to a backup
> >> device name but I don't know hot it fits your situation.
> >>
> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name NVARCHAR(150)
> >>
> >>
> >> DECLARE cur_DBs CURSOR STATIC LOCAL
> >> FOR SELECT Catalog_Name
> >> FROM Information_Schema.Schemata
> >> -- WHERE [Catalog_Name] NOT IN
> >> ('MASTER','MODEL','MSDB','TEMPDB')
> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
> >>
> >>
> >> OPEN cur_DBs
> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >>
> >> WHILE @.@.FETCH_STATUS = 0
> >> BEGIN
> >>
> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> >> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
> >>
> >> SET @.Name = @.DBName + N' Full Backup'
> >>
> >> PRINT 'Backing up database ' + @.DBName
> >>
> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> >>
> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> >>
> >> PRINT '--- '
> >>
> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> END
> >>
> >> CLOSE cur_DBs
> >> DEALLOCATE cur_DBs
> >>
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >> >I want to add a datetime stamp on my backup file names but what I have
> >> >is
> >> >not
> >> > adding to the filename.
> >> >
> >> > Does anyone know how to do this?
> >>
> >>
> >>
>
>|||Andrew,
Do you know how to access a network drive within Enterprise Mgr?
I'm trying to restore a db where the backups are located on the network. I
can see other network drives but cannot view the I need. I've already checked
the permissions and made sure the drive was shared. Do you have any
suggestions?
"Andrew J. Kelly" wrote:
> Just change the CONVERT to use an different format and make the variable
> large enough to handle the extra characters. You will have to look at
> CONVERT in BOL to see which suites your requirement best. The only thing is
> that when you add the time portion you usually have to use a format that
> inserts dashs or slashes. See if this works for you:
> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
> GETDATE(),120),'-',''),':',''),' ',''),12)
>
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
> > Andrew,
> >
> > Thanks worked. How can I add the time to it as well. I am going to create
> > a
> > DTS package to run this query.
> >
> > "Andrew J. Kelly" wrote:
> >
> >> It would help if you gave a little more background or at least showed
> >> what
> >> you were doing now. Here is an example of adding a datetime to a backup
> >> device name but I don't know hot it fits your situation.
> >>
> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name NVARCHAR(150)
> >>
> >>
> >> DECLARE cur_DBs CURSOR STATIC LOCAL
> >> FOR SELECT Catalog_Name
> >> FROM Information_Schema.Schemata
> >> -- WHERE [Catalog_Name] NOT IN
> >> ('MASTER','MODEL','MSDB','TEMPDB')
> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
> >>
> >>
> >> OPEN cur_DBs
> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >>
> >> WHILE @.@.FETCH_STATUS = 0
> >> BEGIN
> >>
> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> >> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
> >>
> >> SET @.Name = @.DBName + N' Full Backup'
> >>
> >> PRINT 'Backing up database ' + @.DBName
> >>
> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> >>
> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> >>
> >> PRINT '--- '
> >>
> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> END
> >>
> >> CLOSE cur_DBs
> >> DEALLOCATE cur_DBs
> >>
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >> >I want to add a datetime stamp on my backup file names but what I have
> >> >is
> >> >not
> >> > adding to the filename.
> >> >
> >> > Does anyone know how to do this?
> >>
> >>
> >>
>
>|||Don't use EM, use a script instead. EM has some limitations and things like
this can be much more controlled through scripts. The restore syntax is
pretty simple and there are examples in BOL. If you have troubles with it
post your script and we can help.
--
Andrew J. Kelly SQL MVP
"Sonya" <Sonya@.discussions.microsoft.com> wrote in message
news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
> Andrew,
> Do you know how to access a network drive within Enterprise Mgr?
> I'm trying to restore a db where the backups are located on the network. I
> can see other network drives but cannot view the I need. I've already
> checked
> the permissions and made sure the drive was shared. Do you have any
> suggestions?
> "Andrew J. Kelly" wrote:
>> Just change the CONVERT to use an different format and make the variable
>> large enough to handle the extra characters. You will have to look at
>> CONVERT in BOL to see which suites your requirement best. The only thing
>> is
>> that when you add the time portion you usually have to use a format that
>> inserts dashs or slashes. See if this works for you:
>> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
>> GETDATE(),120),'-',''),':',''),' ',''),12)
>>
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
>> > Andrew,
>> >
>> > Thanks worked. How can I add the time to it as well. I am going to
>> > create
>> > a
>> > DTS package to run this query.
>> >
>> > "Andrew J. Kelly" wrote:
>> >
>> >> It would help if you gave a little more background or at least showed
>> >> what
>> >> you were doing now. Here is an example of adding a datetime to a
>> >> backup
>> >> device name but I don't know hot it fits your situation.
>> >>
>> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
>> >> NVARCHAR(150)
>> >>
>> >>
>> >> DECLARE cur_DBs CURSOR STATIC LOCAL
>> >> FOR SELECT Catalog_Name
>> >> FROM Information_Schema.Schemata
>> >> -- WHERE [Catalog_Name] NOT IN
>> >> ('MASTER','MODEL','MSDB','TEMPDB')
>> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>> >>
>> >>
>> >> OPEN cur_DBs
>> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >>
>> >> WHILE @.@.FETCH_STATUS = 0
>> >> BEGIN
>> >>
>> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
>> >> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
>> >>
>> >> SET @.Name = @.DBName + N' Full Backup'
>> >>
>> >> PRINT 'Backing up database ' + @.DBName
>> >>
>> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
>> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
>> >>
>> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
>> >>
>> >> PRINT '--- '
>> >>
>> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> END
>> >>
>> >> CLOSE cur_DBs
>> >> DEALLOCATE cur_DBs
>> >>
>> >>
>> >> --
>> >> Andrew J. Kelly SQL MVP
>> >>
>> >>
>> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
>> >> >I want to add a datetime stamp on my backup file names but what I
>> >> >have
>> >> >is
>> >> >not
>> >> > adding to the filename.
>> >> >
>> >> > Does anyone know how to do this?
>> >>
>> >>
>> >>
>>|||Andrew or anyone,
I'm trying to write a short sql stmts to attached to a folder and query that
folder for *.bak and if the*.bak is older than a certain amount of days, then
I want to delete them.
Is this possible using tsql?
"Andrew J. Kelly" wrote:
> Don't use EM, use a script instead. EM has some limitations and things like
> this can be much more controlled through scripts. The restore syntax is
> pretty simple and there are examples in BOL. If you have troubles with it
> post your script and we can help.
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
> > Andrew,
> >
> > Do you know how to access a network drive within Enterprise Mgr?
> >
> > I'm trying to restore a db where the backups are located on the network. I
> > can see other network drives but cannot view the I need. I've already
> > checked
> > the permissions and made sure the drive was shared. Do you have any
> > suggestions?
> >
> > "Andrew J. Kelly" wrote:
> >
> >> Just change the CONVERT to use an different format and make the variable
> >> large enough to handle the extra characters. You will have to look at
> >> CONVERT in BOL to see which suites your requirement best. The only thing
> >> is
> >> that when you add the time portion you usually have to use a format that
> >> inserts dashs or slashes. See if this works for you:
> >>
> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
> >> GETDATE(),120),'-',''),':',''),' ',''),12)
> >>
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
> >> > Andrew,
> >> >
> >> > Thanks worked. How can I add the time to it as well. I am going to
> >> > create
> >> > a
> >> > DTS package to run this query.
> >> >
> >> > "Andrew J. Kelly" wrote:
> >> >
> >> >> It would help if you gave a little more background or at least showed
> >> >> what
> >> >> you were doing now. Here is an example of adding a datetime to a
> >> >> backup
> >> >> device name but I don't know hot it fits your situation.
> >> >>
> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
> >> >> NVARCHAR(150)
> >> >>
> >> >>
> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
> >> >> FOR SELECT Catalog_Name
> >> >> FROM Information_Schema.Schemata
> >> >> -- WHERE [Catalog_Name] NOT IN
> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
> >> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
> >> >>
> >> >>
> >> >> OPEN cur_DBs
> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >>
> >> >> WHILE @.@.FETCH_STATUS = 0
> >> >> BEGIN
> >> >>
> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
> >> >>
> >> >> SET @.Name = @.DBName + N' Full Backup'
> >> >>
> >> >> PRINT 'Backing up database ' + @.DBName
> >> >>
> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT , NOUNLOAD ,
> >> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> >> >>
> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> >> >>
> >> >> PRINT '--- '
> >> >>
> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >> END
> >> >>
> >> >> CLOSE cur_DBs
> >> >> DEALLOCATE cur_DBs
> >> >>
> >> >>
> >> >> --
> >> >> Andrew J. Kelly SQL MVP
> >> >>
> >> >>
> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >> >> >I want to add a datetime stamp on my backup file names but what I
> >> >> >have
> >> >> >is
> >> >> >not
> >> >> > adding to the filename.
> >> >> >
> >> >> > Does anyone know how to do this?
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>|||You can't do this strictly with TSQL but this is as close as you will get.
There is a sample sp that will delete old log file backups based on the
timestamp in the name of the files.
-- Removing Older Backup Files --
-- Remove any log files older than 7 days
DECLARE @.Error INT, @.D DATETIME
SET @.D = DATEADD(dd,-7,GETDATE())
EXEC @.Error = remove_old_log_files @.D
SELECT @.Error
--
CREATE PROCEDURE remove_old_log_files
@.DelDate DATETIME
AS
SET NOCOUNT ON
DECLARE @.SQL VARCHAR(500), @.FName VARCHAR(40), @.Error INT
DECLARE @.Delete VARCHAR(300), @.Msg VARCHAR(100), @.Return INT
-- Used later when we cast strings to Datetimes
SET DATEFORMAT MDY
-- Create a table to hold the results of the DIR command
IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
DROP TABLE #DirList
CREATE TABLE #dirlist (FName VARCHAR(1000))
-- Create a table to hold any errors
IF OBJECT_ID('tempdb..#Errors') IS NOT NULL
DROP TABLE #Errors
CREATE TABLE #Errors (Results VARCHAR(1000))
-- Insert the results of the dir cmd into a table so we can scan it
INSERT INTO #dirlist (FName)
exec @.Return = master..xp_cmdshell 'dir /OD C:\Data\Backups\*.Bak'
SET @.Error = @.@.ERROR
IF @.Error <> 0 OR @.Return <> 0
BEGIN
IF @.Return = 1
SET @.Error = -1
SET @.Msg = 'Error while getting the filenames with DIR '
GOTO On_Error
END
-- Remove the garbage
DELETE #dirlist WHERE
SUBSTRING(FName,1,2) < '00' OR
SUBSTRING(FName,1,2) > '99' OR
FName IS NULL OR
FName LIKE '%<DIR>%'
-- Create a cursor and for each file name do the processing.
-- The files will be processed in date order.
DECLARE curDir CURSOR READ_ONLY LOCAL
FOR
SELECT SUBSTRING(FName,40,40) AS FName
FROM #dirlist
WHERE CAST(SUBSTRING(FName,1,20) AS DATETIME) < @.DelDate
AND SUBSTRING(FName,40,40) LIKE '%.BAK'
ORDER BY CAST(SUBSTRING(FName,1,20) AS DATETIME)
OPEN curDir
FETCH NEXT FROM curDir INTO @.Fname
WHILE (@.@.fetch_status = 0)
BEGIN
-- Delete the old backup files
SET @.Delete = 'DEL "C:\Data\Backups\' + @.FName + '"'
INSERT INTO #Errors (Results)
exec @.Return = master..xp_cmdshell @.Delete
IF @.@.RowCount > 1 OR @.Return = 1
BEGIN
SET @.Error = -1
SET @.Msg = 'Error while Deleting file ' + @.FName
GOTO On_Error
END
PRINT 'Deleted ' + @.FName + ' at ' +
CONVERT(VARCHAR(28),GETDATE(),113)
FETCH NEXT FROM curDir INTO @.Fname
END
CLOSE curDir
DEALLOCATE curDir
DROP TABLE #DirList
DROP TABLE #Errors
RETURN @.Error
On_Error:
BEGIN
IF @.Error <> 0
BEGIN
SELECT @.Msg + '. Error # ' + CAST(@.Error AS VARCHAR(10))
RAISERROR(@.Msg,12,1)
RETURN @.Error
END
END
GO
Andrew J. Kelly SQL MVP
"Sonya" <Sonya@.discussions.microsoft.com> wrote in message
news:041D4B77-15AB-42F8-80B6-AB740408D913@.microsoft.com...
> Andrew or anyone,
> I'm trying to write a short sql stmts to attached to a folder and query
> that
> folder for *.bak and if the*.bak is older than a certain amount of days,
> then
> I want to delete them.
> Is this possible using tsql?
> "Andrew J. Kelly" wrote:
>> Don't use EM, use a script instead. EM has some limitations and things
>> like
>> this can be much more controlled through scripts. The restore syntax is
>> pretty simple and there are examples in BOL. If you have troubles with it
>> post your script and we can help.
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
>> > Andrew,
>> >
>> > Do you know how to access a network drive within Enterprise Mgr?
>> >
>> > I'm trying to restore a db where the backups are located on the
>> > network. I
>> > can see other network drives but cannot view the I need. I've already
>> > checked
>> > the permissions and made sure the drive was shared. Do you have any
>> > suggestions?
>> >
>> > "Andrew J. Kelly" wrote:
>> >
>> >> Just change the CONVERT to use an different format and make the
>> >> variable
>> >> large enough to handle the extra characters. You will have to look at
>> >> CONVERT in BOL to see which suites your requirement best. The only
>> >> thing
>> >> is
>> >> that when you add the time portion you usually have to use a format
>> >> that
>> >> inserts dashs or slashes. See if this works for you:
>> >>
>> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
>> >> GETDATE(),120),'-',''),':',''),' ',''),12)
>> >>
>> >>
>> >> --
>> >> Andrew J. Kelly SQL MVP
>> >>
>> >>
>> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
>> >> > Andrew,
>> >> >
>> >> > Thanks worked. How can I add the time to it as well. I am going to
>> >> > create
>> >> > a
>> >> > DTS package to run this query.
>> >> >
>> >> > "Andrew J. Kelly" wrote:
>> >> >
>> >> >> It would help if you gave a little more background or at least
>> >> >> showed
>> >> >> what
>> >> >> you were doing now. Here is an example of adding a datetime to a
>> >> >> backup
>> >> >> device name but I don't know hot it fits your situation.
>> >> >>
>> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
>> >> >> NVARCHAR(150)
>> >> >>
>> >> >>
>> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
>> >> >> FOR SELECT Catalog_Name
>> >> >> FROM Information_Schema.Schemata
>> >> >> -- WHERE [Catalog_Name] NOT IN
>> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
>> >> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>> >> >>
>> >> >>
>> >> >> OPEN cur_DBs
>> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >>
>> >> >> WHILE @.@.FETCH_STATUS = 0
>> >> >> BEGIN
>> >> >>
>> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
>> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
>> >> >>
>> >> >> SET @.Name = @.DBName + N' Full Backup'
>> >> >>
>> >> >> PRINT 'Backing up database ' + @.DBName
>> >> >>
>> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT ,
>> >> >> NOUNLOAD ,
>> >> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
>> >> >>
>> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
>> >> >>
>> >> >> PRINT '--- '
>> >> >>
>> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >> END
>> >> >>
>> >> >> CLOSE cur_DBs
>> >> >> DEALLOCATE cur_DBs
>> >> >>
>> >> >>
>> >> >> --
>> >> >> Andrew J. Kelly SQL MVP
>> >> >>
>> >> >>
>> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
>> >> >> >I want to add a datetime stamp on my backup file names but what I
>> >> >> >have
>> >> >> >is
>> >> >> >not
>> >> >> > adding to the filename.
>> >> >> >
>> >> >> > Does anyone know how to do this?
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>|||Thanks.. I'm going through the code and I'm running into an error when you
use the directory list command. I'm not able to view the temporary table to
see if data is being stored.
"Andrew J. Kelly" wrote:
> You can't do this strictly with TSQL but this is as close as you will get.
> There is a sample sp that will delete old log file backups based on the
> timestamp in the name of the files.
> -- Removing Older Backup Files --
> -- Remove any log files older than 7 days
> DECLARE @.Error INT, @.D DATETIME
> SET @.D = DATEADD(dd,-7,GETDATE())
> EXEC @.Error = remove_old_log_files @.D
>
> SELECT @.Error
> --
> CREATE PROCEDURE remove_old_log_files
> @.DelDate DATETIME
> AS
> SET NOCOUNT ON
> DECLARE @.SQL VARCHAR(500), @.FName VARCHAR(40), @.Error INT
> DECLARE @.Delete VARCHAR(300), @.Msg VARCHAR(100), @.Return INT
> -- Used later when we cast strings to Datetimes
> SET DATEFORMAT MDY
> -- Create a table to hold the results of the DIR command
> IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
> DROP TABLE #DirList
> CREATE TABLE #dirlist (FName VARCHAR(1000))
> -- Create a table to hold any errors
> IF OBJECT_ID('tempdb..#Errors') IS NOT NULL
> DROP TABLE #Errors
> CREATE TABLE #Errors (Results VARCHAR(1000))
> -- Insert the results of the dir cmd into a table so we can scan it
> INSERT INTO #dirlist (FName)
> exec @.Return = master..xp_cmdshell 'dir /OD C:\Data\Backups\*.Bak'
> SET @.Error = @.@.ERROR
> IF @.Error <> 0 OR @.Return <> 0
> BEGIN
> IF @.Return = 1
> SET @.Error = -1
> SET @.Msg = 'Error while getting the filenames with DIR '
> GOTO On_Error
> END
> -- Remove the garbage
> DELETE #dirlist WHERE
> SUBSTRING(FName,1,2) < '00' OR
> SUBSTRING(FName,1,2) > '99' OR
> FName IS NULL OR
> FName LIKE '%<DIR>%'
>
> -- Create a cursor and for each file name do the processing.
> -- The files will be processed in date order.
> DECLARE curDir CURSOR READ_ONLY LOCAL
> FOR
> SELECT SUBSTRING(FName,40,40) AS FName
> FROM #dirlist
> WHERE CAST(SUBSTRING(FName,1,20) AS DATETIME) < @.DelDate
> AND SUBSTRING(FName,40,40) LIKE '%.BAK'
> ORDER BY CAST(SUBSTRING(FName,1,20) AS DATETIME)
> OPEN curDir
> FETCH NEXT FROM curDir INTO @.Fname
> WHILE (@.@.fetch_status = 0)
> BEGIN
>
> -- Delete the old backup files
> SET @.Delete = 'DEL "C:\Data\Backups\' + @.FName + '"'
> INSERT INTO #Errors (Results)
> exec @.Return = master..xp_cmdshell @.Delete
>
> IF @.@.RowCount > 1 OR @.Return = 1
> BEGIN
> SET @.Error = -1
> SET @.Msg = 'Error while Deleting file ' + @.FName
> GOTO On_Error
> END
> PRINT 'Deleted ' + @.FName + ' at ' +
> CONVERT(VARCHAR(28),GETDATE(),113)
> FETCH NEXT FROM curDir INTO @.Fname
> END
> CLOSE curDir
> DEALLOCATE curDir
> DROP TABLE #DirList
> DROP TABLE #Errors
> RETURN @.Error
> On_Error:
> BEGIN
> IF @.Error <> 0
> BEGIN
> SELECT @.Msg + '. Error # ' + CAST(@.Error AS VARCHAR(10))
> RAISERROR(@.Msg,12,1)
> RETURN @.Error
> END
> END
> GO
>
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:041D4B77-15AB-42F8-80B6-AB740408D913@.microsoft.com...
> > Andrew or anyone,
> >
> > I'm trying to write a short sql stmts to attached to a folder and query
> > that
> > folder for *.bak and if the*.bak is older than a certain amount of days,
> > then
> > I want to delete them.
> >
> > Is this possible using tsql?
> >
> > "Andrew J. Kelly" wrote:
> >
> >> Don't use EM, use a script instead. EM has some limitations and things
> >> like
> >> this can be much more controlled through scripts. The restore syntax is
> >> pretty simple and there are examples in BOL. If you have troubles with it
> >> post your script and we can help.
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
> >> > Andrew,
> >> >
> >> > Do you know how to access a network drive within Enterprise Mgr?
> >> >
> >> > I'm trying to restore a db where the backups are located on the
> >> > network. I
> >> > can see other network drives but cannot view the I need. I've already
> >> > checked
> >> > the permissions and made sure the drive was shared. Do you have any
> >> > suggestions?
> >> >
> >> > "Andrew J. Kelly" wrote:
> >> >
> >> >> Just change the CONVERT to use an different format and make the
> >> >> variable
> >> >> large enough to handle the extra characters. You will have to look at
> >> >> CONVERT in BOL to see which suites your requirement best. The only
> >> >> thing
> >> >> is
> >> >> that when you add the time portion you usually have to use a format
> >> >> that
> >> >> inserts dashs or slashes. See if this works for you:
> >> >>
> >> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
> >> >> GETDATE(),120),'-',''),':',''),' ',''),12)
> >> >>
> >> >>
> >> >> --
> >> >> Andrew J. Kelly SQL MVP
> >> >>
> >> >>
> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
> >> >> > Andrew,
> >> >> >
> >> >> > Thanks worked. How can I add the time to it as well. I am going to
> >> >> > create
> >> >> > a
> >> >> > DTS package to run this query.
> >> >> >
> >> >> > "Andrew J. Kelly" wrote:
> >> >> >
> >> >> >> It would help if you gave a little more background or at least
> >> >> >> showed
> >> >> >> what
> >> >> >> you were doing now. Here is an example of adding a datetime to a
> >> >> >> backup
> >> >> >> device name but I don't know hot it fits your situation.
> >> >> >>
> >> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
> >> >> >> NVARCHAR(150)
> >> >> >>
> >> >> >>
> >> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
> >> >> >> FOR SELECT Catalog_Name
> >> >> >> FROM Information_Schema.Schemata
> >> >> >> -- WHERE [Catalog_Name] NOT IN
> >> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
> >> >> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
> >> >> >>
> >> >> >>
> >> >> >> OPEN cur_DBs
> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >> >>
> >> >> >> WHILE @.@.FETCH_STATUS = 0
> >> >> >> BEGIN
> >> >> >>
> >> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> >> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) + N'.BAK'
> >> >> >>
> >> >> >> SET @.Name = @.DBName + N' Full Backup'
> >> >> >>
> >> >> >> PRINT 'Backing up database ' + @.DBName
> >> >> >>
> >> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT ,
> >> >> >> NOUNLOAD ,
> >> >> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> >> >> >>
> >> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> >> >> >>
> >> >> >> PRINT '--- '
> >> >> >>
> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >> >> END
> >> >> >>
> >> >> >> CLOSE cur_DBs
> >> >> >> DEALLOCATE cur_DBs
> >> >> >>
> >> >> >>
> >> >> >> --
> >> >> >> Andrew J. Kelly SQL MVP
> >> >> >>
> >> >> >>
> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >> >> >> >I want to add a datetime stamp on my backup file names but what I
> >> >> >> >have
> >> >> >> >is
> >> >> >> >not
> >> >> >> > adding to the filename.
> >> >> >> >
> >> >> >> > Does anyone know how to do this?
> >> >> >>
> >> >> >>
> >> >> >>
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>|||Do you mean this line: INSERT INTO #dirlist (FName)
exec @.Return = master..xp_cmdshell 'dir /OD C:\Data\Backups\*.Bak'
Can you be so kind as to list exactly what the error is that you are
getting? I don't understand why you can't see this table? Are you running
the select in the same connection as the one that created the table? If not
you won't be able to see it since it is a local temporary table. You really
need to be a little more specific on what you are doing and what the real
errors are that you are getting otherwise I am simply guessing.
--
Andrew J. Kelly SQL MVP
"Sonya" <Sonya@.discussions.microsoft.com> wrote in message
news:61C4CAE3-8398-4905-AE4C-411D6A55DA12@.microsoft.com...
> Thanks.. I'm going through the code and I'm running into an error when you
> use the directory list command. I'm not able to view the temporary table
> to
> see if data is being stored.
> "Andrew J. Kelly" wrote:
>> You can't do this strictly with TSQL but this is as close as you will
>> get.
>> There is a sample sp that will delete old log file backups based on the
>> timestamp in the name of the files.
>> -- Removing Older Backup Files --
>> -- Remove any log files older than 7 days
>> DECLARE @.Error INT, @.D DATETIME
>> SET @.D = DATEADD(dd,-7,GETDATE())
>> EXEC @.Error = remove_old_log_files @.D
>>
>> SELECT @.Error
>> --
>> CREATE PROCEDURE remove_old_log_files
>> @.DelDate DATETIME
>> AS
>> SET NOCOUNT ON
>> DECLARE @.SQL VARCHAR(500), @.FName VARCHAR(40), @.Error INT
>> DECLARE @.Delete VARCHAR(300), @.Msg VARCHAR(100), @.Return INT
>> -- Used later when we cast strings to Datetimes
>> SET DATEFORMAT MDY
>> -- Create a table to hold the results of the DIR command
>> IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
>> DROP TABLE #DirList
>> CREATE TABLE #dirlist (FName VARCHAR(1000))
>> -- Create a table to hold any errors
>> IF OBJECT_ID('tempdb..#Errors') IS NOT NULL
>> DROP TABLE #Errors
>> CREATE TABLE #Errors (Results VARCHAR(1000))
>> -- Insert the results of the dir cmd into a table so we can scan it
>> INSERT INTO #dirlist (FName)
>> exec @.Return = master..xp_cmdshell 'dir /OD
>> C:\Data\Backups\*.Bak'
>> SET @.Error = @.@.ERROR
>> IF @.Error <> 0 OR @.Return <> 0
>> BEGIN
>> IF @.Return = 1
>> SET @.Error = -1
>> SET @.Msg = 'Error while getting the filenames with DIR '
>> GOTO On_Error
>> END
>> -- Remove the garbage
>> DELETE #dirlist WHERE
>> SUBSTRING(FName,1,2) < '00' OR
>> SUBSTRING(FName,1,2) > '99' OR
>> FName IS NULL OR
>> FName LIKE '%<DIR>%'
>>
>> -- Create a cursor and for each file name do the processing.
>> -- The files will be processed in date order.
>> DECLARE curDir CURSOR READ_ONLY LOCAL
>> FOR
>> SELECT SUBSTRING(FName,40,40) AS FName
>> FROM #dirlist
>> WHERE CAST(SUBSTRING(FName,1,20) AS DATETIME) < @.DelDate
>> AND SUBSTRING(FName,40,40) LIKE '%.BAK'
>> ORDER BY CAST(SUBSTRING(FName,1,20) AS DATETIME)
>> OPEN curDir
>> FETCH NEXT FROM curDir INTO @.Fname
>> WHILE (@.@.fetch_status = 0)
>> BEGIN
>>
>> -- Delete the old backup files
>> SET @.Delete = 'DEL "C:\Data\Backups\' + @.FName + '"'
>> INSERT INTO #Errors (Results)
>> exec @.Return = master..xp_cmdshell @.Delete
>>
>> IF @.@.RowCount > 1 OR @.Return = 1
>> BEGIN
>> SET @.Error = -1
>> SET @.Msg = 'Error while Deleting file ' + @.FName
>> GOTO On_Error
>> END
>> PRINT 'Deleted ' + @.FName + ' at ' +
>> CONVERT(VARCHAR(28),GETDATE(),113)
>> FETCH NEXT FROM curDir INTO @.Fname
>> END
>> CLOSE curDir
>> DEALLOCATE curDir
>> DROP TABLE #DirList
>> DROP TABLE #Errors
>> RETURN @.Error
>> On_Error:
>> BEGIN
>> IF @.Error <> 0
>> BEGIN
>> SELECT @.Msg + '. Error # ' + CAST(@.Error AS VARCHAR(10))
>> RAISERROR(@.Msg,12,1)
>> RETURN @.Error
>> END
>> END
>> GO
>>
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> news:041D4B77-15AB-42F8-80B6-AB740408D913@.microsoft.com...
>> > Andrew or anyone,
>> >
>> > I'm trying to write a short sql stmts to attached to a folder and query
>> > that
>> > folder for *.bak and if the*.bak is older than a certain amount of
>> > days,
>> > then
>> > I want to delete them.
>> >
>> > Is this possible using tsql?
>> >
>> > "Andrew J. Kelly" wrote:
>> >
>> >> Don't use EM, use a script instead. EM has some limitations and
>> >> things
>> >> like
>> >> this can be much more controlled through scripts. The restore syntax
>> >> is
>> >> pretty simple and there are examples in BOL. If you have troubles with
>> >> it
>> >> post your script and we can help.
>> >>
>> >> --
>> >> Andrew J. Kelly SQL MVP
>> >>
>> >>
>> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
>> >> > Andrew,
>> >> >
>> >> > Do you know how to access a network drive within Enterprise Mgr?
>> >> >
>> >> > I'm trying to restore a db where the backups are located on the
>> >> > network. I
>> >> > can see other network drives but cannot view the I need. I've
>> >> > already
>> >> > checked
>> >> > the permissions and made sure the drive was shared. Do you have any
>> >> > suggestions?
>> >> >
>> >> > "Andrew J. Kelly" wrote:
>> >> >
>> >> >> Just change the CONVERT to use an different format and make the
>> >> >> variable
>> >> >> large enough to handle the extra characters. You will have to look
>> >> >> at
>> >> >> CONVERT in BOL to see which suites your requirement best. The only
>> >> >> thing
>> >> >> is
>> >> >> that when you add the time portion you usually have to use a format
>> >> >> that
>> >> >> inserts dashs or slashes. See if this works for you:
>> >> >>
>> >> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
>> >> >> GETDATE(),120),'-',''),':',''),' ',''),12)
>> >> >>
>> >> >>
>> >> >> --
>> >> >> Andrew J. Kelly SQL MVP
>> >> >>
>> >> >>
>> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
>> >> >> > Andrew,
>> >> >> >
>> >> >> > Thanks worked. How can I add the time to it as well. I am going
>> >> >> > to
>> >> >> > create
>> >> >> > a
>> >> >> > DTS package to run this query.
>> >> >> >
>> >> >> > "Andrew J. Kelly" wrote:
>> >> >> >
>> >> >> >> It would help if you gave a little more background or at least
>> >> >> >> showed
>> >> >> >> what
>> >> >> >> you were doing now. Here is an example of adding a datetime to
>> >> >> >> a
>> >> >> >> backup
>> >> >> >> device name but I don't know hot it fits your situation.
>> >> >> >>
>> >> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
>> >> >> >> NVARCHAR(150)
>> >> >> >>
>> >> >> >>
>> >> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
>> >> >> >> FOR SELECT Catalog_Name
>> >> >> >> FROM Information_Schema.Schemata
>> >> >> >> -- WHERE [Catalog_Name] NOT IN
>> >> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
>> >> >> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>> >> >> >>
>> >> >> >>
>> >> >> >> OPEN cur_DBs
>> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >> >>
>> >> >> >> WHILE @.@.FETCH_STATUS = 0
>> >> >> >> BEGIN
>> >> >> >>
>> >> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
>> >> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) +
>> >> >> >> N'.BAK'
>> >> >> >>
>> >> >> >> SET @.Name = @.DBName + N' Full Backup'
>> >> >> >>
>> >> >> >> PRINT 'Backing up database ' + @.DBName
>> >> >> >>
>> >> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT ,
>> >> >> >> NOUNLOAD ,
>> >> >> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
>> >> >> >>
>> >> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
>> >> >> >>
>> >> >> >> PRINT '--- '
>> >> >> >>
>> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >> >> END
>> >> >> >>
>> >> >> >> CLOSE cur_DBs
>> >> >> >> DEALLOCATE cur_DBs
>> >> >> >>
>> >> >> >>
>> >> >> >> --
>> >> >> >> Andrew J. Kelly SQL MVP
>> >> >> >>
>> >> >> >>
>> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
>> >> >> >> >I want to add a datetime stamp on my backup file names but what
>> >> >> >> >I
>> >> >> >> >have
>> >> >> >> >is
>> >> >> >> >not
>> >> >> >> > adding to the filename.
>> >> >> >> >
>> >> >> >> > Does anyone know how to do this?
>> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>|||When I try to query the temp table within the same connection, it says the
object is invalid. So I'm not sure the script is working because I can't view
the table.
I don't have any more specific errors. Sorry if I was vague in my previous
messages.
"Andrew J. Kelly" wrote:
> Do you mean this line: INSERT INTO #dirlist (FName)
> exec @.Return = master..xp_cmdshell 'dir /OD C:\Data\Backups\*.Bak'
> Can you be so kind as to list exactly what the error is that you are
> getting? I don't understand why you can't see this table? Are you running
> the select in the same connection as the one that created the table? If not
> you won't be able to see it since it is a local temporary table. You really
> need to be a little more specific on what you are doing and what the real
> errors are that you are getting otherwise I am simply guessing.
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:61C4CAE3-8398-4905-AE4C-411D6A55DA12@.microsoft.com...
> > Thanks.. I'm going through the code and I'm running into an error when you
> > use the directory list command. I'm not able to view the temporary table
> > to
> > see if data is being stored.
> >
> > "Andrew J. Kelly" wrote:
> >
> >> You can't do this strictly with TSQL but this is as close as you will
> >> get.
> >> There is a sample sp that will delete old log file backups based on the
> >> timestamp in the name of the files.
> >>
> >> -- Removing Older Backup Files --
> >>
> >> -- Remove any log files older than 7 days
> >> DECLARE @.Error INT, @.D DATETIME
> >> SET @.D = DATEADD(dd,-7,GETDATE())
> >>
> >> EXEC @.Error = remove_old_log_files @.D
> >>
> >>
> >> SELECT @.Error
> >>
> >> --
> >> CREATE PROCEDURE remove_old_log_files
> >> @.DelDate DATETIME
> >>
> >> AS
> >>
> >> SET NOCOUNT ON
> >>
> >> DECLARE @.SQL VARCHAR(500), @.FName VARCHAR(40), @.Error INT
> >> DECLARE @.Delete VARCHAR(300), @.Msg VARCHAR(100), @.Return INT
> >>
> >> -- Used later when we cast strings to Datetimes
> >> SET DATEFORMAT MDY
> >>
> >> -- Create a table to hold the results of the DIR command
> >> IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
> >> DROP TABLE #DirList
> >>
> >> CREATE TABLE #dirlist (FName VARCHAR(1000))
> >>
> >> -- Create a table to hold any errors
> >> IF OBJECT_ID('tempdb..#Errors') IS NOT NULL
> >> DROP TABLE #Errors
> >>
> >> CREATE TABLE #Errors (Results VARCHAR(1000))
> >>
> >> -- Insert the results of the dir cmd into a table so we can scan it
> >> INSERT INTO #dirlist (FName)
> >> exec @.Return = master..xp_cmdshell 'dir /OD
> >> C:\Data\Backups\*.Bak'
> >>
> >> SET @.Error = @.@.ERROR
> >>
> >> IF @.Error <> 0 OR @.Return <> 0
> >> BEGIN
> >> IF @.Return = 1
> >> SET @.Error = -1
> >> SET @.Msg = 'Error while getting the filenames with DIR '
> >> GOTO On_Error
> >> END
> >>
> >> -- Remove the garbage
> >> DELETE #dirlist WHERE
> >> SUBSTRING(FName,1,2) < '00' OR
> >> SUBSTRING(FName,1,2) > '99' OR
> >> FName IS NULL OR
> >> FName LIKE '%<DIR>%'
> >>
> >>
> >> -- Create a cursor and for each file name do the processing.
> >> -- The files will be processed in date order.
> >> DECLARE curDir CURSOR READ_ONLY LOCAL
> >> FOR
> >> SELECT SUBSTRING(FName,40,40) AS FName
> >> FROM #dirlist
> >> WHERE CAST(SUBSTRING(FName,1,20) AS DATETIME) < @.DelDate
> >> AND SUBSTRING(FName,40,40) LIKE '%.BAK'
> >> ORDER BY CAST(SUBSTRING(FName,1,20) AS DATETIME)
> >>
> >> OPEN curDir
> >>
> >> FETCH NEXT FROM curDir INTO @.Fname
> >> WHILE (@.@.fetch_status = 0)
> >> BEGIN
> >>
> >>
> >> -- Delete the old backup files
> >> SET @.Delete = 'DEL "C:\Data\Backups\' + @.FName + '"'
> >>
> >> INSERT INTO #Errors (Results)
> >> exec @.Return = master..xp_cmdshell @.Delete
> >>
> >>
> >> IF @.@.RowCount > 1 OR @.Return = 1
> >> BEGIN
> >> SET @.Error = -1
> >> SET @.Msg = 'Error while Deleting file ' + @.FName
> >> GOTO On_Error
> >> END
> >>
> >> PRINT 'Deleted ' + @.FName + ' at ' +
> >> CONVERT(VARCHAR(28),GETDATE(),113)
> >>
> >> FETCH NEXT FROM curDir INTO @.Fname
> >> END
> >>
> >> CLOSE curDir
> >> DEALLOCATE curDir
> >>
> >> DROP TABLE #DirList
> >> DROP TABLE #Errors
> >>
> >> RETURN @.Error
> >>
> >> On_Error:
> >> BEGIN
> >> IF @.Error <> 0
> >> BEGIN
> >> SELECT @.Msg + '. Error # ' + CAST(@.Error AS VARCHAR(10))
> >> RAISERROR(@.Msg,12,1)
> >> RETURN @.Error
> >> END
> >> END
> >> GO
> >>
> >>
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> news:041D4B77-15AB-42F8-80B6-AB740408D913@.microsoft.com...
> >> > Andrew or anyone,
> >> >
> >> > I'm trying to write a short sql stmts to attached to a folder and query
> >> > that
> >> > folder for *.bak and if the*.bak is older than a certain amount of
> >> > days,
> >> > then
> >> > I want to delete them.
> >> >
> >> > Is this possible using tsql?
> >> >
> >> > "Andrew J. Kelly" wrote:
> >> >
> >> >> Don't use EM, use a script instead. EM has some limitations and
> >> >> things
> >> >> like
> >> >> this can be much more controlled through scripts. The restore syntax
> >> >> is
> >> >> pretty simple and there are examples in BOL. If you have troubles with
> >> >> it
> >> >> post your script and we can help.
> >> >>
> >> >> --
> >> >> Andrew J. Kelly SQL MVP
> >> >>
> >> >>
> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
> >> >> > Andrew,
> >> >> >
> >> >> > Do you know how to access a network drive within Enterprise Mgr?
> >> >> >
> >> >> > I'm trying to restore a db where the backups are located on the
> >> >> > network. I
> >> >> > can see other network drives but cannot view the I need. I've
> >> >> > already
> >> >> > checked
> >> >> > the permissions and made sure the drive was shared. Do you have any
> >> >> > suggestions?
> >> >> >
> >> >> > "Andrew J. Kelly" wrote:
> >> >> >
> >> >> >> Just change the CONVERT to use an different format and make the
> >> >> >> variable
> >> >> >> large enough to handle the extra characters. You will have to look
> >> >> >> at
> >> >> >> CONVERT in BOL to see which suites your requirement best. The only
> >> >> >> thing
> >> >> >> is
> >> >> >> that when you add the time portion you usually have to use a format
> >> >> >> that
> >> >> >> inserts dashs or slashes. See if this works for you:
> >> >> >>
> >> >> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
> >> >> >> GETDATE(),120),'-',''),':',''),' ',''),12)
> >> >> >>
> >> >> >>
> >> >> >> --
> >> >> >> Andrew J. Kelly SQL MVP
> >> >> >>
> >> >> >>
> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
> >> >> >> > Andrew,
> >> >> >> >
> >> >> >> > Thanks worked. How can I add the time to it as well. I am going
> >> >> >> > to
> >> >> >> > create
> >> >> >> > a
> >> >> >> > DTS package to run this query.
> >> >> >> >
> >> >> >> > "Andrew J. Kelly" wrote:
> >> >> >> >
> >> >> >> >> It would help if you gave a little more background or at least
> >> >> >> >> showed
> >> >> >> >> what
> >> >> >> >> you were doing now. Here is an example of adding a datetime to
> >> >> >> >> a
> >> >> >> >> backup
> >> >> >> >> device name but I don't know hot it fits your situation.
> >> >> >> >>
> >> >> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
> >> >> >> >> NVARCHAR(150)
> >> >> >> >>
> >> >> >> >>
> >> >> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
> >> >> >> >> FOR SELECT Catalog_Name
> >> >> >> >> FROM Information_Schema.Schemata
> >> >> >> >> -- WHERE [Catalog_Name] NOT IN
> >> >> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
> >> >> >> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
> >> >> >> >>
> >> >> >> >>
> >> >> >> >> OPEN cur_DBs
> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >> >> >>
> >> >> >> >> WHILE @.@.FETCH_STATUS = 0
> >> >> >> >> BEGIN
> >> >> >> >>
> >> >> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_' +
> >> >> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) +
> >> >> >> >> N'.BAK'
> >> >> >> >>
> >> >> >> >> SET @.Name = @.DBName + N' Full Backup'
> >> >> >> >>
> >> >> >> >> PRINT 'Backing up database ' + @.DBName
> >> >> >> >>
> >> >> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT ,
> >> >> >> >> NOUNLOAD ,
> >> >> >> >> NAME = @.Name, NOSKIP , STATS = 10, NOFORMAT
> >> >> >> >>
> >> >> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> >> >> >> >>
> >> >> >> >> PRINT '--- '
> >> >> >> >>
> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >> >> >> END
> >> >> >> >>
> >> >> >> >> CLOSE cur_DBs
> >> >> >> >> DEALLOCATE cur_DBs
> >> >> >> >>
> >> >> >> >>
> >> >> >> >> --
> >> >> >> >> Andrew J. Kelly SQL MVP
> >> >> >> >>
> >> >> >> >>
> >> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >> >> >> >> >I want to add a datetime stamp on my backup file names but what
> >> >> >> >> >I
> >> >> >> >> >have
> >> >> >> >> >is
> >> >> >> >> >not
> >> >> >> >> > adding to the filename.
> >> >> >> >> >
> >> >> >> >> > Does anyone know how to do this?
> >> >> >> >>
> >> >> >> >>
> >> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>|||Sonya,
The temp table is created inside of a stored procedure. If you run the sp
and it creates a temp table it only lives for the duration of the sp
execution. So by the time you query for it the table has been destroyed.
This is how local temp tables work and is the whole purpose of them. If you
wan to play around with it you need to remove the code from the sp and run
it as a standard batch in query analyzer.
--
Andrew J. Kelly SQL MVP
"Sonya" <Sonya@.discussions.microsoft.com> wrote in message
news:D7770A92-6EEA-4B7D-8C9C-6A9DFBC92046@.microsoft.com...
> When I try to query the temp table within the same connection, it says the
> object is invalid. So I'm not sure the script is working because I can't
> view
> the table.
> I don't have any more specific errors. Sorry if I was vague in my previous
> messages.
> "Andrew J. Kelly" wrote:
>> Do you mean this line: INSERT INTO #dirlist (FName)
>> exec @.Return = master..xp_cmdshell 'dir /OD
>> C:\Data\Backups\*.Bak'
>> Can you be so kind as to list exactly what the error is that you are
>> getting? I don't understand why you can't see this table? Are you
>> running
>> the select in the same connection as the one that created the table? If
>> not
>> you won't be able to see it since it is a local temporary table. You
>> really
>> need to be a little more specific on what you are doing and what the real
>> errors are that you are getting otherwise I am simply guessing.
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> news:61C4CAE3-8398-4905-AE4C-411D6A55DA12@.microsoft.com...
>> > Thanks.. I'm going through the code and I'm running into an error when
>> > you
>> > use the directory list command. I'm not able to view the temporary
>> > table
>> > to
>> > see if data is being stored.
>> >
>> > "Andrew J. Kelly" wrote:
>> >
>> >> You can't do this strictly with TSQL but this is as close as you will
>> >> get.
>> >> There is a sample sp that will delete old log file backups based on
>> >> the
>> >> timestamp in the name of the files.
>> >>
>> >> -- Removing Older Backup Files --
>> >>
>> >> -- Remove any log files older than 7 days
>> >> DECLARE @.Error INT, @.D DATETIME
>> >> SET @.D = DATEADD(dd,-7,GETDATE())
>> >>
>> >> EXEC @.Error = remove_old_log_files @.D
>> >>
>> >>
>> >> SELECT @.Error
>> >>
>> >> --
>> >> CREATE PROCEDURE remove_old_log_files
>> >> @.DelDate DATETIME
>> >>
>> >> AS
>> >>
>> >> SET NOCOUNT ON
>> >>
>> >> DECLARE @.SQL VARCHAR(500), @.FName VARCHAR(40), @.Error INT
>> >> DECLARE @.Delete VARCHAR(300), @.Msg VARCHAR(100), @.Return INT
>> >>
>> >> -- Used later when we cast strings to Datetimes
>> >> SET DATEFORMAT MDY
>> >>
>> >> -- Create a table to hold the results of the DIR command
>> >> IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
>> >> DROP TABLE #DirList
>> >>
>> >> CREATE TABLE #dirlist (FName VARCHAR(1000))
>> >>
>> >> -- Create a table to hold any errors
>> >> IF OBJECT_ID('tempdb..#Errors') IS NOT NULL
>> >> DROP TABLE #Errors
>> >>
>> >> CREATE TABLE #Errors (Results VARCHAR(1000))
>> >>
>> >> -- Insert the results of the dir cmd into a table so we can scan it
>> >> INSERT INTO #dirlist (FName)
>> >> exec @.Return = master..xp_cmdshell 'dir /OD
>> >> C:\Data\Backups\*.Bak'
>> >>
>> >> SET @.Error = @.@.ERROR
>> >>
>> >> IF @.Error <> 0 OR @.Return <> 0
>> >> BEGIN
>> >> IF @.Return = 1
>> >> SET @.Error = -1
>> >> SET @.Msg = 'Error while getting the filenames with DIR '
>> >> GOTO On_Error
>> >> END
>> >>
>> >> -- Remove the garbage
>> >> DELETE #dirlist WHERE
>> >> SUBSTRING(FName,1,2) < '00' OR
>> >> SUBSTRING(FName,1,2) > '99' OR
>> >> FName IS NULL OR
>> >> FName LIKE '%<DIR>%'
>> >>
>> >>
>> >> -- Create a cursor and for each file name do the processing.
>> >> -- The files will be processed in date order.
>> >> DECLARE curDir CURSOR READ_ONLY LOCAL
>> >> FOR
>> >> SELECT SUBSTRING(FName,40,40) AS FName
>> >> FROM #dirlist
>> >> WHERE CAST(SUBSTRING(FName,1,20) AS DATETIME) < @.DelDate
>> >> AND SUBSTRING(FName,40,40) LIKE '%.BAK'
>> >> ORDER BY CAST(SUBSTRING(FName,1,20) AS DATETIME)
>> >>
>> >> OPEN curDir
>> >>
>> >> FETCH NEXT FROM curDir INTO @.Fname
>> >> WHILE (@.@.fetch_status = 0)
>> >> BEGIN
>> >>
>> >>
>> >> -- Delete the old backup files
>> >> SET @.Delete = 'DEL "C:\Data\Backups\' + @.FName + '"'
>> >>
>> >> INSERT INTO #Errors (Results)
>> >> exec @.Return = master..xp_cmdshell @.Delete
>> >>
>> >>
>> >> IF @.@.RowCount > 1 OR @.Return = 1
>> >> BEGIN
>> >> SET @.Error = -1
>> >> SET @.Msg = 'Error while Deleting file ' + @.FName
>> >> GOTO On_Error
>> >> END
>> >>
>> >> PRINT 'Deleted ' + @.FName + ' at ' +
>> >> CONVERT(VARCHAR(28),GETDATE(),113)
>> >>
>> >> FETCH NEXT FROM curDir INTO @.Fname
>> >> END
>> >>
>> >> CLOSE curDir
>> >> DEALLOCATE curDir
>> >>
>> >> DROP TABLE #DirList
>> >> DROP TABLE #Errors
>> >>
>> >> RETURN @.Error
>> >>
>> >> On_Error:
>> >> BEGIN
>> >> IF @.Error <> 0
>> >> BEGIN
>> >> SELECT @.Msg + '. Error # ' + CAST(@.Error AS VARCHAR(10))
>> >> RAISERROR(@.Msg,12,1)
>> >> RETURN @.Error
>> >> END
>> >> END
>> >> GO
>> >>
>> >>
>> >>
>> >> --
>> >> Andrew J. Kelly SQL MVP
>> >>
>> >>
>> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> news:041D4B77-15AB-42F8-80B6-AB740408D913@.microsoft.com...
>> >> > Andrew or anyone,
>> >> >
>> >> > I'm trying to write a short sql stmts to attached to a folder and
>> >> > query
>> >> > that
>> >> > folder for *.bak and if the*.bak is older than a certain amount of
>> >> > days,
>> >> > then
>> >> > I want to delete them.
>> >> >
>> >> > Is this possible using tsql?
>> >> >
>> >> > "Andrew J. Kelly" wrote:
>> >> >
>> >> >> Don't use EM, use a script instead. EM has some limitations and
>> >> >> things
>> >> >> like
>> >> >> this can be much more controlled through scripts. The restore
>> >> >> syntax
>> >> >> is
>> >> >> pretty simple and there are examples in BOL. If you have troubles
>> >> >> with
>> >> >> it
>> >> >> post your script and we can help.
>> >> >>
>> >> >> --
>> >> >> Andrew J. Kelly SQL MVP
>> >> >>
>> >> >>
>> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
>> >> >> > Andrew,
>> >> >> >
>> >> >> > Do you know how to access a network drive within Enterprise Mgr?
>> >> >> >
>> >> >> > I'm trying to restore a db where the backups are located on the
>> >> >> > network. I
>> >> >> > can see other network drives but cannot view the I need. I've
>> >> >> > already
>> >> >> > checked
>> >> >> > the permissions and made sure the drive was shared. Do you have
>> >> >> > any
>> >> >> > suggestions?
>> >> >> >
>> >> >> > "Andrew J. Kelly" wrote:
>> >> >> >
>> >> >> >> Just change the CONVERT to use an different format and make the
>> >> >> >> variable
>> >> >> >> large enough to handle the extra characters. You will have to
>> >> >> >> look
>> >> >> >> at
>> >> >> >> CONVERT in BOL to see which suites your requirement best. The
>> >> >> >> only
>> >> >> >> thing
>> >> >> >> is
>> >> >> >> that when you add the time portion you usually have to use a
>> >> >> >> format
>> >> >> >> that
>> >> >> >> inserts dashs or slashes. See if this works for you:
>> >> >> >>
>> >> >> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
>> >> >> >> GETDATE(),120),'-',''),':',''),' ',''),12)
>> >> >> >>
>> >> >> >>
>> >> >> >> --
>> >> >> >> Andrew J. Kelly SQL MVP
>> >> >> >>
>> >> >> >>
>> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
>> >> >> >> > Andrew,
>> >> >> >> >
>> >> >> >> > Thanks worked. How can I add the time to it as well. I am
>> >> >> >> > going
>> >> >> >> > to
>> >> >> >> > create
>> >> >> >> > a
>> >> >> >> > DTS package to run this query.
>> >> >> >> >
>> >> >> >> > "Andrew J. Kelly" wrote:
>> >> >> >> >
>> >> >> >> >> It would help if you gave a little more background or at
>> >> >> >> >> least
>> >> >> >> >> showed
>> >> >> >> >> what
>> >> >> >> >> you were doing now. Here is an example of adding a datetime
>> >> >> >> >> to
>> >> >> >> >> a
>> >> >> >> >> backup
>> >> >> >> >> device name but I don't know hot it fits your situation.
>> >> >> >> >>
>> >> >> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
>> >> >> >> >> NVARCHAR(150)
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
>> >> >> >> >> FOR SELECT Catalog_Name
>> >> >> >> >> FROM Information_Schema.Schemata
>> >> >> >> >> -- WHERE [Catalog_Name] NOT IN
>> >> >> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
>> >> >> >> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >> OPEN cur_DBs
>> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >> >> >>
>> >> >> >> >> WHILE @.@.FETCH_STATUS = 0
>> >> >> >> >> BEGIN
>> >> >> >> >>
>> >> >> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_'
>> >> >> >> >> +
>> >> >> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) +
>> >> >> >> >> N'.BAK'
>> >> >> >> >>
>> >> >> >> >> SET @.Name = @.DBName + N' Full Backup'
>> >> >> >> >>
>> >> >> >> >> PRINT 'Backing up database ' + @.DBName
>> >> >> >> >>
>> >> >> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT ,
>> >> >> >> >> NOUNLOAD ,
>> >> >> >> >> NAME = @.Name, NOSKIP , STATS = 10,
>> >> >> >> >> NOFORMAT
>> >> >> >> >>
>> >> >> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
>> >> >> >> >>
>> >> >> >> >> PRINT '--- '
>> >> >> >> >>
>> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >> >> >> END
>> >> >> >> >>
>> >> >> >> >> CLOSE cur_DBs
>> >> >> >> >> DEALLOCATE cur_DBs
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >> --
>> >> >> >> >> Andrew J. Kelly SQL MVP
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
>> >> >> >> >> >I want to add a datetime stamp on my backup file names but
>> >> >> >> >> >what
>> >> >> >> >> >I
>> >> >> >> >> >have
>> >> >> >> >> >is
>> >> >> >> >> >not
>> >> >> >> >> > adding to the filename.
>> >> >> >> >> >
>> >> >> >> >> > Does anyone know how to do this?
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>|||Ok, I was doing that as well. I'll have to remove or reference the return
function differently. Thank you for all of your help. The message that I
received is the following:
Server: Msg 178, Level 15, State 1, Line 92
A RETURN statement with a return value cannot be used in this context.
Server: Msg 178, Level 15, State 1, Line 99
A RETURN statement with a return value cannot be used in this context.
"Andrew J. Kelly" wrote:
> Sonya,
> The temp table is created inside of a stored procedure. If you run the sp
> and it creates a temp table it only lives for the duration of the sp
> execution. So by the time you query for it the table has been destroyed.
> This is how local temp tables work and is the whole purpose of them. If you
> wan to play around with it you need to remove the code from the sp and run
> it as a standard batch in query analyzer.
> --
> Andrew J. Kelly SQL MVP
>
> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> news:D7770A92-6EEA-4B7D-8C9C-6A9DFBC92046@.microsoft.com...
> > When I try to query the temp table within the same connection, it says the
> > object is invalid. So I'm not sure the script is working because I can't
> > view
> > the table.
> >
> > I don't have any more specific errors. Sorry if I was vague in my previous
> > messages.
> > "Andrew J. Kelly" wrote:
> >
> >> Do you mean this line: INSERT INTO #dirlist (FName)
> >> exec @.Return = master..xp_cmdshell 'dir /OD
> >> C:\Data\Backups\*.Bak'
> >>
> >> Can you be so kind as to list exactly what the error is that you are
> >> getting? I don't understand why you can't see this table? Are you
> >> running
> >> the select in the same connection as the one that created the table? If
> >> not
> >> you won't be able to see it since it is a local temporary table. You
> >> really
> >> need to be a little more specific on what you are doing and what the real
> >> errors are that you are getting otherwise I am simply guessing.
> >>
> >> --
> >> Andrew J. Kelly SQL MVP
> >>
> >>
> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> news:61C4CAE3-8398-4905-AE4C-411D6A55DA12@.microsoft.com...
> >> > Thanks.. I'm going through the code and I'm running into an error when
> >> > you
> >> > use the directory list command. I'm not able to view the temporary
> >> > table
> >> > to
> >> > see if data is being stored.
> >> >
> >> > "Andrew J. Kelly" wrote:
> >> >
> >> >> You can't do this strictly with TSQL but this is as close as you will
> >> >> get.
> >> >> There is a sample sp that will delete old log file backups based on
> >> >> the
> >> >> timestamp in the name of the files.
> >> >>
> >> >> -- Removing Older Backup Files --
> >> >>
> >> >> -- Remove any log files older than 7 days
> >> >> DECLARE @.Error INT, @.D DATETIME
> >> >> SET @.D = DATEADD(dd,-7,GETDATE())
> >> >>
> >> >> EXEC @.Error = remove_old_log_files @.D
> >> >>
> >> >>
> >> >> SELECT @.Error
> >> >>
> >> >> --
> >> >> CREATE PROCEDURE remove_old_log_files
> >> >> @.DelDate DATETIME
> >> >>
> >> >> AS
> >> >>
> >> >> SET NOCOUNT ON
> >> >>
> >> >> DECLARE @.SQL VARCHAR(500), @.FName VARCHAR(40), @.Error INT
> >> >> DECLARE @.Delete VARCHAR(300), @.Msg VARCHAR(100), @.Return INT
> >> >>
> >> >> -- Used later when we cast strings to Datetimes
> >> >> SET DATEFORMAT MDY
> >> >>
> >> >> -- Create a table to hold the results of the DIR command
> >> >> IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
> >> >> DROP TABLE #DirList
> >> >>
> >> >> CREATE TABLE #dirlist (FName VARCHAR(1000))
> >> >>
> >> >> -- Create a table to hold any errors
> >> >> IF OBJECT_ID('tempdb..#Errors') IS NOT NULL
> >> >> DROP TABLE #Errors
> >> >>
> >> >> CREATE TABLE #Errors (Results VARCHAR(1000))
> >> >>
> >> >> -- Insert the results of the dir cmd into a table so we can scan it
> >> >> INSERT INTO #dirlist (FName)
> >> >> exec @.Return = master..xp_cmdshell 'dir /OD
> >> >> C:\Data\Backups\*.Bak'
> >> >>
> >> >> SET @.Error = @.@.ERROR
> >> >>
> >> >> IF @.Error <> 0 OR @.Return <> 0
> >> >> BEGIN
> >> >> IF @.Return = 1
> >> >> SET @.Error = -1
> >> >> SET @.Msg = 'Error while getting the filenames with DIR '
> >> >> GOTO On_Error
> >> >> END
> >> >>
> >> >> -- Remove the garbage
> >> >> DELETE #dirlist WHERE
> >> >> SUBSTRING(FName,1,2) < '00' OR
> >> >> SUBSTRING(FName,1,2) > '99' OR
> >> >> FName IS NULL OR
> >> >> FName LIKE '%<DIR>%'
> >> >>
> >> >>
> >> >> -- Create a cursor and for each file name do the processing.
> >> >> -- The files will be processed in date order.
> >> >> DECLARE curDir CURSOR READ_ONLY LOCAL
> >> >> FOR
> >> >> SELECT SUBSTRING(FName,40,40) AS FName
> >> >> FROM #dirlist
> >> >> WHERE CAST(SUBSTRING(FName,1,20) AS DATETIME) < @.DelDate
> >> >> AND SUBSTRING(FName,40,40) LIKE '%.BAK'
> >> >> ORDER BY CAST(SUBSTRING(FName,1,20) AS DATETIME)
> >> >>
> >> >> OPEN curDir
> >> >>
> >> >> FETCH NEXT FROM curDir INTO @.Fname
> >> >> WHILE (@.@.fetch_status = 0)
> >> >> BEGIN
> >> >>
> >> >>
> >> >> -- Delete the old backup files
> >> >> SET @.Delete = 'DEL "C:\Data\Backups\' + @.FName + '"'
> >> >>
> >> >> INSERT INTO #Errors (Results)
> >> >> exec @.Return = master..xp_cmdshell @.Delete
> >> >>
> >> >>
> >> >> IF @.@.RowCount > 1 OR @.Return = 1
> >> >> BEGIN
> >> >> SET @.Error = -1
> >> >> SET @.Msg = 'Error while Deleting file ' + @.FName
> >> >> GOTO On_Error
> >> >> END
> >> >>
> >> >> PRINT 'Deleted ' + @.FName + ' at ' +
> >> >> CONVERT(VARCHAR(28),GETDATE(),113)
> >> >>
> >> >> FETCH NEXT FROM curDir INTO @.Fname
> >> >> END
> >> >>
> >> >> CLOSE curDir
> >> >> DEALLOCATE curDir
> >> >>
> >> >> DROP TABLE #DirList
> >> >> DROP TABLE #Errors
> >> >>
> >> >> RETURN @.Error
> >> >>
> >> >> On_Error:
> >> >> BEGIN
> >> >> IF @.Error <> 0
> >> >> BEGIN
> >> >> SELECT @.Msg + '. Error # ' + CAST(@.Error AS VARCHAR(10))
> >> >> RAISERROR(@.Msg,12,1)
> >> >> RETURN @.Error
> >> >> END
> >> >> END
> >> >> GO
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >> Andrew J. Kelly SQL MVP
> >> >>
> >> >>
> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> news:041D4B77-15AB-42F8-80B6-AB740408D913@.microsoft.com...
> >> >> > Andrew or anyone,
> >> >> >
> >> >> > I'm trying to write a short sql stmts to attached to a folder and
> >> >> > query
> >> >> > that
> >> >> > folder for *.bak and if the*.bak is older than a certain amount of
> >> >> > days,
> >> >> > then
> >> >> > I want to delete them.
> >> >> >
> >> >> > Is this possible using tsql?
> >> >> >
> >> >> > "Andrew J. Kelly" wrote:
> >> >> >
> >> >> >> Don't use EM, use a script instead. EM has some limitations and
> >> >> >> things
> >> >> >> like
> >> >> >> this can be much more controlled through scripts. The restore
> >> >> >> syntax
> >> >> >> is
> >> >> >> pretty simple and there are examples in BOL. If you have troubles
> >> >> >> with
> >> >> >> it
> >> >> >> post your script and we can help.
> >> >> >>
> >> >> >> --
> >> >> >> Andrew J. Kelly SQL MVP
> >> >> >>
> >> >> >>
> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> >> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
> >> >> >> > Andrew,
> >> >> >> >
> >> >> >> > Do you know how to access a network drive within Enterprise Mgr?
> >> >> >> >
> >> >> >> > I'm trying to restore a db where the backups are located on the
> >> >> >> > network. I
> >> >> >> > can see other network drives but cannot view the I need. I've
> >> >> >> > already
> >> >> >> > checked
> >> >> >> > the permissions and made sure the drive was shared. Do you have
> >> >> >> > any
> >> >> >> > suggestions?
> >> >> >> >
> >> >> >> > "Andrew J. Kelly" wrote:
> >> >> >> >
> >> >> >> >> Just change the CONVERT to use an different format and make the
> >> >> >> >> variable
> >> >> >> >> large enough to handle the extra characters. You will have to
> >> >> >> >> look
> >> >> >> >> at
> >> >> >> >> CONVERT in BOL to see which suites your requirement best. The
> >> >> >> >> only
> >> >> >> >> thing
> >> >> >> >> is
> >> >> >> >> that when you add the time portion you usually have to use a
> >> >> >> >> format
> >> >> >> >> that
> >> >> >> >> inserts dashs or slashes. See if this works for you:
> >> >> >> >>
> >> >> >> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
> >> >> >> >> GETDATE(),120),'-',''),':',''),' ',''),12)
> >> >> >> >>
> >> >> >> >>
> >> >> >> >> --
> >> >> >> >> Andrew J. Kelly SQL MVP
> >> >> >> >>
> >> >> >> >>
> >> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> >> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
> >> >> >> >> > Andrew,
> >> >> >> >> >
> >> >> >> >> > Thanks worked. How can I add the time to it as well. I am
> >> >> >> >> > going
> >> >> >> >> > to
> >> >> >> >> > create
> >> >> >> >> > a
> >> >> >> >> > DTS package to run this query.
> >> >> >> >> >
> >> >> >> >> > "Andrew J. Kelly" wrote:
> >> >> >> >> >
> >> >> >> >> >> It would help if you gave a little more background or at
> >> >> >> >> >> least
> >> >> >> >> >> showed
> >> >> >> >> >> what
> >> >> >> >> >> you were doing now. Here is an example of adding a datetime
> >> >> >> >> >> to
> >> >> >> >> >> a
> >> >> >> >> >> backup
> >> >> >> >> >> device name but I don't know hot it fits your situation.
> >> >> >> >> >>
> >> >> >> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100), @.Name
> >> >> >> >> >> NVARCHAR(150)
> >> >> >> >> >>
> >> >> >> >> >>
> >> >> >> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
> >> >> >> >> >> FOR SELECT Catalog_Name
> >> >> >> >> >> FROM Information_Schema.Schemata
> >> >> >> >> >> -- WHERE [Catalog_Name] NOT IN
> >> >> >> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
> >> >> >> >> >> WHERE [Catalog_Name] IN ('MASTER','MODEL','MSDB')
> >> >> >> >> >>
> >> >> >> >> >>
> >> >> >> >> >> OPEN cur_DBs
> >> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >> >> >> >>
> >> >> >> >> >> WHILE @.@.FETCH_STATUS = 0
> >> >> >> >> >> BEGIN
> >> >> >> >> >>
> >> >> >> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName + '_Full_'
> >> >> >> >> >> +
> >> >> >> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) +
> >> >> >> >> >> N'.BAK'
> >> >> >> >> >>
> >> >> >> >> >> SET @.Name = @.DBName + N' Full Backup'
> >> >> >> >> >>
> >> >> >> >> >> PRINT 'Backing up database ' + @.DBName
> >> >> >> >> >>
> >> >> >> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT ,
> >> >> >> >> >> NOUNLOAD ,
> >> >> >> >> >> NAME = @.Name, NOSKIP , STATS = 10,
> >> >> >> >> >> NOFORMAT
> >> >> >> >> >>
> >> >> >> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
> >> >> >> >> >>
> >> >> >> >> >> PRINT '--- '
> >> >> >> >> >>
> >> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
> >> >> >> >> >> END
> >> >> >> >> >>
> >> >> >> >> >> CLOSE cur_DBs
> >> >> >> >> >> DEALLOCATE cur_DBs
> >> >> >> >> >>
> >> >> >> >> >>
> >> >> >> >> >> --
> >> >> >> >> >> Andrew J. Kelly SQL MVP
> >> >> >> >> >>
> >> >> >> >> >>
> >> >> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
> >> >> >> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
> >> >> >> >> >> >I want to add a datetime stamp on my backup file names but
> >> >> >> >> >> >what
> >> >> >> >> >> >I
> >> >> >> >> >> >have
> >> >> >> >> >> >is
> >> >> >> >> >> >not
> >> >> >> >> >> > adding to the filename.
> >> >> >> >> >> >
> >> >> >> >> >> > Does anyone know how to do this?
> >> >> >> >> >>
> >> >> >> >> >>
> >> >> >> >> >>
> >> >> >> >>
> >> >> >> >>
> >> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >>
> >> >>
> >> >>
> >> >>
> >>
> >>
> >>
>
>|||You can't use RETURN if you are not in a stored procedure. Just comment
that line out for your testing.
--
Andrew J. Kelly SQL MVP
"Sonya" <Sonya@.discussions.microsoft.com> wrote in message
news:D62D330E-685E-4048-8635-FC6943AC9F50@.microsoft.com...
> Ok, I was doing that as well. I'll have to remove or reference the return
> function differently. Thank you for all of your help. The message that I
> received is the following:
> Server: Msg 178, Level 15, State 1, Line 92
> A RETURN statement with a return value cannot be used in this context.
> Server: Msg 178, Level 15, State 1, Line 99
> A RETURN statement with a return value cannot be used in this context.
>
> "Andrew J. Kelly" wrote:
>> Sonya,
>> The temp table is created inside of a stored procedure. If you run the
>> sp
>> and it creates a temp table it only lives for the duration of the sp
>> execution. So by the time you query for it the table has been destroyed.
>> This is how local temp tables work and is the whole purpose of them. If
>> you
>> wan to play around with it you need to remove the code from the sp and
>> run
>> it as a standard batch in query analyzer.
>> --
>> Andrew J. Kelly SQL MVP
>>
>> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> news:D7770A92-6EEA-4B7D-8C9C-6A9DFBC92046@.microsoft.com...
>> > When I try to query the temp table within the same connection, it says
>> > the
>> > object is invalid. So I'm not sure the script is working because I
>> > can't
>> > view
>> > the table.
>> >
>> > I don't have any more specific errors. Sorry if I was vague in my
>> > previous
>> > messages.
>> > "Andrew J. Kelly" wrote:
>> >
>> >> Do you mean this line: INSERT INTO #dirlist (FName)
>> >> exec @.Return = master..xp_cmdshell 'dir /OD
>> >> C:\Data\Backups\*.Bak'
>> >>
>> >> Can you be so kind as to list exactly what the error is that you are
>> >> getting? I don't understand why you can't see this table? Are you
>> >> running
>> >> the select in the same connection as the one that created the table?
>> >> If
>> >> not
>> >> you won't be able to see it since it is a local temporary table. You
>> >> really
>> >> need to be a little more specific on what you are doing and what the
>> >> real
>> >> errors are that you are getting otherwise I am simply guessing.
>> >>
>> >> --
>> >> Andrew J. Kelly SQL MVP
>> >>
>> >>
>> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> news:61C4CAE3-8398-4905-AE4C-411D6A55DA12@.microsoft.com...
>> >> > Thanks.. I'm going through the code and I'm running into an error
>> >> > when
>> >> > you
>> >> > use the directory list command. I'm not able to view the temporary
>> >> > table
>> >> > to
>> >> > see if data is being stored.
>> >> >
>> >> > "Andrew J. Kelly" wrote:
>> >> >
>> >> >> You can't do this strictly with TSQL but this is as close as you
>> >> >> will
>> >> >> get.
>> >> >> There is a sample sp that will delete old log file backups based on
>> >> >> the
>> >> >> timestamp in the name of the files.
>> >> >>
>> >> >> -- Removing Older Backup Files --
>> >> >>
>> >> >> -- Remove any log files older than 7 days
>> >> >> DECLARE @.Error INT, @.D DATETIME
>> >> >> SET @.D = DATEADD(dd,-7,GETDATE())
>> >> >>
>> >> >> EXEC @.Error = remove_old_log_files @.D
>> >> >>
>> >> >>
>> >> >> SELECT @.Error
>> >> >>
>> >> >> --
>> >> >> CREATE PROCEDURE remove_old_log_files
>> >> >> @.DelDate DATETIME
>> >> >>
>> >> >> AS
>> >> >>
>> >> >> SET NOCOUNT ON
>> >> >>
>> >> >> DECLARE @.SQL VARCHAR(500), @.FName VARCHAR(40), @.Error INT
>> >> >> DECLARE @.Delete VARCHAR(300), @.Msg VARCHAR(100), @.Return INT
>> >> >>
>> >> >> -- Used later when we cast strings to Datetimes
>> >> >> SET DATEFORMAT MDY
>> >> >>
>> >> >> -- Create a table to hold the results of the DIR command
>> >> >> IF OBJECT_ID('tempdb..#dirlist') IS NOT NULL
>> >> >> DROP TABLE #DirList
>> >> >>
>> >> >> CREATE TABLE #dirlist (FName VARCHAR(1000))
>> >> >>
>> >> >> -- Create a table to hold any errors
>> >> >> IF OBJECT_ID('tempdb..#Errors') IS NOT NULL
>> >> >> DROP TABLE #Errors
>> >> >>
>> >> >> CREATE TABLE #Errors (Results VARCHAR(1000))
>> >> >>
>> >> >> -- Insert the results of the dir cmd into a table so we can scan
>> >> >> it
>> >> >> INSERT INTO #dirlist (FName)
>> >> >> exec @.Return = master..xp_cmdshell 'dir /OD
>> >> >> C:\Data\Backups\*.Bak'
>> >> >>
>> >> >> SET @.Error = @.@.ERROR
>> >> >>
>> >> >> IF @.Error <> 0 OR @.Return <> 0
>> >> >> BEGIN
>> >> >> IF @.Return = 1
>> >> >> SET @.Error = -1
>> >> >> SET @.Msg = 'Error while getting the filenames with DIR '
>> >> >> GOTO On_Error
>> >> >> END
>> >> >>
>> >> >> -- Remove the garbage
>> >> >> DELETE #dirlist WHERE
>> >> >> SUBSTRING(FName,1,2) < '00' OR
>> >> >> SUBSTRING(FName,1,2) > '99' OR
>> >> >> FName IS NULL OR
>> >> >> FName LIKE '%<DIR>%'
>> >> >>
>> >> >>
>> >> >> -- Create a cursor and for each file name do the processing.
>> >> >> -- The files will be processed in date order.
>> >> >> DECLARE curDir CURSOR READ_ONLY LOCAL
>> >> >> FOR
>> >> >> SELECT SUBSTRING(FName,40,40) AS FName
>> >> >> FROM #dirlist
>> >> >> WHERE CAST(SUBSTRING(FName,1,20) AS DATETIME) <
>> >> >> @.DelDate
>> >> >> AND SUBSTRING(FName,40,40) LIKE '%.BAK'
>> >> >> ORDER BY CAST(SUBSTRING(FName,1,20) AS DATETIME)
>> >> >>
>> >> >> OPEN curDir
>> >> >>
>> >> >> FETCH NEXT FROM curDir INTO @.Fname
>> >> >> WHILE (@.@.fetch_status = 0)
>> >> >> BEGIN
>> >> >>
>> >> >>
>> >> >> -- Delete the old backup files
>> >> >> SET @.Delete = 'DEL "C:\Data\Backups\' + @.FName + '"'
>> >> >>
>> >> >> INSERT INTO #Errors (Results)
>> >> >> exec @.Return = master..xp_cmdshell @.Delete
>> >> >>
>> >> >>
>> >> >> IF @.@.RowCount > 1 OR @.Return = 1
>> >> >> BEGIN
>> >> >> SET @.Error = -1
>> >> >> SET @.Msg = 'Error while Deleting file ' + @.FName
>> >> >> GOTO On_Error
>> >> >> END
>> >> >>
>> >> >> PRINT 'Deleted ' + @.FName + ' at ' +
>> >> >> CONVERT(VARCHAR(28),GETDATE(),113)
>> >> >>
>> >> >> FETCH NEXT FROM curDir INTO @.Fname
>> >> >> END
>> >> >>
>> >> >> CLOSE curDir
>> >> >> DEALLOCATE curDir
>> >> >>
>> >> >> DROP TABLE #DirList
>> >> >> DROP TABLE #Errors
>> >> >>
>> >> >> RETURN @.Error
>> >> >>
>> >> >> On_Error:
>> >> >> BEGIN
>> >> >> IF @.Error <> 0
>> >> >> BEGIN
>> >> >> SELECT @.Msg + '. Error # ' + CAST(@.Error AS VARCHAR(10))
>> >> >> RAISERROR(@.Msg,12,1)
>> >> >> RETURN @.Error
>> >> >> END
>> >> >> END
>> >> >> GO
>> >> >>
>> >> >>
>> >> >>
>> >> >> --
>> >> >> Andrew J. Kelly SQL MVP
>> >> >>
>> >> >>
>> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> news:041D4B77-15AB-42F8-80B6-AB740408D913@.microsoft.com...
>> >> >> > Andrew or anyone,
>> >> >> >
>> >> >> > I'm trying to write a short sql stmts to attached to a folder and
>> >> >> > query
>> >> >> > that
>> >> >> > folder for *.bak and if the*.bak is older than a certain amount
>> >> >> > of
>> >> >> > days,
>> >> >> > then
>> >> >> > I want to delete them.
>> >> >> >
>> >> >> > Is this possible using tsql?
>> >> >> >
>> >> >> > "Andrew J. Kelly" wrote:
>> >> >> >
>> >> >> >> Don't use EM, use a script instead. EM has some limitations and
>> >> >> >> things
>> >> >> >> like
>> >> >> >> this can be much more controlled through scripts. The restore
>> >> >> >> syntax
>> >> >> >> is
>> >> >> >> pretty simple and there are examples in BOL. If you have
>> >> >> >> troubles
>> >> >> >> with
>> >> >> >> it
>> >> >> >> post your script and we can help.
>> >> >> >>
>> >> >> >> --
>> >> >> >> Andrew J. Kelly SQL MVP
>> >> >> >>
>> >> >> >>
>> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> >> news:B202FDF2-5301-4E8E-9558-F2E0505F563C@.microsoft.com...
>> >> >> >> > Andrew,
>> >> >> >> >
>> >> >> >> > Do you know how to access a network drive within Enterprise
>> >> >> >> > Mgr?
>> >> >> >> >
>> >> >> >> > I'm trying to restore a db where the backups are located on
>> >> >> >> > the
>> >> >> >> > network. I
>> >> >> >> > can see other network drives but cannot view the I need. I've
>> >> >> >> > already
>> >> >> >> > checked
>> >> >> >> > the permissions and made sure the drive was shared. Do you
>> >> >> >> > have
>> >> >> >> > any
>> >> >> >> > suggestions?
>> >> >> >> >
>> >> >> >> > "Andrew J. Kelly" wrote:
>> >> >> >> >
>> >> >> >> >> Just change the CONVERT to use an different format and make
>> >> >> >> >> the
>> >> >> >> >> variable
>> >> >> >> >> large enough to handle the extra characters. You will have
>> >> >> >> >> to
>> >> >> >> >> look
>> >> >> >> >> at
>> >> >> >> >> CONVERT in BOL to see which suites your requirement best.
>> >> >> >> >> The
>> >> >> >> >> only
>> >> >> >> >> thing
>> >> >> >> >> is
>> >> >> >> >> that when you add the time portion you usually have to use a
>> >> >> >> >> format
>> >> >> >> >> that
>> >> >> >> >> inserts dashs or slashes. See if this works for you:
>> >> >> >> >>
>> >> >> >> >> LEFT(REPLACE(REPLACE(REPLACE(CONVERT(NVARCHAR(20),
>> >> >> >> >> GETDATE(),120),'-',''),':',''),' ',''),12)
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >> --
>> >> >> >> >> Andrew J. Kelly SQL MVP
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> >> >> news:3661E6F5-DECE-4A20-BFD1-70927898E1C7@.microsoft.com...
>> >> >> >> >> > Andrew,
>> >> >> >> >> >
>> >> >> >> >> > Thanks worked. How can I add the time to it as well. I am
>> >> >> >> >> > going
>> >> >> >> >> > to
>> >> >> >> >> > create
>> >> >> >> >> > a
>> >> >> >> >> > DTS package to run this query.
>> >> >> >> >> >
>> >> >> >> >> > "Andrew J. Kelly" wrote:
>> >> >> >> >> >
>> >> >> >> >> >> It would help if you gave a little more background or at
>> >> >> >> >> >> least
>> >> >> >> >> >> showed
>> >> >> >> >> >> what
>> >> >> >> >> >> you were doing now. Here is an example of adding a
>> >> >> >> >> >> datetime
>> >> >> >> >> >> to
>> >> >> >> >> >> a
>> >> >> >> >> >> backup
>> >> >> >> >> >> device name but I don't know hot it fits your situation.
>> >> >> >> >> >>
>> >> >> >> >> >> DECLARE @.DBName NVARCHAR(100), @.Device NVARCHAR(100),
>> >> >> >> >> >> @.Name
>> >> >> >> >> >> NVARCHAR(150)
>> >> >> >> >> >>
>> >> >> >> >> >>
>> >> >> >> >> >> DECLARE cur_DBs CURSOR STATIC LOCAL
>> >> >> >> >> >> FOR SELECT Catalog_Name
>> >> >> >> >> >> FROM Information_Schema.Schemata
>> >> >> >> >> >> -- WHERE [Catalog_Name] NOT IN
>> >> >> >> >> >> ('MASTER','MODEL','MSDB','TEMPDB')
>> >> >> >> >> >> WHERE [Catalog_Name] IN
>> >> >> >> >> >> ('MASTER','MODEL','MSDB')
>> >> >> >> >> >>
>> >> >> >> >> >>
>> >> >> >> >> >> OPEN cur_DBs
>> >> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >> >> >> >>
>> >> >> >> >> >> WHILE @.@.FETCH_STATUS = 0
>> >> >> >> >> >> BEGIN
>> >> >> >> >> >>
>> >> >> >> >> >> SET @.Device = N'C:\Data\Backups\DD_' + @.DBName +
>> >> >> >> >> >> '_Full_'
>> >> >> >> >> >> +
>> >> >> >> >> >> CONVERT(NVARCHAR(16),GETDATE(),112) +
>> >> >> >> >> >> N'.BAK'
>> >> >> >> >> >>
>> >> >> >> >> >> SET @.Name = @.DBName + N' Full Backup'
>> >> >> >> >> >>
>> >> >> >> >> >> PRINT 'Backing up database ' + @.DBName
>> >> >> >> >> >>
>> >> >> >> >> >> BACKUP DATABASE @.DBName TO DISK = @.Device WITH INIT ,
>> >> >> >> >> >> NOUNLOAD ,
>> >> >> >> >> >> NAME = @.Name, NOSKIP , STATS = 10,
>> >> >> >> >> >> NOFORMAT
>> >> >> >> >> >>
>> >> >> >> >> >> RESTORE VERIFYONLY FROM DISK = @.Device WITH FILE = 1
>> >> >> >> >> >>
>> >> >> >> >> >> PRINT '--- '
>> >> >> >> >> >>
>> >> >> >> >> >> FETCH NEXT FROM cur_DBs INTO @.DBName
>> >> >> >> >> >> END
>> >> >> >> >> >>
>> >> >> >> >> >> CLOSE cur_DBs
>> >> >> >> >> >> DEALLOCATE cur_DBs
>> >> >> >> >> >>
>> >> >> >> >> >>
>> >> >> >> >> >> --
>> >> >> >> >> >> Andrew J. Kelly SQL MVP
>> >> >> >> >> >>
>> >> >> >> >> >>
>> >> >> >> >> >> "Sonya" <Sonya@.discussions.microsoft.com> wrote in message
>> >> >> >> >> >> news:7500995A-A50E-4C69-BF45-FA9CDB70AEBF@.microsoft.com...
>> >> >> >> >> >> >I want to add a datetime stamp on my backup file names
>> >> >> >> >> >> >but
>> >> >> >> >> >> >what
>> >> >> >> >> >> >I
>> >> >> >> >> >> >have
>> >> >> >> >> >> >is
>> >> >> >> >> >> >not
>> >> >> >> >> >> > adding to the filename.
>> >> >> >> >> >> >
>> >> >> >> >> >> > Does anyone know how to do this?
>> >> >> >> >> >>
>> >> >> >> >> >>
>> >> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> >> >>
>> >> >>
>> >> >>
>> >> >>
>> >>
>> >>
>> >>
>>