简体   繁体   English

TSQL翻译CRM 2011中的ChangeData字段

[英]TSQL Translate ChangeData Field in CRM 2011

I have a delimited string which acts as a key for a target delimited string. 我有一个定界字符串,它充当目标定界字符串的键。 I need to know where 2 values are in the key sequence, and then pull only those positions from my target string. 我需要知道键序列中2个值的位置,然后仅从目标字符串中拉出那些位置。

Example 1 例子1
Key : ,15,90,104,73,95,13,14, 键:,15,90,104,73,95,13,​​14,
Target : Medium~Male~28~Green~Right~No~10/04/2013 对象:中〜男〜28〜绿〜右〜否〜10/04/2013

Example 2 例子2
Key : ,14,73,104,95,15,13,90, 键:,14,73,104,95,15,13,​​90,
Target : 12/03/2013~Green~28~Right~Medium~No~Male~ 目标:2013年12月3日〜绿色〜28〜右〜中〜不〜男〜

I only care about the gender and date values in the target and the corresponding entries in the key will always be 14 and 90 我只关心目标中的性别和日期值,并且密钥中的相应条目始终为14和90

Once I know where those values are sequentially (positions 2 & 7 in example 1), I need to pull out the same sections of my target string so I end up with the gender and the date values in their own variables 一旦知道了这些值的顺序(示例1中的位置2和7),就需要提取目标字符串的相同部分,以便最终在其自己的变量中使用性别和日期值

I have changed the type of data I'm looking for to make the question easier to understand 我已更改了要查找的数据类型,以使问题更易于理解

I hope that made sense 我希望这是有道理的

Thanks 谢谢

Matt 马特

You can split the two strings (see here: Split function equivalent in T-SQL? ). 您可以拆分两个字符串(请参见此处: 拆分函数是否等效于T-SQL? )。

Then, when you have splitted the strings in two table objects, access at the N-th record (eg using the TOP keyword) in both and you can get the result. 然后,将字符串拆分为两个表对象后,访问两个表中的第N条记录(例如,使用TOP关键字),就可以得到结果。

Given the two strings and an int you can write a function to get the two coupled values. 给定两个字符串和一个int,您可以编写一个函数来获取两个耦合值。

Try this solution - 试试这个解决方案-

DECLARE @temp TABLE
(
      id INT IDENTITY(1,1)
    , k NVARCHAR(100)
    , t NVARCHAR(500)
)

INSERT INTO @temp (k, t)
VALUES 
    (',15,90,104,73,95,13,14,', 'Medium~Male~28~Green~Right~No~10/04/2013'),
    (',14,73,104,95,15,13,90,', '12/03/2013~Green~28~Right~Medium~No~Male~')

SELECT 
      data.id
    , data.p
    , data.r 
FROM (
    SELECT 
          p = p.value('(.)[1]', 'NVARCHAR(50)')
        , po = p.value('for $i in . return count(../*[. << $i])', 'int')
        , r = r.value('(.)[1]', 'NVARCHAR(50)')
        , ro = r.value('for $i in . return count(../*[. << $i])', 'int')
        , d.id
    FROM (
        SELECT 
              t.id
            , txml = CAST('<r><s>' + REPLACE(t.k + ',', ',', '</s>' + '<s>') + '</s></r>' AS XML)
            , kxml = CAST('<r><s>' + REPLACE(t.t + '~', '~', '</s>' + '<s>') + '</s></r>' AS XML)  
        FROM @temp t
    ) d
    CROSS APPLY kxml.nodes('/r/s') t(p)
    CROSS APPLY txml.nodes('/r/s') k(r)
) data
WHERE data.po = data.ro - 1
    AND data.r IN ('14', '90')
    --AND r + data.p != ''
ORDER BY data.id

As a quick Follow up, I had trouble applying the above solution (probably due to a lack of knowledge on my part) so I created the following functions and they do the job 作为快速跟进,我在应用上述解决方案时遇到了麻烦(可能是由于我缺乏知识),因此我创建了以下函数,它们可以完成工作

GET SEQUENTIAL VALUE ---------------------------------------------------------------------- 获得序列值----------------------------------------------- -----------------------

DECLARE @Attributemask varchar(max) = '[PASS IN KEY STRING]' 
DECLARE @Required varchar(10) = '[PASS IN VALUE TO LOOK FOR FOLLOWED BY A COMMA]'
DECLARE @AttPosition int
DECLARE @TempString varchar(max)
DECLARE @Result as int

SET @AttPosition = CHARINDEX(@Required, @Attributemask)
SET @TempString = LEFT(@Attributemask, @AttPosition)
SET @Result = len(@TempString)-len(replace(@TempString,',',''))
SELECT @Result

GET DATA ---------------------------------------------------------------------------------- 获取数据------------------------------------------------ ----------------------------------

DECLARE @ChangeData varchar(max) = '[PASS IN TARGET STRING]'
DECLARE @AttPos int = [PASS IN RESULT FROM PREVIOUS FUNCTION] 
DECLARE @Count int = 1
DECLARE @NotNeeded varchar(100)
Declare @Result varchar(max)

SET @ChangeData = '~'+@ChangeData

WHILE @Count < (@AttPos +1)
BEGIN
    SET @NotNeeded = CHARINDEX('~', @ChangeData)
    SET @ChangeData = right(@ChangeData,len(@ChangeData) - @NotNeeded)
    IF len(@ChangeData) = 0 BREAK
    SET @Count = @Count +1
END

SET @Result = LEFT(@ChangeData, (CHARINDEX('~', @ChangeData)))
SET @Result = LEFT(@Result, (LEN(@Result)-1))
Select @Result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM