Reputation: 13
I have db whose name is shreyanshdb] that is it has a closing square bracket. I wanted to perform the backup of this db using SQL query in SQL server. But it is throwing me an exception: Transact-sql exception.
Here is the command : BACKUP DATABASE [shreyanshdb]] to disk = 'c:\DB'
I really don't know how do i escape the closing square bracket...
Upvotes: 1
Views: 964
Reputation: 16145
BACKUP DATABASE [shreyanshdb]]] to disk = 'c:\DB'
You can see how things should be quoted from the QUOTENAME
function documentation. For rectangular brackets, the closing bracket should be doubled.
It's probably best not to use those brackets, or spaces, or any irregular characters in a database name, table name, column name or any other object name in SQL Server.
Upvotes: 1
Reputation: 515
I think you can use sp_executesql for this. Where you pass the databasename as a parameter.
Upvotes: 1