Friday, November 21, 2008

How to Import, Store and Export PDF Text Image PPT Word Files in SQL Server

We may get this requirement of storing PDF documents or Text or Image or PPT or Word in SQL Server. You can use below code to do so...

-- Create Sample Table
CREATE TABLE tblPDFData (pdf_data VARBINARY(MAX));
GO

-- Get PDF Binary Data into SQL Table
INSERT INTO tblPDFData (pdf_data)
SELECT pdf_data
FROM OPENROWSET(BULK N'E:\DLO.pdf',SINGLE_BLOB) AS ImageSource(pdf_data);
GO

-- Generate PDF from Table
DECLARE @SQLcommand NVARCHAR(1000)
SET @SQLcommand = 'BCP "SELECT pdf_data FROM mydb.dbo.tblPDFData" QUERYOUT "E:\PDFOutput.pdf" -T -n'
EXEC master.sys.xp_cmdshell @SQLcommand
GO

No comments:

Post a Comment

Please do not spam!