Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 why so many single quotes ' in dynamic sql

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2012-09-23 : 09:20:13
hi,

I just started learning dynamic sql from free online sources. I'm confused with the multiple quotes on those examples.

Is there any logic behind those quotes?

Here is one example :

DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)
SET @columnList = 'CustomerID, ContactName, City'
SET @city = '''London'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM customers WHERE City = ' + @city
EXEC (@sqlCommand)


Why have they put three quotes in London?

bitsmed
Aged Yak Warrior

545 Posts

Posted - 2012-09-23 : 10:11:03
quote:
Originally posted by learning_grsql

hi,

I just started learning dynamic sql from free online sources. I'm confused with the multiple quotes on those examples.

Is there any logic behind those quotes?

Here is one example :

DECLARE @sqlCommand varchar(1000)
DECLARE @columnList varchar(75)
DECLARE @city varchar(75)
SET @columnList = 'CustomerID, ContactName, City'
SET @city = '''London'''
SET @sqlCommand = 'SELECT ' + @columnList + ' FROM customers WHERE City = ' + @city
EXEC (@sqlCommand)


Why have they put three quotes in London?


When inside the string definition, two single quotes will convert to one single quote.
Lets say you want a string to hold the value I am a sql wiz, you'd just type:
SET @description='I am a sql wiz'

Now, if you want the string to hold I'm a sql wiz, you'd type:
SET @description='I''m a sql wiz'

In the example above:
Red quotes are start/stop quotes for a given string.
Green quotes are inside the given string, and will thus convert to a single quote.

So in essence, the city variable in your example, is set to 'London' in order for the select statement to work, as you would write:
SELECT CustomerID, ContactName, City FROM customers WHERE City = 'London'

Hope that you understand, if not let me know, and I'll explain another way.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-23 : 19:44:19
see

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/02/19/understanding-single-quotes.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

learning_grsql
Posting Yak Master

230 Posts

Posted - 2012-09-24 : 03:33:42
Many Thanks bitsmed and Visakh.

Now I feel like I have understood quotes completely.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-09-24 : 13:04:28
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -