• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1393
  • Last Modified:

SQl Syntax --Quotename

I am trying to add a linked server name customer to get this
customer.Addresses.dbo.sysfiles from code below
@name = databasename => addresses inthis example
customer   = linked server
Addresses  = databasename


select @SQL = 'insert into #databases
                        select N'''+ @name + ''', sum(size) from '
                        + QuoteName(@name) + '.dbo.sysfiles'

Any ideas ,  also can someone explain to me what Quotename actually does?

1 Solution
It returns the stringwith valid SQL identifier. i.e. adds '[' or ']' to string if required.

More info here


Hope it helps

ohemaaAuthor Commented:
no I saw this already ..did not really understand. I was hoping someone could break it down and help me add link server to code above.  That was my main question
To add your linked server name just simply add it :-) :

select @SQL = 'insert into #databases
                        select N'''+ @name + ''', sum(size) from customer.'
                        + QuoteName(@name) + '.dbo.sysfiles'

QUOTENAME allows you to surround a string of length up to 128 characters (which can include white characters like spaces) with brackets (by default) or any other characters you want.


SELECT QUOTENAME('this is my string')


[this is my string]

This  function is useful when you want to make sure that your non-standard identifiers (nonstandard object names - those names which consists of not only alphanumeric characters) will not cause any errors in SQL statements. In your example no matter what will the name of the database be, the query should run properly.

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now