Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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