First, the identifiers in sqlserver have certain rules. For example, if your createtableabc123 (...) contains spaces, it does not comply with the rules. You will write createtable [abc123] (...) to define the identifier using. Quotename makes the string a valid identifier. What is its use? For example, you have
First, the identifiers in SQL Server have certain rules. For example, if you create table abc 123 (...), there are spaces in the middle. It does not comply with the rules. You will write the create table [abc 123] (...) to define the identifier using. Quotename makes the string a valid identifier. What is its use? For example, you have
First, the identifiers in SQL Server have certain rules, such as you
Create table ABC123 (...)
There are spaces in the middle. It does not comply with the rules.
You will write it into create table [abc 123] (...)
[] Is used to define the identifier.
Quotename makes the string a valid identifier.
What is its use? For example:
You have a table named aa [] bb.
How do you write dynamic statement queries for some applications?
Exec ('select * from aa [] bb ')? X
Set @ SQL = 'select * from' + quotename ('aa [] BB ')
Exec (@ SQL)
Of course, you can also write the escape Code directly.
Select * from [aa [] bb]
That is, quotename makes the input in the function a valid identifier.
For example, in the above example, aa [] bb is not a valid identifier.
Another point is that the quotename function can be written in several ways:
The valid identifier generated by quotename ('A') is [aa]
The valid identifier generated by quotename ('aaa', '') is [aa]
The valid identifier generated by quotename ('A', ''') is 'A'
Interpretation 2:
To put it simply
For example, you have a table named index.
You have a dynamic query. The parameter is the table name.
Declare @ tbname varchar (256)
Set @ tbname = 'index'
--- Query the data in this table:
Print ('select * from' + @ tbname)
Exec ('select * from' + @ tbname)
-- The print data is
Select * from index
Because index is a key word, it must have an error. You can add brackets:
Select * from [index]
QUOTENAME is available, namely:
Print ('select * from' + QUOTENAME (@ tbname ))
-- Result: select * from [index]
Exec ('select * from' + QUOTENAME (@ tbname ))
Reference: http://www.cnblogs.com/smfish007bin/archive/2008/10/16/1312786.html