-1

我在我的环境中使用 SQL Server 2005。

我有一列,其中每一行都包含有关某些 SQL 查询的大量数据。每一行看起来就像数据库中的一个长行,但是当您从该行中提取数据时,它会被拆分为多行,因为它包含回车符。

下面是由回车分割的列中的一行数据的示例。我试图找出一种方法,到目前为止没有成功,在这种方法中,我可以提取每行包含“伪=”的等号之后的所有内容,但前导和尾随空格除外。

所以在包含“pseudo=”的行上,我想要 = 符号之后的所有内容。然后我想修剪前导和尾随空格,如果有的话。我还想在每个“伪=”行的末尾保留回车。然后,我想将所有“pseudo=”行的这个新字符串存储到新列的新行中。

我的 SQL 不是很强大,这个小谜题让我陷入了困境。任何帮助是极大的赞赏。请注意,该列最多有 500 到 600 行,但如果这对任何事情都很重要,那么其中会有一堆文本。

0x8A40D34D4BEC294D8934029D569B2BFE
table=ca_discovered_hardware
target_type=1
label=label
typefield=
primarykeyfield=dis_hw_uuid
secondarykeyfield=domain_uuid
excludeclause=
[Arguments]
Arguments=12
[Argument 1]
type=1
pseudo= ( 
sql=(
valid=1
[Argument 2]
type=11
pseudo=(Link Query '[ES] IPQ: Tokyo (JPN)' using 'default' method)
sql=dis_hw_uuid in ($TARGETLINKSQL$)
valid=1
queryuuid=f8e488a2b34ee24b85db3f8fafd4e9a2
query_name=[ES] IPQ: S&B - Tokyo (JPN)
[Argument 3]
type=1
pseudo= ) 
sql=)
valid=1
[Argument 4]
type=1
pseudo= AND 
sql= AND 
valid=1
[Argument 5]
type=1
pseudo= NOT 
sql= NOT 
valid=1
[Argument 6]
type=1
pseudo= ( 
sql=(
valid=1
[Argument 7]
type=9
pseudo=(Current Server = 'APTOPRK01CIGE')
sql=ca_discovered_hardware.dis_hw_uuid in (SELECT object_uuid FROM ca_agent WHERE server_uuid IN (SELECT server_uuid FROM ca_server WHERE label='APTOPRK01CIGE'))
valid=1
[Argument 8]
type=1
pseudo= OR 
sql= OR 
valid=1
[Argument 9]
type=11
pseudo=(Link Query '[DM] EXQ: Obsolete Exclude' using 'default' method)
sql=dis_hw_uuid in ($TARGETLINKSQL$)
valid=1
queryuuid=00000000000000000000000000000000
query_name=[DM] EXQ: Obsolete Exclude
[Argument 10]
type=1
pseudo= OR 
sql= OR 
valid=1
[Argument 11]
type=11
pseudo=(Link Query '[DM] EXQ: Server Exclude' using 'default' method)
sql=dis_hw_uuid in ($TARGETLINKSQL$)
valid=1
queryuuid=00000000000000000000000000000000
query_name=[DM] EXQ: Server Exclude
[Argument 12]
type=1
pseudo= ) 
sql=)
valid=1
4

1 回答 1

1

试试这个代码。

