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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.