基于 SELECT 更新列,参数来自 UPDATE
我想做这样的事情。
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_id
列 tbl_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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)