用户定义的函数iter_charlist_to_tbl将您的字符串拆分为具有定义分隔符的行。(取自这里

my_convert函数将字符串的行连接到字符串

SQL小提琴

MS SQL Server 2008 架构设置

create table t ( str varchar(max))
go

create function my_convert(@str varchar(max))
returns varchar(max)
as
begin
  declare @p_str varchar(max), @result varchar(max), @delim varchar(2)
  set @delim = char(10)
  set @result = ''

  declare CUR cursor for 
    select right(str, len(str) -7) as result--, str 
    from dbo.iter_charlist_to_tbl(@str, @delim) t
    where str like 'pseudo=%'
  open CUR
  fetch next from CUR into @p_str
  while @@fetch_status = 0
  begin
    set @result = @result + @p_str + char(10)
    fetch next from CUR into @p_str
  end
  close CUR
  deallocate CUR
  return @result
end
go


CREATE FUNCTION iter_charlist_to_tbl
                 (@list      nvarchar(MAX),
                  @delimiter nvarchar(2) = N',')
      RETURNS @tbl TABLE (listpos int IDENTITY(1, 1) NOT NULL,
                          str     varchar(4000)      NOT NULL,
                          nstr    nvarchar(2000)     NOT NULL) AS

BEGIN
   DECLARE @endpos   int,
           @startpos int,
           @textpos  int,
           @chunklen smallint,
           @tmpstr   nvarchar(4000),
           @leftover nvarchar(4000),
           @tmpval   nvarchar(4000)

   SET @textpos = 1
   SET @leftover = ''
   WHILE @textpos <= datalength(@list) / 2
   BEGIN
      SET @chunklen = 4000 - datalength(@leftover) / 2
      SET @tmpstr = @leftover + substring(@list, @textpos, @chunklen)
      SET @textpos = @textpos + @chunklen

      SET @startpos = 0
      SET @endpos = charindex(@delimiter COLLATE Slovenian_BIN2, @tmpstr)

      WHILE @endpos > 0
      BEGIN
         SET @tmpval = ltrim(rtrim(substring(@tmpstr, @startpos + 1,
                                             @endpos - @startpos - 1)))
         INSERT @tbl (str, nstr) VALUES(@tmpval, @tmpval)
         SET @startpos = @endpos
         SET @endpos = charindex(@delimiter COLLATE Slovenian_BIN2,
                                 @tmpstr, @startpos + 1)
      END

      SET @leftover = right(@tmpstr, datalength(@tmpstr) / 2 - @startpos)
   END

   INSERT @tbl(str, nstr) VALUES (ltrim(rtrim(@leftover)), ltrim(rtrim(@leftover)))
   RETURN
END
go

insert t (str) values (
  '0x8A40D34D4BEC294D8934029D569B2BFE
table=ca_discovered_hardware
target_type=1
label=label
typefield=
primarykeyfield=dis_hw_uuid
secondarykeyfield=domain_uuid
excludeclause=
[Arguments]
Arguments=12
[Argument 1]
type=1
pseudo= ( 
sql=(
valid=1
[Argument 2]
type=11
pseudo=(Link Query ''[ES] IPQ: Tokyo (JPN)'' using ''default'' method)
sql=dis_hw_uuid in ($TARGETLINKSQL$)
valid=1
queryuuid=f8e488a2b34ee24b85db3f8fafd4e9a2
query_name=[ES] IPQ: S&B - Tokyo (JPN)
[Argument 3]
type=1
pseudo= ) 
sql=)
valid=1
[Argument 4]
type=1
pseudo= AND 
sql= AND 
valid=1
[Argument 5]
type=1
pseudo= NOT 
sql= NOT 
valid=1
[Argument 6]
type=1
pseudo= ( 
sql=(
valid=1
[Argument 7]
type=9
pseudo=(Current Server = ''APTOPRK01CIGE'')
sql=ca_discovered_hardware.dis_hw_uuid in (SELECT object_uuid FROM ca_agent WHERE server_uuid IN (SELECT server_uuid FROM ca_server WHERE label=''APTOPRK01CIGE''))
valid=1
[Argument 8]
type=1
pseudo= OR 
sql= OR 
valid=1
[Argument 9]
type=11
pseudo=(Link Query ''[DM] EXQ: Obsolete Exclude'' using ''default'' method)
sql=dis_hw_uuid in ($TARGETLINKSQL$)
valid=1
queryuuid=00000000000000000000000000000000
query_name=[DM] EXQ: Obsolete Exclude
[Argument 10]
type=1
pseudo= OR 
sql= OR 
valid=1
[Argument 11]
type=11
pseudo=(Link Query ''[DM] EXQ: Server Exclude'' using ''default'' method)
sql=dis_hw_uuid in ($TARGETLINKSQL$)
valid=1
queryuuid=00000000000000000000000000000000
query_name=[DM] EXQ: Server Exclude
[Argument 12]
type=1
pseudo= ) 
sql=)
valid=1
  ')

查询 1

-- table result
declare @long varchar(max)
declare @crlf varchar(2) = char(10)
select @long = str from t
select right(str, len(str) -7) as result--, str 
from dbo.iter_charlist_to_tbl(@long, @crlf) t
where str like 'pseudo=%'

/*--string result
select dbo.my_convert(str) from t
*/

结果

|                                                           RESULT |
--------------------------------------------------------------------
|                                                                ( |
|      (Link Query '[ES] IPQ: Tokyo (JPN)' using 'default' method) |
|                                                                ) |
|                                                              AND |
|                                                              NOT |
|                                                                ( |
|                               (Current Server = 'APTOPRK01CIGE') |
|                                                               OR |
| (Link Query '[DM] EXQ: Obsolete Exclude' using 'default' method) |
|                                                               OR |
|   (Link Query '[DM] EXQ: Server Exclude' using 'default' method) |
|                                                                ) |
于 2013-04-25T02:09:51.953 回答