Solved

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

Posted on 2011-02-24
3
653 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
convert null in sql server 12 57
Master DB with Masterkey 1 37
Problem when I run a simple storeproc - help 4 30
Visual Studio hangs on running project 6 56
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 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