Solved

How to insert single quote values using dynamic sql during import process

Posted on 2011-02-24
3
649 Views
Last Modified: 2012-06-27
Below is how i have my dynamic sql: I am inserting the values into the field through the import process. Out of 1000 records in a file, for 300 of them i had apostrophe. Below sql is working only if the value doesn't have apostrophe.

declare @SQL as nvarchar(1000)
set @SQL = N'exec USP_TABLE_ADD @ID=''' + CAST(@ID as nvarchar(100)) + ''', @VALUE=''' + @VAL + ''''
exec sp_executesql @SQL

Any help will be greatly appreciated!

Thanks,
Sam



0
Comment
Question by:Giftcard
  • 2
3 Comments
 
LVL 11

Expert Comment

by:dougaug
ID: 34976175
Try this:

SET QUOTED_IDENTIFIER OFF

declare @SQL as nvarchar(1000)
set @SQL = N'exec USP_TABLE_ADD @ID='"' + CAST(@ID as nvarchar(100)) + '"', @VALUE='"' + @VAL + '"''
exec sp_executesql @SQL

0
 
LVL 57

Accepted Solution

by:
Raja Jegan R earned 500 total points
ID: 34976186
You need to use REPLACE function like this
declare @SQL nvarchar(1000)
DECLARE @id INT;
DECLARE @val NVARCHAR(100);

SET @id = 10
SET @val = 'a''bc'
set @SQL = N'exec USP_TABLE_ADD @ID=''' + CAST(@ID as nvarchar(100)) + ''', CAST(@VALUE as nvarchar(100))=''' + REPLACE(@VAL, '''' , '''''') + ''''
PRINT @sql

exec sp_executesql @SQL

-- And your code would be

declare @SQL nvarchar(1000)
set @SQL = N'exec USP_TABLE_ADD @ID=''' + CAST(@ID as nvarchar(100)) + ''', CAST(@VALUE as nvarchar(100))=''' + REPLACE(@VAL, '''' , '''''') + ''''
exec sp_executesql @SQL

Open in new window

0
 
LVL 11

Expert Comment

by:dougaug
ID: 34976206
Follow another version of my previous code:

SET QUOTED_IDENTIFIER OFF

declare @SQL as nvarchar(1000)
set @SQL = N'exec USP_TABLE_ADD @ID="' + CAST(@ID as nvarchar(100)) + '", @VALUE="' + @VAL + '"'
exec sp_executesql @SQL

0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Auditing in Azure SQL Database 3 38
SQL Maintenance Plan 3 29
SQL Exceptions 3 35
How to pass 2 IN parameters to a stored procedure using SQL Server 2008? 6 24
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now