Please start any new threads on our new site at 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
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  

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?


Flowing Fount of Yak Knowledge

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

Yak Posting Veteran

52 Posts

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

Premature Yak Congratulator

22864 Posts

Posted - 04/01/2014 :  06:31:56  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Also read this post


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.03 seconds. Powered By: Snitz Forums 2000