2013-04-04 30 views
1

我正在嘗試使用Starfish ETL從Goldmine 5.5遷移到MCRM 2011。SQL查詢檢索我想要的值,存儲到一行中

問題是在Goldmine數據庫中有一個名爲Mailbox的表,其中需要遷移傳入電子郵件的所有信息都存儲在名爲rfc822(完整標題和正文消息)的單個行中。即使在其他表中,發件人/收件人/等也沒有行。

我在我的ETL軟件中使用此查詢作爲Origin來從原始數據庫(Goldmine)獲取我需要的信息,但Starfish提供的默認地圖在我的情況下不起作用。

SELECT ch.*, c1.PHONE1, mb.RFC822 
FROM CONTHIST ch 
     INNER JOIN CONTACT1 c1 
      ON ch.ACCOUNTNO=c1.ACCOUNTNO 
     INNER JOIN MAILBOX mb 
      ON ch.recid=mb.LINKRECID 
WHERE ch.RECTYPE='MI' 

之後,我可以映射目標MCRM電子郵件表和添加功能字段(VBScript,C#)。

大多數情況下,一旦作業完成,發件人/收件人將被還原爲錯誤的值(例如,BCC字段中的用戶)。

我想知道的是:

  • 我如何提取CC和BCC字段,其中存儲了RFC822行
  • 有在HTML和純格式的電子郵件/文本,唯一的區別就是在這一行查找它。此外,Dynamics不會以相同的方式存儲它。
  • 如何使用SQL檢索發件人和收件人?

我認爲必須有一種方法來完成SQL查詢的工作。

+0

沒人?只是鏈接到博客文章或讓我知道這是不可能的,複雜或我應該嘗試在VB或C#...基本答案將不勝感激,只有自己有一個點從哪裏開始。 .. 謝謝! – 2013-04-05 14:57:38

回答

0

的GoldMine 5.5(哇!) - 我的哀悼(BOT其實,你是幸運的,因爲在GoldMine的RFC822領域的未來版本進行加密以及)

我看到兩個選項來解決問題(所有需要SQL Server 2005或以上):

  1. 與SQLCLR集成,能夠使用正則表達式解析RFC822場
  2. ,或者如果你想使用SQL的GoldMine API)來獲取電子郵件中使用GoldMineAPI(並集成了SQLCLR通過業務邏輯功能

我有兩個選項的一些例子:使用SQLCLR來解析HTML

我寫這一次解析ContHist注意到了的一個例子

例如,

GoldMine 9。0

SQLCLR函數(C#)

[SqlFunction()] 
public static SqlString RemoveHTML(SqlString html) 
{ 
    if (!html.IsNull) 
    { 
     string remove_style = @"\<STYLE((\S|\ |\s)+?)\<\/STYLE\>"; 
     string remove_html = @"(?></?\w+)(?>(?:[^>'""]+|'[^']*'|""[^""]*"")*)>"; 

     string result = Regex.Replace(html.Value, remove_style, string.Empty, RegexOptions.IgnoreCase); 
     result = Regex.Replace(result, remove_html, string.Empty, RegexOptions.IgnoreCase); 

     return result; 
    } 
    else return new SqlString(); 
} 

安裝到DB

USE YourDB 
sp_configure 'show advanced options', 1; 
GO 
RECONFIGURE; 
GO 
sp_configure 'clr enabled', 1; 
GO 
RECONFIGURE; 
GO 

ALTER DATABASE YourDB SET TRUSTWORTHY ON 
---- 
CREATE ASSEMBLY GMSQLCLRAPI 
FROM 'DRIVE:\Path\To\DLL\dll_name.dll' 
with permission_set = UNSAFE; 
-- 
CREATE FUNCTION RemoveHTML(@html NVARCHAR(MAX)) 
RETURNS NVARCHAR(MAX) WITH EXECUTE AS CALLER 
AS EXTERNAL NAME GMSQLCLRAPI.[CLSQLCLR.GMSQLCLRAPI].RemoveHTML 

用法

select dbo.RemoveHTML(rfc822) from Mailbox 

使用這種方法,您可以將SQLCLR函數中的正則表達式替換爲您自己的,它將爲您解析收件人。

和示例使用的GoldMine API的

實際上有相當多的代碼,我將提供幾件

С#包裝函數來調用readmail將GoldMine的API函數

public Dictionary<string, string> ReadMail(Dictionary<string, string> NVDictionary) 
{ 
    const string failure = "Failure to read email\n"; 
    Int32 res; 
    Dictionary<string, string> result = null; 

    IntPtr nvc = import.GMW_NV_Create(); 
    if (nvc == null) throw (new CLGMAPIException(failure + "Unable to create name/value container")); 
    try 
    { 
     foreach (KeyValuePair<string, string> kvp in NVDictionary) 
     { 
      import.GMW_NV_SetValue(nvc, kvp.Key, kvp.Value); 
     } 

     res = import.GMW_Execute("ReadMail", nvc); 

     if (res > 0) 
     { 
      int count = import.GMW_NV_Count(nvc); 
      result = new Dictionary<string, string>(count); 
      IntPtr ptr; 
      string name; 

      for (int i = 0; i < count; i++) 
      { 
       ptr = import.GMW_NV_GetNameFromIndex(nvc, i); 
       name = Marshal.PtrToStringAnsi(ptr); 

       ptr = import.GMW_NV_GetValue(nvc, name, string.Empty); 
       result[name] = Marshal.PtrToStringAnsi(ptr); 
      } 
     } 
    } 
    finally 
    { 
     import.GMW_NV_Delete(nvc); 
    } 

    switch (res) 
    { 
     case 0: throw (new CLGMAPIException(failure)); 
     case -1: throw (new CLGMAPIException(failure + "Message is private")); 
     case -2: throw (new CLGMAPIException(failure + "Message not found, or cannot be loaded")); 
     case -3: throw (new CLGMAPIException(failure + "Exception")); 
     default: 
      break; 
    } 

    return result; 
} 

包裝用法

public GoldMineEmails(GoldMineAPI api, DataTable mailbox) 
{ 
    foreach (DataRow emailRow in mailbox.Rows) 
    { 
     var dict = new Dictionary<string, string>(3); 
     dict["MboxRecID"] = emailRow["recid"].ToString(); 
     dict["History"] = "1"; 

     try 
     { 
      var res = api.ReadMail(dict); 

      var email = new GoldMineEmail(res["To"], res["Cc"], res["Bcc"], res["Outgoing"]) 
      { 
       body = res["Body"], 
       From = res["From"], 
       subject = res["Subject"], 
       user = res["User"] 
      }; 

      this.Add(email); 
     } 
     catch 
     { 
      throw; 
      // error logging 
     } 
    } 
} 

我希望這有助於。 不要猶豫與我聯繫 - 我可以給你發送源代碼的例子