Tuesday, 20 December 2011

How to Export Image column to file on MSSQL II

Another way how to export binary data from database. It is possible to use bcp utility.

bcp "SELECT img FROM db_name.dbo.Images WHERE ID = 1" queryout "c:\001.jpg" -T -S server

Once you run it you will be asked for the file storage type (no change required), prefix-length (needs to be 0), length of field and field terminator (no change)

Wednesday, 30 November 2011

Update of XML data

Code snippet for updating specific data in xml column in mssql

declare @date datetime
set @date = GETDATE()

UPDATE XmlDocuments
SET    xmlmetadata.modify ('replace value of (/Header/Date/text())[1] with sql:variable("@date")') 
WHERE ID = '1000001'

Friday, 16 September 2011

Bulk update of Image data in MSSQL

This is just simple example how to do update of image data in Microsoft SQL database.

UPDATE Images SET Img = (SELECT BulkColumn AS Img FROM OPENROWSET(BULK N'C:\NoImage.TIF', SINGLE_BLOB) AS [Document])

Friday, 1 July 2011

How to Export Image column to file on MSSQL

It is not that easy how it looks like:) Used the same approach as it is in the answer below but completed the example.

1. Enable the extended stored procedures:
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO

2. Use sp_OA stored procedures

DECLARE @objStream INT
DECLARE @imageBinary VARBINARY(MAX)
DECLARE @filePath VARCHAR(8000)

SELECT @imageBinary = img
FROM Images
WHERE ID = 1

SET @filePath = 'c:\img_1.jpg'

EXEC sp_OACreate 'ADODB.Stream', @objStream OUTPUT
EXEC sp_OASetProperty @objStream, 'Type', 1
EXEC sp_OAMethod @objStream, 'Open'
EXEC sp_OAMethod @objStream, 'Write', NULL, @imageBinary
EXEC sp_OAMethod @objStream, 'SaveToFile', NULL,@filePath, 2
EXEC sp_OAMethod @objStream, 'Close'
EXEC sp_OADestroy @objStream 

Resources:
How to export a ms sql image column to a file

Saturday, 18 June 2011

Wednesday, 4 August 2010

How to get database data file path

Recently I needed to create universal script which added new file group and file to database regardless of where the MSSQL server was installed. The reason for creating new file was to split data which were changing a lot (table with cached documents) and data which were more or less static.

There was necessary to find out where is located primary file for current database and then to get just directory where the file was located.
DECLARE @PATH nvarchar(max)

SELECT @PATH = LEFT(filename, LEN(filename) + 1 - CHARINDEX('\', REVERSE(filename)))
FROM  master.dbo.sysaltfiles WHERE db_name(dbid) = db_name() AND name = 'PrimaryFileName'

Monday, 10 May 2010

Database Project in Visual Studio 2008

There exists database project in Database Developer edition of Visual Studio 2008. It can be very useful but also painful. The original version needs to have installed local database server for this kind of project but a lot of things can go wrong way. There is a lot of troubleshooting articles on Internet so you think it helps you to resolve the problems quickly - unfortunately not in my case. I have spent almost a day without success until I have found Microsoft® Visual Studio Team System 2008 Database Edition GDR R2. It took me less then one hour to install the package, convert old database project and deploy project. Package installs new database project which doesn't need database server on your local. That was really great and I recommend it GDR to everyone struggling with the original database project.