如何将数据从一列插入另一列 table

How to insert the data from one column into another table

我在工作中遇到这个问题已经有一段时间了,我需要一些帮助。

我有 2 个 table,一个包含大量信息,另一个包含部分信息,我需要将一列移动到已经用一些代码创建的 table识别要插入的每一行(主要是用户),它会抛出一个错误,因为它说我还必须插入一个 ID,但 ID 已经在那里了。

假设我有这些 tables:

table_information
admin_name|admin_address|admin_phone|admin_mail
john      |home         |111111111  |something@mail.com
pete      |house        |222222222  |else@mail.com
mike      |crib         |333333333  |my@mail.com
ryan      |someplace    |444444444  |custom@mail.com
daniel    |somewhere    |555555555  |user@mail.com
bruce     |anywhere     |666666666  |bruce@mail.com
dave      |everywhere   |777777777  |dave@mail.com


table_admin
admin_id|admin_name
1       |
2       | 
3       |
4       |
5       |
6       |
7       |

我需要将用户名列移动到具有代码的 table 用户,因此结果必须是这样的:

table_information
admin_name|admin_address|admin_phone|admin_mail
john      |home         |111111111  |something@mail.com
pete      |house        |222222222  |else@mail.com
mike      |crib         |333333333  |my@mail.com
ryan      |someplace    |444444444  |custom@mail.com
daniel    |somewhere    |555555555  |user@mail.com
bruce     |anywhere     |666666666  |bruce@mail.com
dave      |everywhere   |777777777  |dave@mail.com

table_admin
admin_id|admin_name
1       | john
2       | pete
3       | mike
4       | ryan
5       | daniel
6       | bruce
7       | dave

信息来自一个大数据库(大约 1500 行),其中重复了管理员名称,因此我一直在使用 select distinct 来过滤一些重复的行。

我的做法是这样的:

INSERT INTO table_admin (admin_name)
SELECT DISTINCT admin_name FROM table_information

我得到了这个:

Cannot insert the value NULL into column 'admin_id' column does not allow nulls. INSERT fails.

同样重要的是 table table_admin 中的列 admin_id 不能是身份(或自动递增)所以不能自动生成 id(我会做是这样,但这就是他们在这里工作的方式。

我缺什么吗?做错了什么?请让我知道一些建议或解决这个让我发疯的问题的方法。

提前感谢阅读。

Cannot insert the value NULL into column 'admin_id' column does not allow nulls. INSERT fails.

您收到此错误的原因是 admin_id 不是 NULLABLE,这意味着您 必须 每一行都有一个 admin_id。当您执行 INSERT 时,您必须显式插入 admin_id,因为它不是 IDENTITY 属性 列。

您的预期结果表明您想要 UPDATE,而不是 INSERTUPDATEUPDATE 当前在 table_admin table 中的 admin_id,而不是插入新行。 唯一不清楚的是 table_admintable_information 的关系。 john 如何被分配 admin_id = 1?

定义后,这里是 UPDATE

update t 
set t.admin_name = i.admin_name
from table_admin t
inner join table_information i on i.someColumn = t.someColumn

如果您不关心哪个 admin_name 得到什么 admin_id,那么您可以创建一个代理键并更新您的 table。它看起来像这样:

select distinct 
    admin_name
    ,admin_id = row_number() over (order by (select null))
into #tempInformation
from table_information

--this is going to show the admin_id that will be updated in the table_admin table
select * from #tempInformation

update t 
set t.admin_name = i.admin_name
from table_admin t
inner join #tempInformation i on i.admin_id = t.admin_id

编辑

如果你没有 any admin_name 当前填充在 table_admin 那么我建议你只是截断 table 并插入你独特的 admin_name。同样,我也会使 admin_id 成为身份 属性

truncate table table_admin

insert into table_admin (admin_id, admin_name)
   select distinct 
        admin_id = row_number() over (order by (select null))
        ,admin_name

我要强调的是,在大多数模式中,admin_id 都意味着什么。它将是此 table 的 PRIMARY KEY 并用于将此 table 与数据库中的其他人相关联。因此,不知道哪个 admin_id 去哪个 admin_name 意味着你手上真的一团糟并随机分配它们 应该 破坏下游流程......但是情况可能并非如此,因为您一开始就没有坚持下去。

the column admin_id from the table table_admin could not be identity

我会问为什么?这暗示您已经有了 admin_nameadmin_id 的映射...但是您还没有显示出来。