Showing posts with label scripts. Show all posts
Showing posts with label scripts. Show all posts

Friday, March 23, 2012

Rename Database

I have a custom TFSBuild task that restores a backup of our database, runs some scripts against it, deletes the previous day's database, and the renames the new database to the same name as the one that was deleted. This has been working fine for weeks now, but started failing yesterday and also failed again today. The old database is deleted successfully, but the rename command fails.

The code that runs is:

Dim strRenameScript As String = String.Format("ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE", _strTempDatabaseName) & Environment.NewLine & _
"GO" & Environment.NewLine & String.Format("ALTER DATABASE {0} MODIFY NAME={1}", _strTempDatabaseName, _strDatabaseName) & _
Environment.NewLine & "GO" & Environment.NewLine & String.Format("ALTER DATABASE {0} SET MULTI_USER", _strDatabaseName) & _
Environment.NewLine & "GO"

Executed using the Microsoft.SqlServer.Management.Smo classes, which is interpreted as:

ALTER DATABASE MyTempDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE MyTempDatabase MODIFY NAME=MyLiveDatabase
GO
ALTER DATABASE MyLiveDatabase SET MULTI_USER
GO

This used to be fine, but now the batch fails after setting the database into single user mode, and the only error returned is:

An exception occurred while executing a Transact-SQL statement or batch

If I subsequently run the above script in SQLSMS it works fine. Does anyone know why this might have started failing all of a sudden?

Could you run the code below and post back the results?

Chris

SELECT *
FROM master.sys.databases
WHERE [name] IN ('MyTempDatabase', 'MyLiveDatabase')

|||

from which database context u r executing this statement... be sure that u r not connected to this particular database, in that case the connection is already broken by this statement

Madhu

|||

Madhu:
I'm connecting to the master database both when deleting the old database and when renaming the new database:

Dim cnTempDatabase As SqlConnection = New SqlConnection(String.Format(_strConnectionString, _strDatabaseServer, "Master"))
Dim sqlServer As Server = New Server(New ServerConnection(cnTempDatabase))

Chris:
I've manually renamed the database now so daily development can continue, but I get one result back for the 'live' database (what the temp one was renamed to).

I'll see if it fails again in the morning and if so I'll run the same query before doing anything and post back.

Would the error have logged any more details in the SQL Server log?

|||

OK, the statement failed again last night so I ran your query again. As expected, only the temp database (the one left in single user mode) is returned.

Would a more verbose error have been logged anywhere?

|||

if the error is not logged in SQL Server Event Log....most probably it means that the error is not occuring at the SQL Level but the Application level... capture the error from application or application log

Madhu

|||

There are lots of informational messages in the log about restoring the database, setting the old database to single user mode etc (although I note nothing about deleting it) and then the last entry is the about setting the temp database to single user mode (which happens just before the rename command):

2007-03-09 06:16:42.68 spid55 Setting database option SINGLE_USER to ON for database HighwayP2Temp20070309.

Then nothing. Yesterday (when the rename worked) there also wasn't any log entry about deleting the old database, so I guess that isn't an indicator.

Any ideas?

|||

Found more info:

If I use the full backup of our database (~70Gb) then the rename fails, unless I have restarted the SQL Server instance at some point in the day beforehand. If I use the shrunk version of our database (~1.5Gb) then the rename is always fine. Perhaps this is something to do with SQL Server memory usage? Is anyone aware of any existing issues regarding memory utilisation that may affect a rename action?

Rename Database

I have a custom TFSBuild task that restores a backup of our database, runs some scripts against it, deletes the previous day's database, and the renames the new database to the same name as the one that was deleted. This has been working fine for weeks now, but started failing yesterday and also failed again today. The old database is deleted successfully, but the rename command fails.

The code that runs is:

Dim strRenameScript AsString = String.Format("ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE", _strTempDatabaseName) & Environment.NewLine & _
"GO" & Environment.NewLine & String.Format("ALTER DATABASE {0} MODIFY NAME={1}", _strTempDatabaseName, _strDatabaseName) & _
Environment.NewLine & "GO" & Environment.NewLine & String.Format("ALTER DATABASE {0} SET MULTI_USER", _strDatabaseName) & _
Environment.NewLine & "GO"

Executed using the Microsoft.SqlServer.Management.Smo classes, which is interpreted as:

