1

タイプxmlの列を持つテーブルがあります。このテーブルからデータを抽出し、そのデータを別の環境にロードする必要があります。bcp を使用してターゲット テーブルを抽出およびロードしていますが、特殊文字をターゲット テーブルに bcp するときに問題を引き起こしている特殊文字がいくつかあります。回避策はありますか

ありがとうベン

4

1 に答える 1

3

カスタム CLR-SP が最適なソリューションを提供してくれました。SQL サービス アカウントにファイルへのアクセス許可があれば、XML 型のデータを TSQL からファイルに直接書き込むことができるようになりました。これにより、単純な構文が可能になります。

exec dbo.clr_xml2file @xml, @path, @bool_overwrite

SP:

CREATE PROCEDURE [dbo].[clr_xml2file]
    @xml [xml],
    @file [nvarchar](max),
    @overwrite [bit]
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [CLR_FileIO].[FreddyB.FileIO].[Xml2File]

CLR DLL の C#:

using System;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using Microsoft.SqlServer.Server;

namespace FreddyB
{  
  public class FileIO
  {
    public static void Xml2File(
      SqlXml xml, 
      SqlString sFile, 
      SqlBoolean bOverwrite
    ) {

      SqlPipe sqlpipe = SqlContext.Pipe;
      try
      {
        if (xml == null || xml.IsNull || xml.Value.Length == 0) {
          sqlpipe.Send("Cannot write empty content to file : \n\t"
            +sFile.Value);
          return;
        }

        if (File.Exists(sFile.Value) & bOverwrite.IsFalse) {
          sqlpipe.Send("File already exists : \n\t"+sFile.Value);
          return;
        }

        int iFileSize = 0;
        FileStream fs = null;
        try {
          byte[] ba = Encoding.UTF8.GetBytes(xml.Value);
          iFileSize = ba.Length;

          fs = new FileStream(sFile.Value, FileMode.Create, FileAccess.Write);
          fs.Write(ba, 0, ba.Length);

          sqlpipe.Send("Wrote "
            +String.Format("{0:0,0.0}",iFileSize/1024)
            +" KB to : \n\t"
            +sFile.Value);
        }
        catch (Exception ex) 
        {
          sqlpipe.Send("Error as '"
            +WindowsIdentity.GetCurrent().Name
            +"' during file write : \n\t"
            +ex.Message);
          sqlpipe.Send("Stack trace : \n"+ex.StackTrace);
        }
        finally
        {
          if (fs != null) {
            fs.Close();
          }
        }
      }
      catch (Exception ex)
      {
        sqlpipe.Send("Error writing to file : \n\t"
          +ex.Message);
      }
    }
  }
}
于 2009-01-16T17:03:29.170 に答える