python - 如何像使用 Python 一样使用 SQL,通过复制行同时更改该行中的一个值(其中该值源自不同的列)?

标签 python sql string stored-procedures

我陷入了僵局(在 SQL 方面并不比新手好多少)。我想用 SQL 构建一个 POC,它可以完成我已经在 Python 中轻松完成的事情。这是肉-

 TEXT |  PID   | PATCHES
------------------------------------------------------
'abc' | 123456 | '839264, 129361, 927301'
'def' | 234567 | '736492\n 928537'
'ghi' | 284554 | 'Text here #490238Text here #110299'
'jkl' | 776493 | '290389'

我需要的是以下内容-

 TEXT |  PID
--------------
'abc' | 123456
'abc' | 839264
'abc' | 129361
'abc' | 927301
'def' | 234567
'def' | 736492
'def' | 928537
'ghi' | 284554
'ghi' | 490238
'ghi' | 110299
'jkl' | 776493
'jkl' | 290389

如您所见,我已为 PATCHES 列中弹出的每个值复制了 TEXT 中的值,同时保持 PID 值仍在新表中。

在 Python 中,您可以使用 split()find() 并将其附加到列表等,但这现在超出了我的理解范围。

感谢任何帮助。谢谢你!顺便说一句,请随时告诉我最好用 Python 完成,我很乐意将该信息转发给我的团队。

最佳答案

您可以执行以下操作:

  1. 创建临时表
  2. 将主表中的所有值放入临时表(With Identity(1,1))
  3. 使用循环,直到“临时”表的长度
  4. 将每个值/行传递到存储过程中,并执行您的操作。

    4.1 您可以在 SQL Server 中执行 String_Split,方法是将数据库可比性更改为 130。

    4.2 执行操作后可以返回结果。

这里是示例演示:

Create Table #Temp
(
    Entity Int Identity(1,1),
    Text VarChar(3),
    Strings VarChar(100)
)
Insert #Temp
Select Text, Pid+ '-'+Patches From dbo.Errors

--Select * From dbo.#Temp;
Declare @Count As Int = (Select Count(*) From dbo.Errors)
Declare @i as Int = 1
While @i <= @Count
Begin
    Declare @Text As VarChar(3), @Strings As VarChar(100) 

    --select value one by one, and pass it into a stored procedure
    Select @Text = Text, @Strings = Strings From dbo.#Temp Where Entity = @i;
    Exec dbo.ErrorsProc @Text, @Strings

    Set @i = @i +1
End

关于python - 如何像使用 Python 一样使用 SQL,通过复制行同时更改该行中的一个值(其中该值源自不同的列)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54643282/

相关文章:

将十进制转换为十六进制是截断第一位数字

java - JAVA中在文件的一行末尾添加特定字符串

python - 如何在传递一些函数后返回 'for loop' 中的相同数据帧,而不附加等?

python - 循环遍历包含内容和索引的列表

mysql - 为什么MySQL不使用索引?

php - MySQL Process List - 提高性能 - 锁定问题

python - 按列名对 CSV 进行排序

python - 谷歌应用服务器 "unable to add testProject to attribute application"正则表达式问题中的 yaml 异常?

sql - 如何在SQL Server 2008中获取时间字段的总和

java - 如何用内容中的引号拆分字符串