RTF从SQL Server 2008中提取

时间:2012-04-17 11:16:59

标签: sql sql-server-2008 rtf bcp

我有一个我想要提取的专栏,但我遇到了问题!该列存储为类型ntext并包含RTF文档,如下所示:

{\rtf1\ansi\ansicpg1252\uc1\deff0{\fonttbl  {\f0\fnil\fcharset0\fprq2 Arial;}  {\f1\fswiss\fcharset0\fprq2 Arial;}  {\f2\froman\fcharset2\fprq2 Symbol;}}  {\colortbl;\red0\green0\blue0;\red255\green255\blue255;}  {\stylesheet{\s0\itap0\nowidctlpar\f0\fs24 [Normal];}{\*\cs10\additive Default Paragraph Font;}}  {\*\generator TX_RTF32 15.0.530.502;}  \deftab1134\paperw11909\paperh16834\margl1138\margt1138\margr1138\margb1138\widowctrl\formshade\sectd  \headery720\footery720\pgwsxn11909\pghsxn16834\marglsxn1134\margtsxn1134\margrsxn1134\margbsxn1134\pard\itap0\nowidctlpar\plain\f1\fs20 Stephan Bos  28/11/2011 11:19:55\par\par Sold in guy. He likes him, feedback this afternoon.\par Will send him the CV and also our terms.\par Made him aware of our fees.\par }

但是我想把它提取回来(rtf或者txt我真的不介意) 我已经尝试过使用BCP,它已经成功地提取了文档,但它们最终与列完全相同,但每个字符之间有空格而不是我所期望的(上面的示例最终会读取类似的内容:

Stephan Bos  28/11/2011 11:19:55
Sold in guy. He likes him, feedback this afternoon.
Will send him the CV and also our terms.
Made him aware of our fees.

我正在使用的BCP提取(正在提取)如下:

set nocount on;
Declare @sql varchar(1000);
declare @noteid int;
declare xx1 cursor for select nic.NotebookItemId from NotebookItemContent nic
inner join NotebookLinks nl on nl.NotebookItemId = nic.NotebookItemId
inner join NotebookItems ni on ni.NotebookItemId = nic.NotebookItemId
where nl.clientid = 1235074
AND ni.NotebookTypeId = 56;
open xx1;
fetch xx1 into @noteid;
while (@@fetch_status = 0)
begin
set @sql = 'BCP "SELECT memo FROM Monarch_Pronet_ITOIL.dbo.notebookitemcontent where notebookitemid=' + cast(@noteid as varchar) + 
'" QUERYOUT \\bhamws475\docs\' + cast(@noteid as varchar) + '.rtf -T -f \\bhamws475\docs\bcp.fmt -S ' + @@SERVERNAME
EXEC master.dbo.xp_CmdShell @sql
fetch xx1 into @noteid;
end;
close xx1;
deallocate xx1;

有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

我想我现在明白了 - 问题是BCP保存的RTF不被Word识别为RTF文件 - 它是作为纯文本文件打开的。

这是因为导出的文件是Unicode格式(你可以看到,每个字符在屏幕截图中后跟一个空格)。

解决方法是告诉bcp不要保存在Unicode中 - 我认为可以使用-c开关或在格式文件中指定所需的字符集。