I have some varbinary data stored in a table in MS Sql Server 2005. Does anyone have SQL code that takes a query as input (lets say the query guarantees that a single column of varbinary is returned) and outputs the bytes to disk (one file per row?) I'm sure this has been asked a thousand times before, but Googling comes up with mostly .net solutions. I want an SQL solution.
9条答案
按热度按时间p8ekf7hl1#
The BCP approach does not work for me. The bytes it writes to disk cannot be deserialized back to the .net objects I stored. This means that the bytes on disk aren't equivalent to what's stored. Perhaps BCP is writing some kind of header. I'm not sure.
I found the following code here at the bottom of the article. It works great! Although it was intended for stored BMP images, it works with any varbinary.
dwthyt8l2#
I am adding this to build on JohnOpincar's answer , so that others who want to use LinqPad can get a working solution faster.
hpxqektj3#
You can use BCP, not T-SQL, but works well.
fcwjkofz4#
I know it's an old post, but I figured out why the following doesn't work and how to fix it:
The reason is bcp put Prefix Length at the very beginning of the file. It is either 4 bytes or 8 bytes, depends on data type of FileContent column (text, ntext, image: 4 varchar(max), varbinary(max) : 8 Refer to https://msdn.microsoft.com/en-us/library/ms190779.aspx )
Use a binary editor, like the one in Visual Studio, to remove the prefix bytes, and everything runs perfectly. :-)
cbjzeqam5#
If you have linqpad, this works:
8cdiaqws6#
With Powershell
flmtquvp7#
Just an alternative. You can use the freeware Toad for SQL server and save directly from the editor.
You can go to their website https://www.toadworld.com and get the freeware there or a 30 day trial of the full version. Its under Download and pick Toad for SQL server.
You do a regular select statement in Toad on the line that has the image you want to save. When you see the results you can click on the byte image column and on the right you see a PDF tab if this is a PDF document or on the left you see a Image tab. When you click the tab you can see the save logo at the bottom to save the image or file.
pkln4tw68#
You can easy do that in Powershell:
qrjkbowd9#
SQL is designed to work with database objects, so from it's point of view anything else doesn't exists. Sure, there are extended procedures like
xp_cmdshell
that allow you interact with the operating system, but they are proprietary extensions and not part of T-SQL.Maybe the closest approach would be using the FILESTREAM attribute for binary types of SQL Server 2008, which allow storing some columns directly as files in a folder instead of using the database:
FILESTREAM overview
Note that the FILESTREAM storage is designed for maintain large files out of the database in order to increase performance, and not for allowing direct access to files (i.e. T-SQL still doesn't have the concept of a filesystem). I my opinion, direct access to the filesystem from SQL will defeat some of the purposes of a database (mainly having data stored in a structured way).
So I would recommend following the advice of Dustin and use a tool like BCP or any other data dumper.