Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 ALTER DATABASE command fails in SQLCMD

Author  Topic 

NeilMc
Starting Member

2 Posts

Posted - 2010-10-19 : 04:47:04
Hi

I am trying to use the SQLCMD utility from within a windows batch script to update the bacup mode of a database into BULK_LOGGED whilst I do a large data load and then revert back to FULL afterwards.

The database name is fed into the TSQL command as a variable in order for the script to be generic and usable on a number of other servers.

However, the command ALTER DATABASE %DBNAME% SET RECOVERY BULK_LOGGED
fails when executed within the sqlCmd utility with a message that states RECOVERY is invalid for the SET command.

From this I'm assuming that the SET statement within the TSQL for the alter database command is being seen as a separate statement or is possibly a reserved word within SQLCMD.

I've tried serveral ways of enclosing the statement within quotes but it always fails. I've also spent many hours looking for the answer on the Net. I'm now desparate to get this working so would be very grateful for any help or suggestions.

Regards

Neil

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-19 : 05:00:51
I think the problem is the variable.
You need dynamic sql for this.
http://www.sommarskog.se/dynamic_sql.html


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

NeilMc
Starting Member

2 Posts

Posted - 2010-10-19 : 07:01:18
I've finally solved this.

The database name needs to enclosed within square brackets [] ....

EXECUTE dbo.sp_ExecuteSQL N'ALTER DATABASE [%DBNAME%] SET RECOVERY BULK_LOGGED'

Obvious when you know how :o)
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-10-19 : 07:08:51
Ok thx for coming back and posting your solution

So it was already dynamic sql but that was not shown in your first post...


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -