如何将 SQL 服务器中的拆分字符串查询 [拆分为多行] 转换为更新 Table 查询?

How to convert a Split String Query [Splitting in to multiple rows] in to an Update Table Query in SQL Server?

非常新 SQL。 我有一个查询将我的列值拆分为多行。现在我需要将它提交给实际的 Table 而不仅仅是通过 Select 查询的视图。

我阅读了很多解决方案,其中 none 似乎有效。整个 "create Split Function" 写所有这些复杂的变量,然后创建一个 "Temp Table" 然后 "merge it back to the original table with the value column" 等等让我非常困惑。 谁能帮我把它放在计划和简单的格式中。 对于像我这样的开发新手,必须有一个更简单的方法。请不要只告诉我我做错了什么并假设我知道你在说什么(因为我知道它不起作用,我已经确定我不是这方面的专业人士),谁能告诉我我需要做什么才能完成这项工作。

我有输出视图,这正是我想要的,但它只是一个视图,它不会更新 table...我只想要我的 Split_String 的 VALUE 输出查询将其自身插入到我的实际 Table 中,或者即使我可以更新 Table 使其看起来像我的视图查询 table 甚至我如何从中创建新的 table我的查询,这样一个 ACTUAL table 看起来像我的查询,而不仅仅是一个视图,那就太棒了!

EXAMPLE TABLE of 1 row

请注意,这只是一个示例,实际 table 中有超过 120 列,拆分 [DOS 文件] 字符串不是我的问题(我已经用我的查询按分隔符拆分并插入多行并复制它周围的所有其他列),我的问题是尝试将 split_string 查询创建的值列提交给实际的 table 以便我可以对其进行其他数据转换,这需要按分隔符拆分字符串

Column1 Name = DOS File

Column1 Value = example1.doc | example2.doc | example3.doc | example4.doc | example5.doc | example6.doc | example7.doc

Column2 Name = NAME

Column2 Value = Sally Andrews

Column3 Name = ADDRESS

Column3 Value = 42 Wallaby Way, Syndey

Column4 Name = PHONE NUMBER

Column4 Value = 123-123

我的 Split_String 查询具有我的 Split_String 查询的 [输出列],附加到引用的 Table 中的所有其他列:

select value as [New DOS file], [Document Export Edit].*
from [Document Export Edit]
cross apply string_split([DOS file], '|')

[DOS file] 列是一个巨大的 .csv style string,因此分隔符将由 '|'.

我尝试使用的方法:

Update [Document Export Edit]
 set [New DOS File] = (select value
from [Document Export Edit]
cross apply string_split([DOS file], '|'))
GO

出现以下错误消息:

Invalid column name 'New DOS File'.

和这个:

Update [Document Export Edit]
      set [New DOS File] = replace([New DOS File],[New DOS File],
             select value from [Document Export Edit]
      cross apply string_split([DOS file], '|'))

出现以下错误消息:

Invalid column name 'New DOS File'.

和这个:

Alter Table [Document Export Edit]
Add [New DOS File] NVARCHAR(max) null
Add [Test] NVARCHAR(max) null
GO

Update [Document Export Edit]
set [New DOS File] = id1,
    [Test] = id2
from (
  select [DOS File],
          (select top 1 value as val
              from string_split([DOS File], '|')
              order by (row_number() over(order by 1 asc)) asc
              ) as id1,
          (select top 1 value as val
              from string_split([DOS File], '|')
              order by (row_number() over(order by 1 asc)) desc
              ) as id2
              from [Document Export Edit]
      ) A inner join [Document Export Edit] B
      on A.[DOS File] = B.[DOS File]

并出现以下错误消息:

Windowed functions, aggregates and NEXT VALUE FOR functions do not support integer indices as ORDER BY clause expressions.

所以我试了这个:

Update [Document Export Edit]
set [New DOS File] = id1
from (
  select [DOS File],
          (select top 1 value as val
              from string_split([DOS File], '|')
              ) as id1
              from [Document Export Edit]
      ) A inner join [Document Export Edit] B
      on A.[DOS File] = B.[DOS File]

结果:

它起作用了,但它只是拆分了分隔符的第一个实例,在另一列中复制了完全相同的内容,只是忽略了字符串的其余部分。很近!!!我只是想让它做它在第一位所做的事情,然后对字符串的其余部分做同样的事情,但是把它放在不同的行中,每个定界符实例!!

所以去试试这个:

Update [Document Export Edit]
set [New DOS File] = id1
from (
  select [DOS File],
          (select top 1 value as val
              from [Document Export Edit]
              cross apply string_split([DOS File], '|')
              ) as id1
              from [Document Export Edit]
      ) A inner join [Document Export Edit] B
      on A.[DOS File] = B.[DOS File]

结果:

然后发布整个[New DOS file]列只是复制了第一个结果。回去查看我的语法,然后发现我只在第一个实例上设置了变量限制……我的坏处。所以我去改了。

然后我试了这个:

Update [Document Export Edit]
set [New DOS File] = id1
from (
  select [DOS File],
          (select value as val
              from [Document Export Edit]
              cross apply string_split([DOS File], '|')
              ) as id1
              from [Document Export Edit]
      ) A inner join [Document Export Edit] B
      on A.[DOS File] = B.[DOS File]

出现以下错误消息:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

所以看起来这个特定的查询一次只能做 1 个变量,不能同时做多行。

以下是我尝试访问的一些链接,这些链接在我的任务中失败了,或者太复杂了,我无法访问:

Turning a Comma Separated string into individual rows

Splitting delimited values in a SQL column into multiple rows

Split values over multiple rows

Update multiple rows in SQL Server with IN clause

Split string into multiple rows with multiple columns in paired

https://www.emoreau.com/Entries/Articles/2019/04/Splitting-a-value-into-multiple-rows-in-Microsoft-SQL-Server.aspx

我希望这些信息足够有人帮助我。谢谢。

经过这么多小时的尝试和失去希望。我漂亮的技术达人朋友终于指出我的语法顺序错误。

我的问题的答案是:

select value as [New DOS file], dbo.[Document Export Edit].*
into [Document Export String_Split]
from [Document Export Edit]
cross apply string_split([DOS file], '|')