“Update in loop SQL” 可以翻译为 “在循环中更新SQL”。

huangapple go评论63阅读模式
英文:

Update in loop SQL

问题

I will translate the provided text:

我应该创建一个循环,在其中更新标签并添加注册号到它

看起来像这样

LP 文本
1 '妈妈咪呀 #XX'
2 '乔是我最好的朋友 #XX'
3 嗨嘿哈路'
4 '一个 #XX 两个 #XX 三个 #XX'
5 '最好的手机'
6 '好的我喜欢它 #XX 接下来我们做什么 #XX'

它应该看起来像这样

LP 文本
1 '妈妈咪呀 #XX[01]'
2 '乔是我最好的朋友 #XX[02]'
3 嗨嘿哈路'
4 '一个 #XX[03] 两个 #XX[04] 三个 #XX[05]'
5 '最好的手机'
6 '好的我喜欢它 #XX[06] 接下来我们做什么 #XX[07]'
英文:

I am supposed to make a loop in which I will update the tag and add the registration number to it

it looks like

LP Text
1 'Mamma mia #XX'
2 'Joe is my best #XX friend'
3 Hi hey haloo'
4 'one #XX two #XX three #XX'
5 'best phone ever'
6 'Nice im love it #XX what we do next #XX'

it should look like this

LP Text
1 'Mamma mia #XX[01]'
2 'Joe is my best #XX[02] friend'
3 Hi hey haloo'
4 'one #XX[03] two #XX[04] three #XX[05]'
5 'best phone ever'
6 'Nice im love it #XX[06] what we do next #XX[07]'

答案1

得分: 3

A bit ugly, but loops should be the last resort.

示例

Declare @YourTable Table ([LP] int,[Text] varchar(50)) Insert Into @YourTable Values
(1,'Mamma mia #XX')
,(2,'Joe is my best #XX friend')
,(3,'Hi hey haloo')
,(4,'one #XX two #XX three #XX')
,(5,'best phone ever')
,(6,'Nice im love it #XX what we do next #XX')

