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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Quotes, Single vs. Double

Author  Topic 

skillile
Posting Yak Master

208 Posts

Posted - 2002-09-04 : 21:24:38
Can someone please help. I am getting all messed up when to use single or can I use double " ' quotes in dynamic sql. What are the rules. I get this:

DECLARE @name varchar (20)
SET @name = 'Bill'
SELECT 'NAME= ' + ' @name + ' blah'

I am using open query and everything must be inside ' ' when I need to insert @vars I get all confused what is the best way to write something in dynamic sql. Here is a sample of my question.


SET @sqlstring= 'SELECT * from openquery([' + @servername + '], ''select distinct '''''+
@catalogname + ''''', ' +
convert(varchar(5),@serverid) +
', prin.iFolder,
prin.iAddress,
cf.cCustCode,
prin.cParty,
cf.tName,
cf.tAddress,
cf.tCity,
cf.tState,
cf.tZipCode,
cf.tPhone,
cf.tFax,
cf.tContact,
cf.iParent,
cf.cTaxId,
prin.csource,
prin.ctype,
''''' + @dt +'''''

from [' + @catalogname +'].dbo.Prin as prin
left Join [' + @catalogname +'].dbo.Card as cf on prin.iAdd = cf.iAd
where prin.cso=' + '''''P'''''')'


this works but I don't understand all the ''''''''.

Thanks

Onamuji
Aged Yak Warrior

504 Posts

Posted - 2002-09-04 : 21:46:17
Double Quotes: used like old fashion (???) [Column Name]
Single Quotes: represent strings

QUOTENAME(): a nifty function that escapes single quotes (meaing: turns all ' in a string to '')

Go to Top of Page

Crespo

85 Posts

Posted - 2002-09-05 : 04:39:30
It is a method used in Transact SQL to escape a particular character which in this case is the double quotes.

Of course you can use the function mentioned by the other eply, but I recommend the more simpler method used in your query already.

Good Luck!

Crespo.
Hewitt Bacon & Woodrow
Epsom
Surrey
United Kingdom
Go to Top of Page
   

- Advertisement -