Solved

Appending a text file with & and ' in the fields and double quots

Posted on 2012-12-27
3
296 Views
Last Modified: 2012-12-30
Hello.

When I execute the below, my rrecords append to the correct file - they all begin and end with double quotes:

DECLARE @var1 varchar(4000);
SELECT @var1 = 'echo "' + col1 + CHAR(9) + col2 + char(9) +  col3 + '" >>\\pathwhere\fileexists.txt' FROM Table1 WITH(NOLOCK);
EXEC master..xp_cmdshell @var1;

When I remove the double quotes, none of my records with ampersands and single quotes print - instead, this error is logged:  'fieldname' is not recognized as an internal or external command.

When I restore the double quotes, my fieldnames print but every row begins and ends with double quotes.

My customer can not have the double quote but still needs the data with ampersands and quotes in the tite field to print.  (ex  Micky & Minnie)

How can I either escape the double quotes or modify my statement so my titles with ampersands and single quotes append to my file?
0
Comment
Question by:programmher
3 Comments
 
LVL 9

Expert Comment

by:shorvath
ID: 38725807
sample data?
0
 
LVL 12

Accepted Solution

by:
sachitjain earned 150 total points
ID: 38725931
I guess issue is only with ampersand character and not with single quotes and that could be resolved by replacing each & character with ^&.

Like this:

DECLARE @var1 varchar(4000);
SELECT @var1 = 'echo ' + replace(col1, '&', '^&') + CHAR(9) + replace(col2, '&', '^&')
            + char(9) +  replace(col3, '&', '^&') + ' >>\\pathwhere\fileexists.txt' FROM Table1;
EXEC master..xp_cmdshell @var1;
0
 
LVL 12

Expert Comment

by:Saurabh Bhadauria
ID: 38726779
Optionally you can use SQLCMD ......


Create table  temp (Col1 nvarchar(20),col2 nvarchar(20)) 

insert into temp
select 'Saurabh & bhadauria' , 'Saurabh " bhadauria '

DECLARE @var1 varchar(4000);
SELECT @var1 = 'SqlCMD  -S.   -E   -h    -1  -q " set nocount on select col1 + CHAR(9) + col2  from   test_sa.dbo.temp ; "    -o D:\33.txt'
EXEC master..xp_cmdshell @var1; 

Open in new window


Here I have suppressed headers with  ( -h -1)  ..... and  row-count  summary with  Set NoCount ON ...

This will produced desired result...

look at below link for full details...
http://msdn.microsoft.com/en-us/library/ms165702(v=sql.105).aspx
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

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

13 Experts available now in Live!

Get 1:1 Help Now