ALTERDATABASE MyTempDatabase SET SINGLE_USER WITHROLLBACK IMMEDIATE
GO
ALTERDATABASE MyTempDatabase MODIFY NAME=MyLiveDatabase
GO
ALTERDATABASE MyLiveDatabase SET MULTI_USER
GO

This used to be fine, but now the batch fails after setting the database into single user mode, and the only error returned is:

An exception occurred while executing a Transact-SQL statement or batch

If I subsequently run the above script in SQLSMS it works fine. Does anyone know why this might have started failing all of a sudden?

Could you run the code below and post back the results?

Chris

SELECT *
FROM master.sys.databases
WHERE [name] IN ('MyTempDatabase', 'MyLiveDatabase')

|||

from which database context u r executing this statement... be sure that u r not connected to this particular database, in that case the connection is already broken by this statement

Madhu

|||

Madhu:
I'm connecting to the master database both when deleting the old database and when renaming the new database:

Dim cnTempDatabase As SqlConnection = New SqlConnection(String.Format(_strConnectionString, _strDatabaseServer, "Master"))
Dim sqlServer As Server = New Server(New ServerConnection(cnTempDatabase))

Chris:
I've manually renamed the database now so daily development can continue, but I get one result back for the 'live' database (what the temp one was renamed to).

I'll see if it fails again in the morning and if so I'll run the same query before doing anything and post back.

Would the error have logged any more details in the SQL Server log?

|||

OK, the statement failed again last night so I ran your query again. As expected, only the temp database (the one left in single user mode) is returned.

Would a more verbose error have been logged anywhere?

|||

if the error is not logged in SQL Server Event Log....most probably it means that the error is not occuring at the SQL Level but the Application level... capture the error from application or application log

Madhu

|||

There are lots of informational messages in the log about restoring the database, setting the old database to single user mode etc (although I note nothing about deleting it) and then the last entry is the about setting the temp database to single user mode (which happens just before the rename command):

2007-03-09 06:16:42.68 spid55 Setting database option SINGLE_USER to ON for database HighwayP2Temp20070309.

Then nothing. Yesterday (when the rename worked) there also wasn't any log entry about deleting the old database, so I guess that isn't an indicator.

Any ideas?

|||

Found more info:

If I use the full backup of our database (~70Gb) then the rename fails, unless I have restarted the SQL Server instance at some point in the day beforehand. If I use the shrunk version of our database (~1.5Gb) then the rename is always fine. Perhaps this is something to do with SQL Server memory usage? Is anyone aware of any existing issues regarding memory utilisation that may affect a rename action?

Friday, March 9, 2012

Removing old .bak & .trn files