;with cte as (
Select *
,Seq = sum(case when RetVal like '#%' then 1 else 0 end) over ( order by RN,RetSeq)
,Pos = charindex(' ',RetVal+' ')
From (
Select *
,RN = row_number() over( order by LP)
from @YourTable
) A
Cross Apply (
Select RetSeq = [Key]+1
,RetVal = trim(Value)
From OpenJSON( '["'+replace(string_escape(replace([Text],'#','|||#'),'json'),'|||','"',"')+'"]' )

         ) B

)
Select LP
,NewVal = string_agg(
case when RetVal not like '#%' then RetVal
else stuff(RetVal+' ',Pos,0,concat('[',format(Seq,'00'),'] '))
end
,' ') WITHIN GROUP (ORDER BY RN,Seq)
From cte
Group By LP
Order By LP

结果

“Update in loop SQL” 可以翻译为 “在循环中更新SQL”。

英文:

A bit ugly, but loops should be the last resort.

Example

Declare @YourTable Table ([LP] int,[Text] varchar(50))  Insert Into @YourTable Values 
 (1,'Mamma mia #XX')
,(2,'Joe is my best #XX friend')
,(3,'Hi hey haloo')
,(4,'one #XX two #XX three #XX')
,(5,'best phone ever')
,(6,'Nice im love it #XX what we do next #XX')

;with cte as (
Select * 
      ,Seq = sum(case when RetVal like '#%' then 1 else 0 end) over ( order by RN,RetSeq)
      ,Pos = charindex(' ',RetVal+' ')
 From  (
		Select *
			  ,RN = row_number() over( order by LP) 
		 from @YourTable
       ) A 
 Cross Apply (
            Select RetSeq = [Key]+1
                  ,RetVal = trim(Value)
             From  OpenJSON( '["'+replace(string_escape(replace([Text],'#','|||#'),'json'),'|||','","')+'"]' )

             ) B
)
Select LP
      ,NewVal = string_agg(
	            case when RetVal not like '#%' then RetVal
	                 else stuff(RetVal+' ',Pos,0,concat('[',format(Seq,'00'),'] '))
				end
				,' ') WITHIN GROUP (ORDER BY RN,Seq)
 From  cte
 Group By LP
 Order By LP

Results

“Update in loop SQL” 可以翻译为 “在循环中更新SQL”。

答案2

得分: 1

另一种方法可能是:

  • #字符上拆分您的字符串,
  • 计算“#XX”出现的次数,并进行逐步累加,
  • 用“#XX[n]”替换“#XX”,
  • 再次聚合您的字符串部分。
WITH cte AS (
    SELECT LP,
           REPLACE(
               IIF(LEFT(value, 3) = 'XX ', CONCAT('#', value), value),
               '#XX', 
               CONCAT(
                   '#XX[',
                   FORMAT(SUM(IIF(LEFT(value, 3) = 'XX ', 1, 0)) OVER(ORDER BY LP, ordinal), '00'),
                   ']'
               )
           ) AS [TextParts]
    FROM tab
    CROSS APPLY STRING_SPLIT(tab.[Text], '#', 1)
)
SELECT LP, 
       STRING_AGG([TextParts], '') AS [Text]
FROM cte
GROUP BY LP

输出

LP Text
1 Mamma mia #XX[01]
2 Joe is my best #XX[02] friend
3 Hi hey haloo
4 one #XX[03] two #XX[04] three #XX[05]
5 best phone ever
6 Nice im love it #XX[06] what we do next #XX[07]

查看演示这里

英文:

Another approach could:

  • split your strings on # characters,
  • compute the amount of times "#XX" is present, with a running sum
  • replace "#XX" with "#XX[n]"
  • aggregate your string parts again
WITH cte AS (
    SELECT LP,
           REPLACE(
               IIF(LEFT(value, 3) = 'XX ', CONCAT('#',value), value),
               '#XX', 
               CONCAT(
                   '#XX[',
                   FORMAT(SUM(IIF(LEFT(value, 3) = 'XX ', 1, 0)) OVER(ORDER BY LP, ordinal), '00'),
                   ']'
               )
           ) AS [TextParts]
    FROM tab
    CROSS APPLY STRING_SPLIT(tab.[Text], '#', 1)
)
SELECT LP, 
       STRING_AGG([TextParts], '') AS [Text]
FROM cte
GROUP BY LP

Output:

LP Text
1 Mamma mia #XX[01]
2 Joe is my best #XX[02] friend
3 Hi hey haloo
4 one #XX[03] two #XX[04] three #XX[05]
5 best phone ever
6 Nice im love it #XX[06] what we do next #XX[07]

Check the demo here.

答案3

得分: -1

声明 @TempTable 表 ([LP] int, [Text] varchar(50)) 将数据插入 @TempTable 表中
 (1, 'Mamma mia #XX')
 (2, 'Joe is my best #XX friend')
 (3, 'Hi hey haloo')
 (4, 'one #XX two #XX three #XX')
 (5, 'best phone ever')
 (6, 'Nice im love it #XX what we do next #XX')

声明 @TempTable2 表 ([LP] int, [Text] varchar(50))

声明
    @lp INT,
    @text VARCHAR(MAX);

从 @TempTable 中选择所有数据

声明 cursor_temptable 游标,用于从 @TempTable 中选择 LP 和 Text
打开 cursor_temptable

声明 @myPrevValue int=0
声明 @myNextValue int=0

从 cursor_temptable 中获取下一个值,存入 @lp 和 @text 中
当 @@FETCH_STATUS=0 时,执行以下操作

声明 @count int
设置 @count 为从 @text 中使用 '#' 分割后的值减1的数量
设置 @myNextValue 为 @myPrevValue 加上 @count

将 @lp 和以下查询结果插入 @TempTable2 中
(select STRING_AGG(a.value, '#') 
From (
    select replace(value, 'XX', 'XX' + '[0' + convert(varchar, (rank() over(order by value)-1+@myPrevValue)) + '] ') value 
    from STRING_SPLIT(@text, '#')
) as a)

从 cursor_temptable 中获取下一个值,存入 @lp 和 @text 中
设置 @myPrevValue 为 @myNextValue

关闭 cursor_temptable
释放 cursor_temptable

从 @TempTable2 中选择所有数据
英文:
Declare @TempTable Table ([LP] int,[Text] varchar(50))  Insert Into @TempTable Values 
 (1,'Mamma mia #XX')
,(2,'Joe is my best #XX friend')
,(3,'Hi hey haloo')
,(4,'one #XX two #XX three #XX')
,(5,'best phone ever')
,(6,'Nice im love it #XX what we do next #XX')

Declare @TempTable2 Table ([LP] int,[Text] varchar(50))

DECLARE 
    @lp INT, 
    @text   VARCHAR(MAX);


select * from @TempTable

Declare cursor_temptable cursor for select LP,Text from @TempTable
open cursor_temptable 

Declare @myPrevValue int=0
Declare @myNextValue int=0


FETCH NEXT FROM cursor_temptable INTO @lp,@text
while @@FETCH_STATUS=0
Begin

declare @count int
set @count=(select count(*)-1 from STRING_SPLIT(@text,'#'))
set @myNextValue=@myPrevValue+@count

insert into @TempTable2 values(@lp,(select 
STRING_AGG(a.value,'#')
From(
select replace(value,'XX','XX'+'[0' +convert(varchar,(rank() over(order by value)-1+@myPrevValue)) +'] ' ) value from STRING_SPLIT(@text,'#')
) as a))


FETCH NEXT FROM cursor_temptable INTO @lp,@text
set @myPrevValue=@myNextValue
end

close cursor_temptable
deallocate cursor_temptable


select * from @TempTable2

huangapple
  • 本文由 发表于 2023年5月21日 04:02:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定