cancel
Showing results for 
Search instead for 
Did you mean: 

Export long binary field to text file?

0 Kudos
3,877

I try to export long binary field to text file and insert into to another database, but the export of the long binary will not be correct. I'm writing this way:

SELECT long_binary_filed from testtable;
output to 'c:\\temp\\Test1.txt' ENCODING 'utf-8';

What I'm wondering is how I can export long binary fields to a text file so I can add it to another database then with an insert?

Accepted Solutions (1)

Accepted Solutions (1)

MarkCulp
Participant

I suggest that you use xp_write_file() since it will not try to do any character conversion on the binary blob when it writes it to the file.

0 Kudos

Okay, thanks for the quick reply. If I then export this.

SELECT xp_write_file( 'c:\\temp\\Test1.txt', long_binary_filed ) FROM testtable;

Do I then read it with xp_read_file? How?

MarkCulp
Participant
0 Kudos

Yes, one way to read the file would be to use xp_read_file.

begin
  declare @blob long binary;
  set @blob = xp_read_file( 'c:\\temp\\Test1.txt' );
  ...
end;
0 Kudos

Then I do an insert (or update) like this?

INSERT INTO testtable ( long_binary_field ) VALUES ( xp_read_file( 'c:\\temp\\Test1.txt' ) );
MarkCulp
Participant
0 Kudos

Yes, that should work.

Answers (0)