Solved

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

Posted on 2012-12-27
3
297 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

929 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