• 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

--code

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?

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

More info here

http://msdn2.microsoft.com/en-us/library/ms176114.aspx

Hope it helps

Thanks
0
 
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
0
 
brejkCommented:
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.

Example:

SELECT QUOTENAME('this is my string')

Result:

[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.
0

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