试试这个代码。
用户定义的函数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) |
| ) |