如何在sql语句中循环存储过程

时间:2016-07-19 11:38:42

标签: html sql sql-server reporting-services

我知道这听起来像一个老问题,但即使在阅读了无数的答案后我也无法解决我的问题,所以我会尝试具体。 我有一个包含一列文本的表,有时包含HTML标记。我需要删除大部分HTML,但留下一些。我编写了以下代码来执行此操作:

      DECLARE @Start INT = 1;
  DECLARE @End INT = 1;
  DECLARE @Length INT = 0;
  DECLARE @Keep INT = 0;
  DECLARE @ReplaceChar VARCHAR(10) = '';
  DECLARE @Offset INT = 0;
  RETURN;

  WHILE @Start > 0 AND @End > 0
    BEGIN
      SET @ReplaceChar = (SELECT CASE WHEN SUBSTRING(@HTMLText,@Start + 1, 7) IN ('/strike')
                                      THEN ('</strike>')
                                      WHEN SUBSTRING(@HTMLText,@Start + 1, 6) IN ('strike')
                                      THEN ('<strike>')
                                      WHEN SUBSTRING(@HTMLText,@Start + 1, 4) IN ('br /')
                                      THEN ('<br />')
                                      WHEN SUBSTRING(@HTMLText,@Start + 1, 3) IN ('/th', '/tr','/td','th ')
                                      THEN ('<' + SUBSTRING(@HTMLText,@Start + 1, 3) + '>')
                                      WHEN SUBSTRING(@HTMLText,@Start + 1, 2) IN ('th', 'tr', 'td')
                                      THEN ('<' + SUBSTRING(@HTMLText,@Start + 1, 2) + '>')
                                      WHEN SUBSTRING(@HTMLText,@Start + 1, 2) IN ('/p', '/i', '/b')
                                      THEN ('<' + SUBSTRING(@HTMLText,@Start + 1, 2) + '>')
                                      WHEN SUBSTRING(@HTMLText,@Start + 1, 1) IN ('p', 'i', 'b')
                                      THEN ('<' + SUBSTRING(@HTMLText,@Start + 1, 1) + '>')
                                      ELSE ''
                                 END);
      SET @Keep = (SELECT CASE WHEN SUBSTRING(@HTMLText,@Start + 1, 7) IN ('/strike') THEN 1
                               WHEN SUBSTRING(@HTMLText,@Start + 1, 6) IN ('strike') THEN 1
                               WHEN SUBSTRING(@HTMLText,@Start + 1, 4) IN ('br /') THEN 1
                               WHEN SUBSTRING(@HTMLText,@Start + 1, 3) IN ('/th', '/tr', '/td') THEN 1
                               WHEN SUBSTRING(@HTMLText,@Start + 1, 2) IN ('th', 'tr', 'td') THEN 1
                               WHEN SUBSTRING(@HTMLText,@Start + 1, 2) IN ('/p', '/i', '/b') THEN 1
                               WHEN SUBSTRING(@HTMLText,@Start + 1, 1) IN ('p', 'i', 'b') THEN 1
                               ELSE 0
                          END);

      SET @HTMLText = RTRIM((LTRIM(STUFF(@HTMLText,@Start, @Length,@ReplaceChar))));
      SET @Start = CHARINDEX('<', @HTMLText,@Start + @Offset);
      SET @End = CHARINDEX('>', @HTMLText,CHARINDEX('<', @HTMLText,@Start + @Offset));
      SET @Length = (@End - @Start) + 1; 
      SET @Offset = @Start + @Keep;

    END;

  RETURN @HTMLText;

在替换函数完成后,需要将此代码应用于以下代码中的Details列。

DECLARE @Region  NVARCHAR(12) = '11'
DECLARE @Location NVARCHAR (1000) = '1932'
DECLARE @IPM NVARCHAR (500) = '1594,1611,1934' -- for 1932 --'8055,15591'--for 1941
DECLARE @NoteFromDate DATETIME = '20150101'
DECLARE @NoteToDate DATETIME= '20160701'
  SELECT r.RegionName
    , l.LocationName
    , CASE WHEN o.CurrentLocationId <> l.LocationId
           THEN '*'
           ELSE ''
      END AS NotCurrentLocation
    , e.DisplayName AS IPM
    , ec.DisplayName AS Counselor
    , oni.OffenderId
    , an.LastName + ', ' + an.FirstName + COALESCE(' ' + an.MiddleName,
                                        '') AS OffenderName
    , oni.NoteDate
    ,REPLACE(REPLACE(REPLACE(REPLACE(
        ono.Details,'''',''),'&nbsp;',' '),'&amp;',' & '),'rsquo;','-')
    FROM ref.Employee AS e
    INNER JOIN ref.Location AS l
      ON e.LocationId = l.LocationId
    INNER JOIN ref.Region AS r
      ON l.RegionId = r.RegionId
    INNER JOIN ind.OffenderNoteInfo AS oni
      ON e.EmployeeId = oni.StaffId
    INNER JOIN ind.Offender AS o
      ON oni.OffenderId = o.OffenderId
    INNER JOIN ind.OffenderNote AS ono
      ON oni.OffenderNoteInfoId = ono.OffenderNoteInfoId
    INNER JOIN ind.OffenderNoteInfo_ContactMode AS onicm
      ON oni.OffenderNoteInfoId = onicm.OffenderNoteInfoId
    INNER JOIN ind.AliasName AS an
      ON oni.OffenderId = an.OffenderId AND an.AliasNameTypeId = 0 --Default Name
    LEFT JOIN ind.OffenderCurrentFactPart AS ocfp
      ON o.OffenderId = ocfp.OffenderId
    LEFT JOIN hsn.CounselorAssignment AS ca
      ON ocfp.PriCounselorAssignmentId = ca.CounselorAssignmentId
    LEFT JOIN ref.Employee AS ec
      ON ca.EmployeeId = ec.EmployeeId
    WHERE e.LocationId IN (
      SELECT Value
        FROM vnfa.udf_FnSplit(@Location, ',')) AND e.EmployeeTypeId = 106 --Treatment Program Supervisor
      AND oni.NoteTypeId = 11 --Facility Notes
      AND onicm.ContactModeId = 229 --Institution Fidelity Review
      AND (oni.NoteDate >= @NoteFromDate AND oni.NoteDate <= @NoteToDate) AND e.EmployeeId IN (
      SELECT Value
        FROM vnfa.udf_FnSplit(@IPM, ',')); 

如何在代码第二部分中运行代码部分1?

“详细信息”列的输入可能如下所示:

<p style="margin: 0in 0in 0pt;"><span style="font-family: Calibri;">All areas were reviewed </span></p>

输出应为:

<p>All areas were reviewed</p>

我只是没有找到一种方法将这两个组合在一起。感谢您的任何建议。

1 个答案:

答案 0 :(得分:0)

我会像其他人一样建议并用另一种语言编写此代码。但是你可以用tsql来做。第一个代码可以是标量函数,然后可以将其应用于查询中的详细信息列。