Oracle 11g - 使用回车符号行查找CLOB中的记录

时间:2014-07-17 13:07:13

标签: sql oracle oracle11g

我使用的是Oracle 11g。我正在尝试在CLOB字段上执行查找和替换功能(使用REPLACE)。

现在我的CLOB中的数据中包含CRLF,替换工作正常,直到我想找到一个包含CRLF的字符串。也许这最好用例子来解释:

So say the text in my field is:
----------------------------------
Hi there this is some text
that has CRLFS in it.
Some other text that
is there also.
Have a nice day

现在我要做的是替换所有这种情况,包括CRLF:

Search Text
--------------------------------------------------------------------------------
Some other text that
is there also.

使用此文本包含CRLF:

Replace Text
------------------------------------
Some other text that
has some new text that is there also.

所以可能会出现:

 ----------------------------------
 Hi there this is some text
 that has CRLFS in it.
 Some other text that
 has some new text that is there also.
 Have a nice day

现在我在存储过程中执行此操作,搜索文本和替换文本作为变量进入,但当我尝试说出%||之类的地方时ReplaceText || '%'返回0行。

有谁知道如何做到这一点?

这是我的存储过程(iOldResponsibilities是搜索文本,iNewResponsibilities是替换文本:

PROCEDURE FindReplaceResponsibilities (
    iOldResponsibilities    IN  JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
    iNewResponsibilities    IN  JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
    oNumRowsUpdated         OUT INTEGER
)
IS

  BEGIN
        oNumRowsUpdated := 0;


        SAVEPOINT sp_jf_findrepresp;

        -- If there is no old text to search for then, 
        -- append the new text to the end of every row.
        -- Else replace all occurrences of the old text with the new text
        IF iOldResponsibilities IS NULL THEN
          UPDATE JP_JOB_FAMILIES
            SET RESPONSIBILITIES = RESPONSIBILITIES || iNewResponsibilities;

          oNumRowsUpdated := SQL%ROWCOUNT;

        ELSE
          UPDATE JP_JOB_FAMILIES
            SET RESPONSIBILITIES = REPLACE(RESPONSIBILITIES, iOldResponsibilities, iNewResponsibilities)
          WHERE RESPONSIBILITIES like '%' || iOldResponsibilities || '%';

          -- I have also tried this:
          --WHERE dbms_lob.instr(RESPONSIBILITIES, TO_CLOB(iOldResponsibilities)) > 0; -- This is a LIKE for CLOBS

          oNumRowsUpdated := SQL%ROWCOUNT;

        END IF;

        RETURN;

        EXCEPTION

            WHEN OTHERS THEN
                BEGIN
                    oNumRowsUpdated := -1;
                    ROLLBACK TO sp_jf_findrepresp;
                    dbms_output.put_line('error: ' || sqlerrm);
                    RETURN;
                END;

END FindReplaceResponsibilities;

Text来自asp.net应用程序(c#)作为String值:

 public int FindReplaceJobFamilyResponsibilities(String oldResponsibilities, String newResponsibilities, IDbTransaction transaction = null)
    {
        using (IDbCommand cmd = this._dataHelper.GetStoredProcedure(_connectionString,
            "JP_JOBFAM_PKG.FindReplaceResponsibilities", true))
        {
            _dataHelper.SetParameterValue(cmd, "iOldResponsibilities", oldResponsibilities);
            _dataHelper.SetParameterValue(cmd, "iNewResponsibilities", newResponsibilities);
            DataHelperBase.VerifyParameters(cmd.Parameters, false);
            base.SetExecuteConnection(cmd, transaction);
            _dataHelper.ExecuteNonQuery(cmd);

            return Convert.ToInt32(_dataHelper.GetParameterValue(cmd, "oNumRowsUpdated"));
        }
    }

1 个答案:

答案 0 :(得分:0)

原来是坏数据的情况。我的测试数据库中的数据已损坏,只有LF而不是CRLF。

GIGO: - )

感谢您的帮助

哦,顺便说一句在我的代码示例中,我使用了INSTR函数而不是类似的函数。如果用户在文本中输入%来搜索,那可能搞砸了like语句。 (无法过滤掉那些因为%可能是我数据中的有效字符)

以下是最终守则:

PROCEDURE FindReplaceResponsibilities (
iOldResponsibilities    IN  JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
iNewResponsibilities    IN  JP_JOB_FAMILIES.RESPONSIBILITIES%TYPE,
oNumRowsUpdated         OUT INTEGER

) IS

BEGIN         oNumRowsUpdated:= 0;

    SAVEPOINT sp_jf_findrepresp;

    -- If there is no old text to search for then, 
    -- append the new text to the end of every row.
    -- Else replace all occurrences of the old text with the new text
    IF iOldResponsibilities IS NULL THEN
      UPDATE JP_JOB_FAMILIES
        SET RESPONSIBILITIES = RESPONSIBILITIES || iNewResponsibilities;

      oNumRowsUpdated := SQL%ROWCOUNT;

    ELSE
      UPDATE JP_JOB_FAMILIES
        SET RESPONSIBILITIES = REPLACE(RESPONSIBILITIES, iOldResponsibilities, iNewResponsibilities)
      WHERE dbms_lob.instr(RESPONSIBILITIES, iOldResponsibilities) > 0; 

      oNumRowsUpdated := SQL%ROWCOUNT;

    END IF;

    RETURN;

    EXCEPTION

        WHEN OTHERS THEN
            BEGIN
                oNumRowsUpdated := -1;
                ROLLBACK TO sp_jf_findrepresp;
                dbms_output.put_line('error: ' || sqlerrm);
                RETURN;
            END;

END FindReplaceResponsibilities;

我的申请表中的代码很好:

public int FindReplaceJobFamilyResponsibilities(String oldResponsibilities, String newResponsibilities, IDbTransaction transaction = null)
{
    using (IDbCommand cmd = this._dataHelper.GetStoredProcedure(_connectionString,
        "JP_JOBFAM_PKG.FindReplaceResponsibilities", true))
    {
        _dataHelper.SetParameterValue(cmd, "iOldResponsibilities", oldResponsibilities);
        _dataHelper.SetParameterValue(cmd, "iNewResponsibilities", newResponsibilities);
        DataHelperBase.VerifyParameters(cmd.Parameters, false);
        base.SetExecuteConnection(cmd, transaction);
        _dataHelper.ExecuteNonQuery(cmd);

        return Convert.ToInt32(_dataHelper.GetParameterValue(cmd, "oNumRowsUpdated"));
    }
}