简体   繁体   中英

How can I access a SQL Server database with . in name via set-location

Question is in the title. I have a database server which has a number of databases that have periods in the name (they are urls so that I know which database is associated with which site such as foo.bar.com_content). When I navigate to SQLSERVER:\\sql\\[server]\\default\\databases and run get-childitem I can see a list of all my databases just fine. When I try to cd [database name with period] I get the following error:

Set-Location : SQL Server PowerShell provider error: The number of keys specified does not match the number of keys required to address this object. The number of keys required are: Name.
At line:1 char:3
+ cd <<<<  '[database name with periods here]'
    + CategoryInfo          : InvalidData: (SQLSERVER:\sql\...t.org_Content:SqlPath) [Set-Location], GenericProviderException
    + FullyQualifiedErrorId : KeysNotMatching,Microsoft.PowerShell.Commands.SetLocationCommand

That error repeats four times, followed by:

Set-Location : Cannot find path 'SQLSERVER:\sql\[servername]\default\databases\[database name with periods here]' because it does not exist.
At line:1 char:3
+ cd <<<<  '[database name with periods here]'
    + CategoryInfo          : ObjectNotFound: (SQLSERVER:\sql\...t.org_Content:String)     [Set-Location], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.SetLocationCommand

I can run the same command to a database on the same server that has no periods in the name and connect just fine.

I am running this from my windows desktop machine in the Powershell ISE, Powershell v2.0 with the SQL Server 2012 tools installed. The remote database server is running SQL Server 2008r2.

Try Changing the period . to it's hex value of %2e like below, considering that the database name is foo.bar.com_content .

cd [foo%2ebar%2ecom_content]

Rahul's answer is generally correct but remove the brackets. Here is a correction to Rahul's answer.

cd foo%2ebar%2ecom_content

If your name convention utilizes spaces then encase it in quotes (single or double either works)

cd 'foo%2eb ar%2ecom_content'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM