posted 4/23/2012 9:09:51 AM by Amit Bansal - Views: [5779]
Hi Friends,
Here is an interesting function: QUOTENAME() which returns a Unicode string with the delimiters added to make the input string a valid SQL Server delimited identifier. (from Books Online)
For example:
The default delimiter is square brackets, unless you specify any of the other two delimiters (single quote ‘ or double quote “)
So where and how can this function be used?
Dynamic SQL is a good example. Suppose you have object names that contain spaces and you want to extract these object names from system catalogs to be used in your dynamic code – you will have to enclose them in delimiters.
For example;
Let us create a table in AdventureWorks that will contain spaces in the object name. After creating a sample table, we will insert a record into it.
use AdventureWorks GO create table [a funny table with spaces] (id int) insert into [a funny table with spaces] values (1)
Now, if you query the sys.objects, you will get the object name (observe that it is without the delimiters)
Now; you want extract this object name and want to use in your dynamic code but it won’t work if you do not use the QUOTENAME function. Let us first try without the QUOTENAME function:
declare @tablename nvarchar(200) =''; select @tablename = 'select * from ' + name + ';' FROM sys.objects where name like '%funny%' EXECUTE (@tablename);
And you get the following error:
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'table'. Msg 319, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Now try the same code with the QUOTENAME function:
declare @tablename nvarchar(200) =''; select @tablename = 'select * from ' + QUOTENAME(name) + ';' FROM sys.objects where name like '%funny%' EXECUTE (@tablename);
And this works :)
Interesting function; comments and usage welcome !
If you liked this post, do like us on FaceBook at http://www.FaceBook.com/SQLServerGeeks
Have a SQL Server question? Join the fastest growing SQL Server facebook group at: http://www.facebook.com/groups/458103987564477/
Regards, Amit Bansal
http://www.twitter.com/A_Bansal http://www.twitter.com/SQLServerGeeks http://www.amitbansal.net/ Visit my FaceBook page at http://www.facebook.com/AmitRSBansal Contribute on SQLServerGeeks.com: visit http://www.sqlservergeeks.com/default-category/write-for-us
Amit Bansal (Member since: 3/12/2011 4:59:54 PM) Follow Amit at Twitter @A_Bansal : Amit Bansal is the CTO of eDominer Systems & Peopleware India. He is a consultant, trainer, writer, speaker & evangelist on SQL Server & Business Intelligence. A seasoned speaker; he speaks at major summits and conferences of Microsoft worldwide including TechED, MCT Summits, MSDN & TechNet conferences. Over the last 8 years, he has consulted, trained & mentored more than 6000 IT professionals on SQL Server & Business Intelligence and worked with top notch blue chip companies worldwide including Microsoft, Infosys, Wipro, RBS, HCL, HP, Siemens, IBM, Accenture, etc. He has delivered more than 400 workshops on SQL Server & Business Intelligence. Apart from holding many Microsoft credentials, he is also a Microsoft Most Valuable Professional (MVP) awardee on SQL Server. He is also one of the first Microsoft Certified Trainer in India. Amit has worked with Microsoft in India and US as a Subject Matter Expert in various capacities participating in OD sessions, technical reviewing, etc. He is also a very active community lead in Asia Pacific. Visit www.amitbansal.net to know more. Subscribe to Amit’s blog and be a member of his technical forum at www.SQLServerGeeks.com - an exclusive SQL Server portal. Visit www.peoplewareindia.com for Corporate Training solutions. FaceBook – http://www.facebook.com/people/Amit-Bansal/525339346 LinkedIn – http://www.linkedin.com/pub/amit-bansal/7/121/755
View Amit Bansal 's profile
New thing... Good !
ncy trick sir
Thanks
Very nice, simple explanation.
Neat function, but why couldn't you just do this?:
select @tablename = 'select * from [' + name + '];'
select
@tablename =
'select * from ['
+
name
'];'
Do you know of a scenario where the quotname funtion should be used exclusively?
Hi Jaosn, sorry for the late reply.. - no i cant think of any specific right now.. just that the function seems to be easy to use,,
Thanks PM, Hitesh & Brent - glad you liked the content !
I love this function :) Very handy!!
Leave a comment