By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,856 Members | 2,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,856 IT Pros & Developers. It's quick & easy.

Using Single Quotes in a dynamic SQL

P: 31
Hi I want to know how can we use single quotted string as a part of dynamic sql
Consider the following code

exec('select branchid from '+@tablename+' where Entrystatus='NEW' AND BranchName= '+@branchname')
In the above query New is string like 'NEW'and BranchName is also a string so how am I suppose to write single quotes?
wht is the main funda behind using them?
thnks
Apr 21 '09 #1
Share this Question
Share on Google+
5 Replies


Uncle Dickie
P: 67
Does this article help?
Apr 21 '09 #2

P: 13
exec('select branchid from '+@tablename+' where Entrystatus=''NEW'' AND BranchName= '+@branchname')

Just change 'NEW' to ''NEW'' and try it...

Reg,
JK
Apr 21 '09 #3

Delerna
Expert 100+
P: 1,134
Expand|Select|Wrap|Line Numbers
  1. exec(
  2.    'select branchid 
  3.    from '+@tablename+' 
  4.    where Entrystatus=' + char(39)+'NEW' +char(39) +'
  5.    AND BranchName= '+ char(39) + @branchname + char(39)
  6. )
  7.  
or this might look cleaner to read
Expand|Select|Wrap|Line Numbers
  1.  
  2. declare @New
  3. set @New=char(39) + 'NEW' + char(39)
  4. set @branchname=char(39) + @branchname + char(39)
  5. exec('select branchid from ' + @tablename + ' where Entrystatus=' + @New +' AND BranchName= ' + @branchname)
  6.  
39 is the ASCII code for a single quote
Apr 22 '09 #4

Delerna
Expert 100+
P: 1,134
and as Uncle dickie highlights

this
select 'test '''+'hello'+''' test'

also works
interresting !
Apr 22 '09 #5

P: 31
thnks for the solution
Apr 23 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.