I've run into a problem I didn't expect with my backup program. It seems
that Windows admins are very lax about writing scripts to remove old backup
files after copying them to tape, even though they say its easy, they don't
do it for all database backups on all servers in the farm.
So, I'm wondering first, if I should do it from my backup program (based on
RealSQLGuy's) and second, how I should go about it.
My rough thought is to do an xm_cmdshell('dir') in the backup directory into
a table variable, parse the date from the filename into a datetime column,
select the old files and issue an xp_cmdshell('del <file>') command via
dynamic sql.
Thanks,
JayHello Jay!
In SQL Server 2005, you could use Maintenance Plan Wizard to create a
Maintenance Cleanup Task to delete old bak and trn files.
Ekrem Önsoy
"Jay" <spam@.nospam.org> wrote in message
news:eQNEUct9HHA.5404@.TK2MSFTNGP02.phx.gbl...
> I've run into a problem I didn't expect with my backup program. It seems
> that Windows admins are very lax about writing scripts to remove old
> backup files after copying them to tape, even though they say its easy,
> they don't do it for all database backups on all servers in the farm.
> So, I'm wondering first, if I should do it from my backup program (based
> on RealSQLGuy's) and second, how I should go about it.
> My rough thought is to do an xm_cmdshell('dir') in the backup directory
> into a table variable, parse the date from the filename into a datetime
> column, select the old files and issue an xp_cmdshell('del <file>')
> command via dynamic sql.
> Thanks,
> Jay
>
>|||Maintenance plan in 2005?
Besides, I do all my maintenance through SQLagent after writing the scripts
myself.
"Ekrem Önsoy" <ekrem@.btegitim.com> wrote in message
news:OOO8bft9HHA.5424@.TK2MSFTNGP02.phx.gbl...
> Hello Jay!
>
> In SQL Server 2005, you could use Maintenance Plan Wizard to create a
> Maintenance Cleanup Task to delete old bak and trn files.
>
> --
> Ekrem Önsoy
>
> "Jay" <spam@.nospam.org> wrote in message
> news:eQNEUct9HHA.5404@.TK2MSFTNGP02.phx.gbl...
>> I've run into a problem I didn't expect with my backup program. It seems
>> that Windows admins are very lax about writing scripts to remove old
>> backup files after copying them to tape, even though they say its easy,
>> they don't do it for all database backups on all servers in the farm.
>> So, I'm wondering first, if I should do it from my backup program (based
>> on RealSQLGuy's) and second, how I should go about it.
>> My rough thought is to do an xm_cmdshell('dir') in the backup directory
>> into a table variable, parse the date from the filename into a datetime
>> column, select the old files and issue an xp_cmdshell('del <file>')
>> command via dynamic sql.
>> Thanks,
>> Jay
>>
>|||This is just me but I would create a batch file to delete the files and give
it to the Windows group to execute it after they do the tape backup. Make
sure that the batch file execution step depends on the tape backup so that
you are not without your backups if the tape backup fails.
"Jay" wrote:
> Maintenance plan in 2005?
> Besides, I do all my maintenance through SQLagent after writing the scripts
> myself.
> "Ekrem Ã?nsoy" <ekrem@.btegitim.com> wrote in message
> news:OOO8bft9HHA.5424@.TK2MSFTNGP02.phx.gbl...
> > Hello Jay!
> >
> >
> > In SQL Server 2005, you could use Maintenance Plan Wizard to create a
> > Maintenance Cleanup Task to delete old bak and trn files.
> >
> >
> > --
> > Ekrem Ã?nsoy
> >
> >
> > "Jay" <spam@.nospam.org> wrote in message
> > news:eQNEUct9HHA.5404@.TK2MSFTNGP02.phx.gbl...
> >> I've run into a problem I didn't expect with my backup program. It seems
> >> that Windows admins are very lax about writing scripts to remove old
> >> backup files after copying them to tape, even though they say its easy,
> >> they don't do it for all database backups on all servers in the farm.
> >>
> >> So, I'm wondering first, if I should do it from my backup program (based
> >> on RealSQLGuy's) and second, how I should go about it.
> >>
> >> My rough thought is to do an xm_cmdshell('dir') in the backup directory
> >> into a table variable, parse the date from the filename into a datetime
> >> column, select the old files and issue an xp_cmdshell('del <file>')
> >> command via dynamic sql.
> >>
> >> Thanks,
> >> Jay
> >>
> >>
> >>
> >
>
>

Wednesday, March 7, 2012

Removing Identity Property

