SQL Server初学者:如何从另一个表更新表数据?

发布于 2024-11-01 05:30:35 字数 1189 浏览 5 评论 0原文

我还在学习SQL。我对更新表格有疑问。如何根据其他表更新我的表?请参阅下面的示例:

create table batch_info (
    batch_key int identity(1, 1) not null primary key,
    batch_num int not null,
    batch_name varchar(50) null,
    batch_start datetime null,
    batch_end datetime null,
    table_name varchar(50) null,
    insert_rows int null
)
go

insert into batch_info (batch_num, batch_name, batch_start)
    values ('32', 'Batch_to_insert_rows', '20110414')
go

select * from batch_info

create table ref_table (
    bat_num int not null,
    bat_end_date datetime null,
    bat_table_name varchar(50) null,
    bat_ins_rows int null,
)
go

insert into ref_table 
    values  ('32','20110414 02:12:00.000','Table1','10'),
            ('32','20110414 02:12:00.000','Table2','33'),
            ('32','20110414 02:12:00.000','Table3','12') 

select * from ref_table

--How can I update batch_info table to get this info?
select 
bi.batch_key, 
bi.batch_num, 
bi.batch_name,
bi.batch_start,
rt.bat_end_date,
rt.bat_table_name, 
rt.bat_ins_rows
 from batch_info as bi
inner join ref_table as rt on bi.batch_num = rt.bat_num

编辑:批处理键是代理项,因此它应该是增量的,而不是像我的联接查询中那样的 (1, 1, 1)。

I'm still learning SQL. I have question about updating table. How can I update my table based on other table? Please see the example below:

create table batch_info (
    batch_key int identity(1, 1) not null primary key,
    batch_num int not null,
    batch_name varchar(50) null,
    batch_start datetime null,
    batch_end datetime null,
    table_name varchar(50) null,
    insert_rows int null
)
go

insert into batch_info (batch_num, batch_name, batch_start)
    values ('32', 'Batch_to_insert_rows', '20110414')
go

select * from batch_info

create table ref_table (
    bat_num int not null,
    bat_end_date datetime null,
    bat_table_name varchar(50) null,
    bat_ins_rows int null,
)
go

insert into ref_table 
    values  ('32','20110414 02:12:00.000','Table1','10'),
            ('32','20110414 02:12:00.000','Table2','33'),
            ('32','20110414 02:12:00.000','Table3','12') 

select * from ref_table

--How can I update batch_info table to get this info?
select 
bi.batch_key, 
bi.batch_num, 
bi.batch_name,
bi.batch_start,
rt.bat_end_date,
rt.bat_table_name, 
rt.bat_ins_rows
 from batch_info as bi
inner join ref_table as rt on bi.batch_num = rt.bat_num

Edit: batch key is surrogate so it should be incremental, not (1, 1, 1) as in my join query.

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

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

发布评论

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

评论(4

緦唸λ蓇 2024-11-08 05:30:35

假设batch_num和bat_end_date的组合是唯一的,那么这应该可以解决问题:

update  batch_info 
set     batch_end = rt.bat_end_date,
        table_name = rt.bat_table_name,
        insert_rows = rt.bat_ins_rows 
from    batch_info bi
        inner join ref_table rt on rt.bat_num = bi.batch_num 
        inner join 
        (
            select  bat_num,
                    MIN(bat_end_date) as min_bat_end_date
            from    ref_table 
        ) oldest on oldest.bat_num = rt.bat_num 
                    and oldest.min_bat_end_date = rt.bat_end_date 
where   bi.batch_end is null

insert into batch_info 
(batch_num, batch_name, batch_start, batch_end, table_name, insert_rows)
select  bi.batch_num,
        bi.batch_name,
        bi.batch_start,
        rt.bat_end_date,
        rt.bat_table_name,
        rt.bat_ins_rows 
from    batch_info bi
        inner join ref_table rt on rt.bat_num = bi.batch_num 
where   not exists 
        (
            select   * 
            from     batch_info e 
            where    e.batch_num = bi.batch_num 
                     and e.batch_end = rt.bat_end_date
        )

如果另一个值的组合建立了唯一性,那么可以将它们添加到第一个查询中的oldest,并且e< /code> 在第二个。

我同意@Jim 的观点,即这应该分为两个表。执行此操作的难度预示着此设计将造成的困难。您试图将两个不同的事物(批次和批次运行)放入一个表中,这绝不是一个好主意。

Assuming that combinations of batch_num and bat_end_date are unique, then this should do the trick:

update  batch_info 
set     batch_end = rt.bat_end_date,
        table_name = rt.bat_table_name,
        insert_rows = rt.bat_ins_rows 
from    batch_info bi
        inner join ref_table rt on rt.bat_num = bi.batch_num 
        inner join 
        (
            select  bat_num,
                    MIN(bat_end_date) as min_bat_end_date
            from    ref_table 
        ) oldest on oldest.bat_num = rt.bat_num 
                    and oldest.min_bat_end_date = rt.bat_end_date 
where   bi.batch_end is null

insert into batch_info 
(batch_num, batch_name, batch_start, batch_end, table_name, insert_rows)
select  bi.batch_num,
        bi.batch_name,
        bi.batch_start,
        rt.bat_end_date,
        rt.bat_table_name,
        rt.bat_ins_rows 
from    batch_info bi
        inner join ref_table rt on rt.bat_num = bi.batch_num 
where   not exists 
        (
            select   * 
            from     batch_info e 
            where    e.batch_num = bi.batch_num 
                     and e.batch_end = rt.bat_end_date
        )

If another combination of values establishes uniqueness, then those can be added to oldest in the first query, and e in the second.

I agree with @Jim that this should be split into two tables. The difficulty of performing this operation is a harbinger of the difficulties that this design is going to create. You are trying to put two different things (batches and batch runs) into a single table, and that is never a good idea.

陪你搞怪i 2024-11-08 05:30:35
insert into batch_info
SELECT batch_num, batch_name, batch_start,
bat_end, bat_table_name, bat_ins_rows
FROM batch_info inner join ref_table
on batch_info.batch_num = ref_table.bat_num

delete from batch_info
where batch_end is null

编辑:INSERT 正在生成所有匹配的行&重新插入。新的身份值将添加到其中。现有行(其他字段为空)用于 INSERT 语句 &后来使用第二条语句删除。

insert into batch_info
SELECT batch_num, batch_name, batch_start,
bat_end, bat_table_name, bat_ins_rows
FROM batch_info inner join ref_table
on batch_info.batch_num = ref_table.bat_num

delete from batch_info
where batch_end is null

EDIT: The INSERT is generating all matching rows & inserting it afresh. New Identity values will be added to those. The existing row (with other fields null) is used for INSERT statement & later deleted using the 2nd statement.

硬不硬你别怂 2024-11-08 05:30:35

我本打算向您指出合并声明,但我不这样做也不认为这可以用那个来完成。我认为您将无法使用 光标

话虽如此,但不知道您现有的要求,它确实应该是两个表。将所有内容推入一张表会使表设计非规范化。

I was going to point you to the Merge Statement, but I don't think this can be done with that either. I think you are going to be stuck using a Cursor.

With that said and not knowing your existing requirements, it really should be two tables. Pushing all into one table is denormalizing the table design.

究竟谁懂我的在乎 2024-11-08 05:30:35

使batch_info.batch_key字段自动递增。

create table batch_info (
batch_key int identity(1, 1) not null primary key, -- <<- make autoincrement.
...

Make the batch_info.batch_key field autoincrement.

create table batch_info (
batch_key int identity(1, 1) not null primary key, -- <<- make autoincrement.
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文