基于 SELECT 更新列,参数来自 UPDATE

发布于 2025-01-02 12:11:58 字数 1404 浏览 2 评论 0原文

我想做这样的事情。

UPDATE tbl_states AS ts
SET    tbl_states.country_id = (SELECT tbl_countries.country_id
                                FROM   tbl_states ts1
                                       JOIN tbl_countries
                                         ON tbl_countries.country_id_id =
                                            ts1.country_id
                                WHERE  ts1.country_id_id = ts.country_id_id)  

我想根据插入的新主键将来自不同数据库的 country_id 更新为 country_id新数据库。这里给你一个想法是架构。

CREATE TABLE [dbo].[tbl_countries](
    [country_id] [int] IDENTITY(1,1) NOT NULL,
    [country_id_id] [int] NULL,
    [country_name] [varchar](50) NULL)

country_id_id 是下一个表格中引用的 country_id,我将向您展示tbl_states

CREATE TABLE [dbo].[tbl_states](
    [state_id] [int] IDENTITY(1,1) NOT NULL,
    [state_name] [varchar](50) NULL,
    [country_id] [int] NULL,
    [state_abbr] [char](3) NOT NULL)

我想更新使用以下 select 语句将该表的 Country_idtbl_states 复制到上表的主列来获取主键。

SELECT tbl_countries.country_id
FROM   tbl_states_old
       JOIN tbl_countries
         ON tbl_countries.country_id_id = tbl_states_old.country_id  

对不起这个标题,我不知道这个叫什么。你能帮我解决这个问题吗?

I want to do something like this.

UPDATE tbl_states AS ts
SET    tbl_states.country_id = (SELECT tbl_countries.country_id
                                FROM   tbl_states ts1
                                       JOIN tbl_countries
                                         ON tbl_countries.country_id_id =
                                            ts1.country_id
                                WHERE  ts1.country_id_id = ts.country_id_id)  

I want to update old country_id which came from different database to new country_id based on new primary keys inserted on the new database. To give you an idea here is the schema.

CREATE TABLE [dbo].[tbl_countries](
    [country_id] [int] IDENTITY(1,1) NOT NULL,
    [country_id_id] [int] NULL,
    [country_name] [varchar](50) NULL)

country_id_id is the old country_id referenced on the next table I will show you tbl_states

CREATE TABLE [dbo].[tbl_states](
    [state_id] [int] IDENTITY(1,1) NOT NULL,
    [state_name] [varchar](50) NULL,
    [country_id] [int] NULL,
    [state_abbr] [char](3) NOT NULL)

I want to update country_id column of this table tbl_states to primary column of the table above using the following select statement to get the primary key.

SELECT tbl_countries.country_id
FROM   tbl_states_old
       JOIN tbl_countries
         ON tbl_countries.country_id_id = tbl_states_old.country_id  

Sorry for the title, I don't know what this is called. Could you help me with this?

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

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

发布评论

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

评论(1

十级心震 2025-01-09 12:11:58
UPDATE s
    SET country_id = c.country_id
    FROM tbl_states s
        INNER JOIN tbl_countries c
            ON s.country_id = c.country_id_id
UPDATE s
    SET country_id = c.country_id
    FROM tbl_states s
        INNER JOIN tbl_countries c
            ON s.country_id = c.country_id_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文