Export Image column from SQL Server 2000 using BCP - sql-server

I've been tasked with extracting some data from an SQL Server 2000 database into a flat format on disk. I've little SQL Server experience.
There is a table which contains files stored in an "IMAGE" type column, together with an nvarchar column storing the filename.
It looks like there are numerous types of files stored in the table: Word docs, XLS, TIF, txt, zip files, etc.
I'm trying to extract just one row using BCP, doing something like this:
bcp "select file from attachments where id = 1234" queryout "c:\myfile.doc" -S <host> -T -n
This saves a file, but it is corrupt and I can't open it with Word. When I open the file with word, I can see a lot of the text, but I also get a lot of un-renderable characters. I've similar issues when trying to extract image files, e.g. TIF. Photo software won't open the files.
I presume I'm hitting some kind of character encoding problems.
I've played around with the -C (e.g. trying RAW) and -n options within BCP, but still can't get it to work.
The table in SQL Serer has a collation of "SQL_Latin1_General_CP1_CI_AS"
I'm running BCP remotely from my Windows 7 desktop.
Any idea where I'm going wrong? Any help greatly appreciated.

I got this working by changing the default options which BCP asks you about when you invoke the command:
The one that made the difference was changing the prefix-length field from 4 to 0.

bcp "select file from attachments where id = 1234" queryout "c:\myfile.doc" -S -T -n
after this
[image] : I (enter capital "I"]
0
0
Enter
save file Y
kallas.. ur file is there

Related

bcp utility for Microsoft SQL Server ignores odd rows

I am trying to import a bunch of hashes into a SQL DB for testing purposes. The hashes are in the form of a text file looking like the following:
hash1
hash2
hash3
hash4
hash5
hash6
Now, for some reason, when I run the command bcp dbo.Table IN "Path\To\Dump.txt" -T -S SERVER\INSTANCE -d DB -c -t \n what happens is that every second hash is imported (i.e. hash2, hash4, and hash6). I have no idea why this is happening. Similarly, if I replace the delimiter argument with just \r or, \n\r, or \r\n, 0 rows get imported. Why is this happening?
It turns out I should have been referencing the View representing my hashes column instead of the whole table itself. That fixed things.

SQL Server bcp command to csv file

We are using the SQL Server bcp command to export data to .csv file with the option -t ,. It works for some tables, but there are some columns that have comma in content, and the exported data are not correct. According to csv definition, if the content has comma in itself, it should be surround by double quotes.
For example, the content
hello,world,
should be exported as
"hello,world"
But bcp doesn't do that - how to resolve this issue?
Thanks,
Eric
If you're doing this for just a few tables/queries, you can escape those strings from a query. Like say you have a field x in a table y, you query would be:
SELECT '"'+REPLACE(x,'"','""')+'"' FROM y
There are no options in bcp (or sqlcmd) to automatically do this for you.

Bulk load to HDFS from sybase database

I need to load data from sybase(production database) to HDFS. By using sqoop it is taking very long time and frequently hit the production database. So, I am thinking to create data files from sybase dump and after that copy the data files to hdfs. Is there any tool(open source) is available to create required data files(flat files) from sybase dump.
Thanks,
The iq_bcp command line utility is designed to do this on a per table basis. You just need to generate a list of tables, and you can iterate through the list.
iq_bcp [ [ database_name. ] owner. ] table_name { in | out } datafile
iq_bcp MyDB..MyTable out MyTable.csv -c -t#$#
-c specifies a character (plaintext) output
-t allows you to customize the column delimiter. You will want to use a character or series of characters that do not appear in your extact e.g. if you have a text column that contains text with a comma, a csv will be tricky to import without additional work.
Sybase IQ: iq_bcp

Exporting Specific Columns,Specific Rows from a Specific Table of a Specific Database in Mysql

I need to export only subset of columns from a very large table containing large number of columns.Also this table contains million of rows so i want to export only specific rows from this table.
I have recently started using Mysql earlier i was working on Oracle.
This worked for me:
mysql -u USERNAME --password=PASSWORD --database=DATABASE \
--execute='SELECT `field_1`, `field_2` FROM `table_name`' -X > file.xml
And then importing the file, using command:
LOAD XML LOCAL INFILE '/pathtofile/file.xml'
INTO TABLE table_name(field_1, field_2, ...);
What format do you need the data in? You could get a CSV using a query. For example
SELECT column1,column2,column3,... FROM table WHERE column1=criteria1,....
INTO OUTFILE '/tmp/output.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
http://www.tech-recipes.com/rx/1475/save-mysql-query-results-into-a-text-or-csv-file/
An administration tool like phpMyAdmin (http://www.phpmyadmin.net/) could also be used to run the query and then export the results in a variety of formats.

How do I use BCP or Sql Server Management Studio to get BLOB data out of Sql Server?

I'm sorry if this question has been asked already, but I couldn't find it anywhere. I have a table that stores files as BLOBS. The column that holds the file is an image datatype. I would like to be able to extract the binary data out of the column and turn it in to an actual file. I would ideally like to be able to do this with BCP or management studio if possible.
I have tried BCP, but for some reason when I try and pull out an office document Word thinks it's corrupt. Here's what I've tried so far (obviously the values have been changed to protect the innocent :):
bcp "select document_binary_data from database where id = 12345" queryout "c:\filename.doc" -n -S server -U username -P password
This isn't working though? Any thoughts?
Edit Turns out you don't need the -n native flag. Also, BCP tries to include a 4 byte prefix by default on the image column - you actually want this set to 0.
bcp "select document_binary_data from database where id = 12345" queryout "c:\filename.doc" -S server -U username -P password
Enter the file storage type of field document_binary [image]:
Enter prefix-length of field document_binary [4]: 0
Enter length of field document_binary [0]:
Enter field terminator [none]:
I'm answering my own question since I'm getting annoyed with SO telling me to setup a bounty
Turns out you don't need the -n native flag. Also, BCP tries to include a 4 byte prefix by default on the image column - you actually want this set to 0.
bcp "select document_binary_data from database where id = 12345" queryout "c:\filename.doc" -S server -U username -P password
Enter the file storage type of field document_binary [image]:
Enter prefix-length of field document_binary [4]: 0
Enter length of field document_binary [0]:
Enter field terminator [none]:
If you can use C# / .NET code to do this, the following KB article may come in handy:
http://support.microsoft.com/kb/317016
Apparently you can do something like this with BCP and a format file, but IIRC the format file has to be pre-populated with the exact number of bytes it is expecting to pull from the column, which makes it quite impractical.
Another option you might choose is to use FILESTREAM in 2008 or, if you are not planning to migrate to 2008 anytime soon, store the documents on the file system and a pointer to them in the database. Yes there are pros and cons to this, but it's the way we've chosen in all projects to date.

Resources