将字符串的所有实例提取到串联结果中

Extract All Instances of String into Concatenated Result

使用 SQL Server 2014,我想在一个字段内搜索 return 找到的字符串的所有实例,加上以下单词。例如,列中的文本可能是:

"exec sproc1 and then some more text here and then maybe execute sproc2 exec storedproc3 and maybe exec sproc1"

我想优雅地 return "sproc1, sproc2, storedproc3, sproc1",因为每个都是 exec 或 execute 之后的词(由空格分隔)。正如您在示例中看到的,前导词可能会有所不同,存储过程名称的长度也可能会有所不同。我已经能够 return 第一次使用 exec/execute;我的问题是有时会有多个(见下文)。

REPLACE(REPLACE(CASE
        WHEN [sJSTP].[subsystem]='TSQL' AND CHARINDEX('EXECUTE',[sJSTP].[command],1)>0
            THEN SUBSTRING([sJSTP].[command],CHARINDEX('EXECUTE',[sJSTP].[command],1)+8,
                IIF(
                CHARINDEX(' ',[sJSTP].[command],CHARINDEX('EXECUTE',[sJSTP].[command],1)+8)>0,
                CHARINDEX(' ',[sJSTP].[command],CHARINDEX('EXECUTE',[sJSTP].[command],1)+8)-CHARINDEX('EXECUTE',[sJSTP].[command],1)-8,
                LEN([sJSTP].[command])))
        WHEN [sJSTP].[subsystem]='TSQL' AND CHARINDEX('EXEC',[sJSTP].[command],1)>0 AND CHARINDEX('DCEXEC',[sJSTP].[command],1)<=0
            THEN SUBSTRING([sJSTP].[command],CHARINDEX('EXEC',[sJSTP].[command],1)+5,
                IIF(
                CHARINDEX(' ',[sJSTP].[command],CHARINDEX('EXEC',[sJSTP].[command],1)+5)>0,
                CHARINDEX(' ',[sJSTP].[command],CHARINDEX('EXEC',[sJSTP].[command],1)+5)-CHARINDEX('EXEC',[sJSTP].[command],1)-5,
                LEN([sJSTP].[command])))
    END,'[',''),']','') AS sprocname

它的最终用途是解析来自 msdb..sysjobsteps table 的作业命令,以查看正在使用的存储过程。

编辑: 添加示例数据

示例 1: 执行 quarterly_run 1,'BW' 执行 quarterly_run_2 1, 'QR ' 执行 quarterly_run 2,'VAS' 执行 quarterly_run 1,'WR' 执行 quarterly_run 3,'RW' 执行 quarterly_run_2 1, 'ASF' 执行 quarterly_run_3 1, 'ALL'

示例 2: 声明@rundate 日期时间、@rptqtr 日期时间、@qtr int

设置@rundate = getdate() 设置@rptqtr = '06/30/2016'

set @qtr = (select datediff(quarter,@rptqtr,@rundate))

exec quarterly_extract @qtr

示例 3: 执行 Daily_Sync_Process 执行 Daily_Process

因此,如果您想获得紧跟在 exec 之后的内容,那么我会在 space 上拆分,然后使用自连接。这是使用下面函数的代码,它是 Jeff Moden's splitter.

with cte as(
select
    job_id
    ,step_name
    ,step_id
    ,s.ItemNumber
    ,s.Item
from msdb..sysjobsteps
--split on the space
cross apply dbo.DelimitedSplit8K(command,' ') s)

select 
    c.job_id
    ,c.step_id
    ,c.step_name
    ,c.Item
    ,c2.Item
from cte c
--self join to get exec myproc in the same row
full join
    cte c2 on 
    c2.ItemNumber = c.ItemNumber + 1 
    and c.job_id = c2.job_id
    and c.step_id = c2.step_id
--we only care where the base table has exec or execute (not executed, etc)
where c.Item = 'exec' or c.Item = 'execute'
order by 
    c.job_id, c.step_id, c.ItemNumber

重要的是要意识到这会失败,例如,命令是 exec someproc,它有两个 space。您可以使用 replace() 来解决这个问题,但是您必须多次嵌套此替换以考虑尽可能多的 space。您将在拆分器函数

command 列中处理它
--here we replace two spaces with 1 for the entire command
cross apply dbo.DelimitedSplit8K(replace(command,'  ',' '),' ') s)

分割函数

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[DelimitedSplit8K] (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!

RETURNS TABLE WITH SCHEMABINDING AS
RETURN

/* "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000... enough to cover VARCHAR(8000)*/

  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = SUBSTRING(@pString, l.N1, l.L1)
   FROM cteLen l


GO

只是另一个内联选项,不限于 8K

例子

Declare @YourTable table (ID int,SomeCol varchar(max))
Insert into @YourTable values
 (1,'exec quarterly_run 1, ''BW'' exec quarterly_run_2 1, ''QR '' exec quarterly_run 2, ''VAS'' exec quarterly_run 1, ''WR'' exec quarterly_run 3, ''RW'' exec quarterly_run_2 1, ''ASF'' exec quarterly_run_3 1, ''ALL''')
,(2,'declare @rundate datetime, @rptqtr datetime, @qtr int

set @rundate = getdate() set @rptqtr = ''06/30/2016''

set @qtr = (select datediff(quarter,@rptqtr,@rundate))

exec quarterly_extract @qtr
')
,(3,'exec Daily_Sync_Process exec Daily_Process')

;with cte as (
Select A.ID
      ,C.*
 From  @YourTable A
 Cross Apply (values (replace(replace(SomeCol,char(13),' '),char(10),' '))) B(CleanString)
 Cross Apply (
                Select RetSeq,RetVal = case when Lag(RetVal,1) over (Order by RetSeq) in ('Exec','Execute') then RetVal else null end
                From (
                        Select RetSeq = Row_Number() over (Order By (Select null))
                              ,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
                        From  (Select x = Cast('<x>' + replace((Select replace(CleanString,' ','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A 
                        Cross Apply x.nodes('x') AS B(i)
                     ) C1
             ) C
)
Select A.ID
      ,NewString = Stuff((Select ', ' +RetVal From cte Where ID=A.ID Order By RetSeq For XML Path ('')),1,2,'') 
 From cte A
 Group By A.ID

Returns

ID  NewString
1   quarterly_run, quarterly_run_2, quarterly_run, quarterly_run, quarterly_run, quarterly_run_2, quarterly_run_3
2   quarterly_extract
3   Daily_Sync_Process, Daily_Process