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 2012 Forums
 Transact-SQL (2012)
 Dynamic SQL insert with quotes

Author  Topic 

Luuk123
Yak Posting Veteran

52 Posts

Posted - 2014-03-31 : 11:12:07
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
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-03-31 : 13:38:26
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 - 2014-04-01 : 02:54:25
Thanks!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-04-01 : 06:31:56
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
   

- Advertisement -