Solved

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

Posted on 2011-02-24
3
655 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

729 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