Folks,
I am trying to alter my database thru SQL Scripts. I have a few tables where
I have columns having Identity Property. I now want to modify these columns
having no Identity Property. But I can't find SQL Statement that can do
this.
Can some one help?
Regards
Shailaindra SharmaYou cannot remove this property... you must add another column, move the id
info to the new column, drop the old column, and rename the new column...
Or do the same thing at the table level... However,
If you go into SEM, select the table , right click and go into design, you
can simply select the column and uncheck Identity at the bottom... SEM will
generate a script to do this work for you - if there are lots of rows it may
take a while.
--
Wayne Snyder MCDBA, SQL Server MVP
Mariner, Charlotte, NC
(Please respond only to the newsgroup.)
I support the Professional Association for SQL Server ( PASS) and it's
community of SQL Professionals.
"Shailaindra Sharma" <shailaindra@.abosoftware.com> wrote in message
news:eIHMP%23pZFHA.3840@.tk2msftngp13.phx.gbl...
> Folks,
> I am trying to alter my database thru SQL Scripts. I have a few tables
where
> I have columns having Identity Property. I now want to modify these
columns
> having no Identity Property. But I can't find SQL Statement that can do
> this.
> Can some one help?
> Regards
> Shailaindra Sharma
>|||Why can't we do things like that which we can easily do in access. How hard
can it be for ms to add these things which will make life so easy.
Regards
"Wayne Snyder" <wayne.nospam.snyder@.mariner-usa.com> wrote in message
news:ewlf9KqZFHA.3048@.TK2MSFTNGP14.phx.gbl...
> You cannot remove this property... you must add another column, move the
> id
> info to the new column, drop the old column, and rename the new column...
> Or do the same thing at the table level... However,
> If you go into SEM, select the table , right click and go into design, you
> can simply select the column and uncheck Identity at the bottom... SEM
> will
> generate a script to do this work for you - if there are lots of rows it
> may
> take a while.
> --
> Wayne Snyder MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> (Please respond only to the newsgroup.)
> I support the Professional Association for SQL Server ( PASS) and it's
> community of SQL Professionals.
> "Shailaindra Sharma" <shailaindra@.abosoftware.com> wrote in message
> news:eIHMP%23pZFHA.3840@.tk2msftngp13.phx.gbl...
>> Folks,
>> I am trying to alter my database thru SQL Scripts. I have a few tables
> where
>> I have columns having Identity Property. I now want to modify these
> columns
>> having no Identity Property. But I can't find SQL Statement that can do
>> this.
>> Can some one help?
>> Regards
>> Shailaindra Sharma
>>
>|||Consider proposing this:
sqlwish@.microsoft.com
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"John" <John@.nospam.infovis.co.uk> wrote in message news:%23iv4mYrZFHA.2664@.TK2MSFTNGP15.phx.gbl...
> Why can't we do things like that which we can easily do in access. How hard
> can it be for ms to add these things which will make life so easy.
> Regards
>|||You mean no one has found it to be a problem as yet? :)
Regards
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:esZQgZsZFHA.3596@.tk2msftngp13.phx.gbl...
> Consider proposing this:
> sqlwish@.microsoft.com
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "John" <John@.nospam.infovis.co.uk> wrote in message
> news:%23iv4mYrZFHA.2664@.TK2MSFTNGP15.phx.gbl...
>> Why can't we do things like that which we can easily do in access. How
>> hard can it be for ms to add these things which will make life so easy.
>> Regards

Removing Identity Property

Folks,
I am trying to alter my database thru SQL Scripts. I have a few tables where
I have columns having Identity Property. I now want to modify these columns
having no Identity Property. But I can't find SQL Statement that can do
this.
Can some one help?
Regards
Shailaindra Sharma
You cannot remove this property... you must add another column, move the id
info to the new column, drop the old column, and rename the new column...
Or do the same thing at the table level... However,
If you go into SEM, select the table , right click and go into design, you
can simply select the column and uncheck Identity at the bottom... SEM will
generate a script to do this work for you - if there are lots of rows it may
take a while.
Wayne Snyder MCDBA, SQL Server MVP
Mariner, Charlotte, NC
(Please respond only to the newsgroup.)
I support the Professional Association for SQL Server ( PASS) and it's
community of SQL Professionals.
"Shailaindra Sharma" <shailaindra@.abosoftware.com> wrote in message
news:eIHMP%23pZFHA.3840@.tk2msftngp13.phx.gbl...
> Folks,
> I am trying to alter my database thru SQL Scripts. I have a few tables
where
> I have columns having Identity Property. I now want to modify these
columns
> having no Identity Property. But I can't find SQL Statement that can do
> this.
> Can some one help?
> Regards
> Shailaindra Sharma
>
|||Why can't we do things like that which we can easily do in access. How hard
can it be for ms to add these things which will make life so easy.
Regards
"Wayne Snyder" <wayne.nospam.snyder@.mariner-usa.com> wrote in message
news:ewlf9KqZFHA.3048@.TK2MSFTNGP14.phx.gbl...
> You cannot remove this property... you must add another column, move the
> id
> info to the new column, drop the old column, and rename the new column...
> Or do the same thing at the table level... However,
> If you go into SEM, select the table , right click and go into design, you
> can simply select the column and uncheck Identity at the bottom... SEM
> will
> generate a script to do this work for you - if there are lots of rows it
> may
> take a while.
> --
> Wayne Snyder MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> (Please respond only to the newsgroup.)
> I support the Professional Association for SQL Server ( PASS) and it's
> community of SQL Professionals.
> "Shailaindra Sharma" <shailaindra@.abosoftware.com> wrote in message
> news:eIHMP%23pZFHA.3840@.tk2msftngp13.phx.gbl...
> where
> columns
>
|||Consider proposing this:
sqlwish@.microsoft.com
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"John" <John@.nospam.infovis.co.uk> wrote in message news:%23iv4mYrZFHA.2664@.TK2MSFTNGP15.phx.gbl...
> Why can't we do things like that which we can easily do in access. How hard
> can it be for ms to add these things which will make life so easy.
> Regards
>
|||You mean no one has found it to be a problem as yet?
Regards
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:esZQgZsZFHA.3596@.tk2msftngp13.phx.gbl...[vbcol=seagreen]
> Consider proposing this:
> sqlwish@.microsoft.com
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "John" <John@.nospam.infovis.co.uk> wrote in message
> news:%23iv4mYrZFHA.2664@.TK2MSFTNGP15.phx.gbl...

