NewInMachineLearning
NewInMachineLearning

Reputation: 13

SQL Server SQL query to escape the closing square bracket ]

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

Answers (2)

TT.
TT.

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

Mr Zach
Mr Zach

Reputation: 515

I think you can use sp_executesql for this. Where you pass the databasename as a parameter.

https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-executesql-transact-sql

Upvotes: 1

Related Questions