Pages

Thursday, May 7, 2015

How import and export document column data?

Execute the following Microsoft SQL Server T-SQL script to demonstrate character-based document import and export.
------------
-- SQL Server import/export character-based large objects (CLOBs)
-- File types .txt, .doc, .xls, .xlsx, .pdf, etc.
------------
-- Create file K:\temp\CLOBTest.txt with notepad: 10 lines of 0123456789
USE tempdb;
GO
CREATE TABLE TestCLOB( ID INT IDENTITY(1,1) PRIMARY KEY, Doc nvarchar(max) NOT NULL)
GO
-- Import CLOB
INSERT INTO TestCLOB(Doc)
SELECT *
FROM OPENROWSET (
BULK 'K:\temp\CLOBTest.txt', SINGLE_CLOB) AS CharFile
GO

-- Export CLOB
DECLARE @BCPcommand NVARCHAR(1024)
SET @BCPcommand = 'bcp "SELECT TOP 1 Doc FROM tempdb.dbo.TestCLOB" queryout "K:\temp\CLOBTestOut.txt"  -T -c -C RAW -SYOURSERVER'
EXEC xp_cmdshell @BCPcommand
GO
DROP  TABLE TestCLOB
------------

No comments:

Post a Comment