Removing Identity Property

Folks,
I am trying to alter my database thru SQL Scripts. I have a few tables where
I have columns having Identity Property. I now want to modify these columns
having no Identity Property. But I can't find SQL Statement that can do
this.
Can some one help?
Regards
Shailaindra SharmaYou cannot remove this property... you must add another column, move the id
info to the new column, drop the old column, and rename the new column...
Or do the same thing at the table level... However,
If you go into SEM, select the table , right click and go into design, you
can simply select the column and uncheck Identity at the bottom... SEM will
generate a script to do this work for you - if there are lots of rows it may
take a while.
Wayne Snyder MCDBA, SQL Server MVP
Mariner, Charlotte, NC
(Please respond only to the newsgroup.)
I support the Professional Association for SQL Server ( PASS) and it's
community of SQL Professionals.
"Shailaindra Sharma" <shailaindra@.abosoftware.com> wrote in message
news:eIHMP%23pZFHA.3840@.tk2msftngp13.phx.gbl...
> Folks,
> I am trying to alter my database thru SQL Scripts. I have a few tables
where
> I have columns having Identity Property. I now want to modify these
columns
> having no Identity Property. But I can't find SQL Statement that can do
> this.
> Can some one help?
> Regards
> Shailaindra Sharma
>|||Why can't we do things like that which we can easily do in access. How hard
can it be for ms to add these things which will make life so easy.
Regards
"Wayne Snyder" <wayne.nospam.snyder@.mariner-usa.com> wrote in message
news:ewlf9KqZFHA.3048@.TK2MSFTNGP14.phx.gbl...
> You cannot remove this property... you must add another column, move the
> id
> info to the new column, drop the old column, and rename the new column...
> Or do the same thing at the table level... However,
> If you go into SEM, select the table , right click and go into design, you
> can simply select the column and uncheck Identity at the bottom... SEM
> will
> generate a script to do this work for you - if there are lots of rows it
> may
> take a while.
> --
> Wayne Snyder MCDBA, SQL Server MVP
> Mariner, Charlotte, NC
> (Please respond only to the newsgroup.)
> I support the Professional Association for SQL Server ( PASS) and it's
> community of SQL Professionals.
> "Shailaindra Sharma" <shailaindra@.abosoftware.com> wrote in message
> news:eIHMP%23pZFHA.3840@.tk2msftngp13.phx.gbl...
> where
> columns
>|||Consider proposing this:
sqlwish@.microsoft.com
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"John" <John@.nospam.infovis.co.uk> wrote in message news:%23iv4mYrZFHA.2664@.TK2MSFTNGP15.phx
.gbl...
> Why can't we do things like that which we can easily do in access. How har
d
> can it be for ms to add these things which will make life so easy.
> Regards
>|||You mean no one has found it to be a problem as yet?
Regards
"Tibor Karaszi" <tibor_please.no.email_karaszi@.hotmail.nomail.com> wrote in
message news:esZQgZsZFHA.3596@.tk2msftngp13.phx.gbl...[vbcol=seagreen]
> Consider proposing this:
> sqlwish@.microsoft.com
>
> --
> Tibor Karaszi, SQL Server MVP
> http://www.karaszi.com/sqlserver/default.asp
> http://www.solidqualitylearning.com/
>
> "John" <John@.nospam.infovis.co.uk> wrote in message
> news:%23iv4mYrZFHA.2664@.TK2MSFTNGP15.phx.gbl...