Getting Started with Web applications development with servlets and JSP - Part 5: Filters
First Time? You can support us by signing up. It takes only 5 seconds. Click here to sign up. If you already have an account, click here to login.


Upload Image Close it
Select File

Learned something today? Share it, or learn from what others have learned today

SQL Server - Using QUOTENAME() function to avoid problems with quotes in object names

Jan 13 2012 2:08AM by bulentgucuk   

I support third party software databases at work. During the implementation of the application it needed to create a database to store the data. However the application created a database with a name that does not follow the rules for the format of identifiers. In my case we ended up with a database name '20120101_Survey'.

On the same server I have a custom built script that backups up all the databases at night. However, following the database creation my backup process failed and I got an alert about the process. Further looking into the problem I found out that there was a problem with the syntax. As soon as I see the statement I knew what the problem was but never thought about it while deploying the application and database. To keep the story short I used the SQL Server function called QUOTENAME() to fix the problem.

<pre class="brush: plain">
SELECT  QUOTENAME(name) AS DelimitedDbName
FROM    sys.databases
</pre>

Using the function in my custom script help me return the database names with delimiters so that backup process completes without error. Since then I have been using the function with all the object names. This simple function can prevent some headaches if a DBA needs to support 3rd party databases (SharePoint is also a good example because the SharePoint database names don't follow the rules of the identifier format). The link to the msdn article is in source URL and below is simple statement that returns all the databases in your system with delimiters.

Read More..  [15 clicks]


bulentgucuk
367 · 0% · 71
16
 
8
 
 
0
Incorrect
 
0
Interesting
 
0
Forgotten
 
0
Move



Submit

2  Comments  

  • But still have to remember about length limitation http://beyondrelational.com/blogs/madhivanan/archive/2010/10/11/usage-of-quotename-function.aspx

    commented on Jan 16 2012 7:01AM  .  Report Abuse This post is not formatted correctly
    Adam Tokarski
    75 · 3% · 691
  • Adam, Thanks for the nice reminder. I should have include a link which is (click here) that talks about the rules around identifiers and specifically says that 'both regular and delimited identifiers must contain from 1 through 128 characters. For local temporary tables, the identifier can have a maximum of 116 characters.'

    Thanks, Bulent

    commented on Jan 16 2012 10:22AM  .  Report Abuse This post is not formatted correctly
    bulentgucuk
    367 · 0% · 71

Your Comment


Sign Up or Login to post a comment.

"SQL Server - Using QUOTENAME() function to avoid problems with quotes in object names" rated 5 out of 5 by 16 readers
SQL Server - Using QUOTENAME() function to avoid problems with quotes in object names , 5.0 out of 5 based on 16 ratings
    Copyright © Beyondrelational.com Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising