【问题标题】:How to insert/update a separate row for each column with SQLServer如何使用 SQLServer 为每一列插入/更新单独的行
【发布时间】:2014-12-09 21:15:19
【问题描述】:

我正在尝试通过存储过程将数据从一个表导入到另一个表。我需要能够同时进行插入和更新。我已经弄清楚了插入物。现在,我正在尝试寻找最优雅的更新方式。

这是一个类似于我试图从中提取数据的表。请原谅表创建脚本中的任何错误。它们只是示例。

CREATE TABLE [dbo].[source_table](
    [linkage_id] [int] IDENTITY(1,1) NOT NULL,
    [code] [char](6) NULL,
    [code_1] [char](6) NULL,
    [code_2] [char](6) NULL,
    [code_3] [char](6) NULL,
    [code_4] [char](6) NULL,
    [code_5] [char](6) NULL,
    [code_6] [char](6) NULL,
    [code_7] [char](6) NULL,
    [code_8] [char](6) NULL,
    [code_9] [char](6) NULL,
    [code_10] [char](6) NULL,
) ON [PRIMARY]

我需要像这样插入表格:

CREATE TABLE [dbo].[destination_table](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [linkage_id] [int] NULL,
    [code] [char](30) NULL
 CONSTRAINT [PK_destination_table] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

当从源表中提取记录时,需要在目标表中插入一行代码,每个代码在源表中输入一个值。您最终可能会在目标表中得到如下值:

id | linkage_id | code
______________________
1  | 501        | abc
2  | 501        | abb
3  | 501        | aba
4  | 502        | aaa
5  | 503        | aaa
6  | 503        | aab
7  | 503        | abc
8  | 503        | bba
9  | 503        | cc

然后,我可能需要使用源表中的新值更新目标表:

linkage_id | code | code_1 | code_2 | code_3 | code_4 | ...
______________________
501        | ccc  | abb    | bbb    |        |        | ...

我需要以正确的顺序更新记录。我的目标是完成后目标表看起来像这样:

id | linkage_id | code
______________________
1  | 501        | ccc
2  | 501        | abb
3  | 501        | bbb
4  | 502        | aaa
5  | 503        | aaa
6  | 503        | aab
7  | 503        | abc
8  | 503        | bba
9  | 503        | cc

我的猜测是会涉及到光标...有人有什么好主意吗?

我刚刚想到的一个选项是,我可以删除所有以前的记录并重新插入它们,而不是尝试更新destination_table 中的记录。

【问题讨论】:

  • 你能在源表中显示一个实际数据的例子吗?我不确定更新的来源。
  • 我无法向您展示实际数据,但上面(原始帖子中的第二个表)是源表中数据的示例。在上面的示例中,我们将尝试更新 destination_table 中的每条记录,其 links_id 为 501。

标签: sql-server-2008 sql-update


【解决方案1】:

您可以使用UNPIVOTMERGE

-- Optional table variable to assist with processing source data into destination table. 
-- Holds the normalized form of the data in source_table
declare @p table(linkage_id int, code nchar(6))

-- UNPIVOT the data from columns into rows to be able to use MERGE
insert into @p (linkage_id, code)
select linkage_id, value
from (select linkage_id, code, code_1, code_2 from source_table) s
unpivot (
    value for [key] in (code, code_1, code_2)
) as PivotTable

-- Check out the data by eye
-- select * from @p

-- Now we can update the destination table from the normalized source values
merge destination_table as d
    using (select linkage_id, code from @p) as source (linkage_id, code)
    on d.linkage_id = source.linkage_id
when matched
    then update set d.code = source.code
when not matched
    then insert (linkage_id, code) values (source.linkage_id, source.code);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    相关资源
    最近更新 更多