SQL将分号列表转换为关系表

发布于 2024-09-14 02:18:05 字数 273 浏览 3 评论 0原文

我目前有一个包含类型列表(Type_ID、Description)的代码表,但它们作为 ID;;ID;;ID...等保存在另一个表中

我正在寻找一个脚本来获取这些 ID 并放置它们在对应于 Type ID 的关系表中,

例如在表 A 中,Type_ID 条目可能如下所示:

1;;2;;4
1
3;;4
1;;2;;3;;4

我完全不知道如何实现这一目标,感谢任何帮助。

I currently have a code table containing a list of types (Type_ID, Description), but they are saved in another table as ID;;ID;;ID...etc

I am looking for a script that will take those ID's and place them in a relationship table corresponding to there Type ID

For example in table A the Type_ID entries could look like:

1;;2;;4
1
3;;4
1;;2;;3;;4

I am completely stumped on how to accomplish this and any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长发绾君心 2024-09-21 02:18:05

首先,我可能会建议采用 UDF 路线(这样您就不会重新发明轮子)。但是,考虑到这听起来像是一次性活动,您可以仅使用以下内容:

declare @output table (parentKey int, value int)

declare @values table (idx int identity(1, 1), parentKey int, value varchar(255))

-- Modify the below query to capture the data from your table
insert into @values (parentKey, value) values(1, '1;;2;;4'),(2, '1'),(3, '3;;4'),(4, '1;;2;;3;;4')

declare @i int
declare @cnt int

select @i = MIN(idx) - 1, @cnt = MAX(idx) from @values

while(@i < @cnt)
begin
    select @i = @i + 1

    declare @value varchar(255)
    declare @key int

    select @value = value, @key = parentKey from @values where idx = @i

    declare @idx int
    declare @next int

    select @idx = 1

    while(@idx <= LEN(@value))
    begin
        select @next = CHARINDEX(';;', @value, @idx)

        if(@next > @idx)
        begin
            insert into @output (parentKey, value) values(@key, SUBSTRING(@value, @idx, @next - @idx))

            select @idx = @next + 2
        end
        else
        begin
            insert into @output (parentKey, value) values(@key, SUBSTRING(@value, @idx, LEN(@value) - @idx + 1))

            select @idx = LEN(@value) + 1
        end
    end
end

select * from @output

@output 表变量现在包含您要查找的映射。您可以将其复制到最后的目标,也可以从查询中删除 @output 并将等效插入直接替换到关系表中。

First of all, I would probably recommend going the UDF route (so that you don't reinvent the wheel). However, given that this sounds like a one-off activity, you could just use the following:

declare @output table (parentKey int, value int)

declare @values table (idx int identity(1, 1), parentKey int, value varchar(255))

-- Modify the below query to capture the data from your table
insert into @values (parentKey, value) values(1, '1;;2;;4'),(2, '1'),(3, '3;;4'),(4, '1;;2;;3;;4')

declare @i int
declare @cnt int

select @i = MIN(idx) - 1, @cnt = MAX(idx) from @values

while(@i < @cnt)
begin
    select @i = @i + 1

    declare @value varchar(255)
    declare @key int

    select @value = value, @key = parentKey from @values where idx = @i

    declare @idx int
    declare @next int

    select @idx = 1

    while(@idx <= LEN(@value))
    begin
        select @next = CHARINDEX(';;', @value, @idx)

        if(@next > @idx)
        begin
            insert into @output (parentKey, value) values(@key, SUBSTRING(@value, @idx, @next - @idx))

            select @idx = @next + 2
        end
        else
        begin
            insert into @output (parentKey, value) values(@key, SUBSTRING(@value, @idx, LEN(@value) - @idx + 1))

            select @idx = LEN(@value) + 1
        end
    end
end

select * from @output

The @output table variable now contains the mapping you're looking for. You can either copy from that to your destination at the end, or you can remove @output from the query and substitute equivalent inserts directly into your relationship table.

安穩 2024-09-21 02:18:05

可能最简单的方法是使用 UDF (用户定义函数),例如此处概述的拆分函数

Probably the easiest way is to use a UDF (User Defined Function), such as the Split functions outlined here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文