QUOTENAME()?Parameters:
-
character_string– The string to be delimited. Maximum length is 128 characters. -
quote_character(optional) – The character to use for quoting. Valid options are: -
[or](default) -
"(double quote) -
'(single quote) -
(or) -
{or} -
<or>
Why Use QUOTENAME()?
SQL identifiers (like table names or column names) can include spaces, reserved words, or special characters. Wrapping them in brackets ensures they are treated correctly by SQL Server.
For example:
-
A table named
order detailswould cause an error unless written as[order details]. -
A column named
select(which is a reserved keyword) must be written as[select].
QUOTENAME() automates this for you.
- If the string exceeds 128 characters, QUOTENAME() returns NULL.
- It escapes the closing delimiter if it appears inside the string.
QUOTENAME() here ensures that even if @tableName contains spaces or reserved words, the query will execute without error.