Usage of quotename

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.