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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Dynamic SQL insert with quotes
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Luuk123
Yak Posting Veteran

52 Posts

Posted - 03/31/2014 :  11:12:07  Show Profile  Reply with Quote
Hi all,

I want to insert a string which contains a quotation mark like ' with dynamic sql.
For example:



declare @sql varchar(1000), @string varchar(100)

select @string = 'O'Sullivan';

set @sql = 'select ' + @string + '';

insert into person (name)
exec (@sql)



How can I insert the name correctly?

Thanks!

webfred
Flowing Fount of Yak Knowledge

Germany
8781 Posts

Posted - 03/31/2014 :  13:38:26  Show Profile  Visit webfred's Homepage  Reply with Quote
escape the single quote with another single quote:
select @string = 'O''Sullivan';



Too old to Rock'n'Roll too young to die.
Go to Top of Page

Luuk123
Yak Posting Veteran

52 Posts

Posted - 04/01/2014 :  02:54:25  Show Profile  Reply with Quote
Thanks!
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22864 Posts

Posted - 04/01/2014 :  06:31:56  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also read this post http://beyondrelational.com/modules/2/blogs/70/posts/10827/understanding-single-quotes.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000