SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

change logical names in SMO

change logical names in SMO

Old Hand
Old Hand (310 reputation)Old Hand (310 reputation)Old Hand (310 reputation)Old Hand (310 reputation)Old Hand (310 reputation)Old Hand (310 reputation)Old Hand (310 reputation)Old Hand (310 reputation)

Group: General Forum Members
Points: 310 Visits: 197
Hello All,

I am having issues changing the logical names post restore.

I am restoring a database with SMO (using VB.NET). After the database has been restored, I am changing the logical names of the files to match the new database name by using an alter database statement query.

Before the restore is performed.I am doing a validation for valid database names (i.e. valid windows OS filenames) via a regex pattern before performing the restore. The problem is, the T-SQL that I am executing after the restore is not adequately compensating for special characters in the database name (i.e. filename). Here are the statements

Taken from SMO SQL capture during a VB.NET application run. SMO changed the database name to DBA[]] instead of DBA[]. This works.
RESTORE DATABASE [DBA[]]] FROM  DISK = N'C:\DBA-NoPassword.bak' WITH  MOVE N'DBA' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DBA[].mdf',  MOVE N'DBA_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\DBA[]_log.ldf',  UNLOAD,  REPLACE,  STATS = 1

Trying to change the logical names, post restore.

Corrected SQL statement that works:

Uncorrected statement that does not work:

As you can see, I had to add an extra "]" at the end of the database name "DBA[]" to make the statement work. It is as if the extra "]" is acting as an escape character for the final ].

My question is.. if SMO can correctly restore a database name which contains "[]" in the name, then a SMO operation should be able to change the logical names. I have been googling and have not been able to find SMO code to incorporate into my VB.NET app, and I do not want to create special conditions for each special character that may be used in a database name. I also do not want to limit the special characters either.

Did I miss something on my google search for how to change logical names using SMO?

Let me know if you guys have any ideas.


I am aware that when I create a new database, it will create logical names based upon the database name. When creating a new database via a restore, it uses the logical names embedded in the backup headers. I cannot override the default logical names during the restore, and I need the move option because the remote server I am restoring to does not contain the same file paths as the backup file.

My question is for already existing databases, that are being restored, which already have logical names different than the database name.



Update #2:

I think a workaround solution might be for me to first drop the database, create a new database, and then do a restore with move to option. Sure seems like a lot of work. Please let me know if I can change logical names with SMO...


You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum