使用来自另一个表中的 SELECT 的 UPDATE 语句覆盖 ID 值 SQL Server 2008

发布于 2024-11-29 17:59:31 字数 291 浏览 2 评论 0原文

我有两个表,其中正好有 20 行。我想用 Table1 中的 ID 值覆盖 Table2 的 ID 值,以便我可以使用 JOIN 查询进行测试。我将如何覆盖这些值?

我找到了指导如何使用 SELECT 语句进行 UPDATE 的帖子,但是它需要将数据连接到列上,在我的情况下没有列匹配。

UPDATE Table2
SET Table2.ID = Table1.ID
FROM Table1

上面的查询使用 Table1 中第一个 ID 列的值覆盖 Table2 中的所有 ID 列。

I have two tables that have exactly 20 rows in them. I want to overwrite Table2's ID values with the ID values from Table1 so that I can do tests with JOIN queries. How would I go about overwriting the values?

I have found posts instructing how to do an UPDATE using a SELECT statement, however it requires the data to be joined on a column, in my case no columns match.

UPDATE Table2
SET Table2.ID = Table1.ID
FROM Table1

The query above overwrites all ID columns in Table2 with the value of the first ID column in Table1.

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

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

发布评论

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

评论(4

梦里南柯 2024-12-06 17:59:31

为了好玩(是的,周五晚上我有一种扭曲的幽默感!),这里有一个查询可以做到这一点! - 我已经声明了表和顶部,并且每个表中只使用了 6 行,但您会得到这样的想法:

--Setup test data
declare @table1 table (ID int, Name varchar(10))
declare @table2 table (ID int, Name varchar(10))

insert @table1
    select ID = 1, Name = 'Item1'
    union select ID = 2, Name = 'Item2'
    union select ID = 3, Name = 'Item3'
    union select ID = 4, Name = 'Item4'
    union select ID = 5, Name = 'Item5'
    union select ID = 6, Name = 'Item6'

insert @table2
    select ID = 11, Name = 'Item11'
    union select ID = 12, Name = 'Item12'
    union select ID = 13, Name = 'Item13'
    union select ID = 14, Name = 'Item14'
    union select ID = 15, Name = 'Item15'
    union select ID = 16, Name = 'Item16'


--Do the update
update t1
    set 
        ID = t2.ID
from
    @table1 t1 --Assign a row number to each row of table 1
    cross apply (select rownum = COUNT(1) from @table1 sub where sub.ID <= t1.ID) x1,
    @table2 t2 --Assign a row number to each row of table 2
    cross apply (select rownum = COUNT(1) from @table2 sub where sub.ID <= t2.ID) x2
where x1.rownum = x2.rownum --Match the row numbers

更新:

基于 @sllev 建议的替代更新命令:

update t1
    set 
        ID = t2.ID
from
    (select id, rownum  = ROW_NUMBER() OVER(order by ID) from @table1) t1
    join (select id, rownum  = ROW_NUMBER() over (order by id) from @table2) t2
        on t1.rownum = t2.rownum

For fun (yes I have a warped sense of humour for a Friday night!), here is a query that does it! - I've declare the tables and the top and only used 6 rows in each table, but you'll get the idea:

--Setup test data
declare @table1 table (ID int, Name varchar(10))
declare @table2 table (ID int, Name varchar(10))

insert @table1
    select ID = 1, Name = 'Item1'
    union select ID = 2, Name = 'Item2'
    union select ID = 3, Name = 'Item3'
    union select ID = 4, Name = 'Item4'
    union select ID = 5, Name = 'Item5'
    union select ID = 6, Name = 'Item6'

insert @table2
    select ID = 11, Name = 'Item11'
    union select ID = 12, Name = 'Item12'
    union select ID = 13, Name = 'Item13'
    union select ID = 14, Name = 'Item14'
    union select ID = 15, Name = 'Item15'
    union select ID = 16, Name = 'Item16'


--Do the update
update t1
    set 
        ID = t2.ID
from
    @table1 t1 --Assign a row number to each row of table 1
    cross apply (select rownum = COUNT(1) from @table1 sub where sub.ID <= t1.ID) x1,
    @table2 t2 --Assign a row number to each row of table 2
    cross apply (select rownum = COUNT(1) from @table2 sub where sub.ID <= t2.ID) x2
where x1.rownum = x2.rownum --Match the row numbers

Update:

Alternative Update command based on suggestion by @sllev:

update t1
    set 
        ID = t2.ID
from
    (select id, rownum  = ROW_NUMBER() OVER(order by ID) from @table1) t1
    join (select id, rownum  = ROW_NUMBER() over (order by id) from @table2) t2
        on t1.rownum = t2.rownum
与之呼应 2024-12-06 17:59:31

还有另一个解决方案,这次使用 CTE。表#First从表#Second获取id:

CREATE TABLE #First ( ID INT NOT NULL )
INSERT INTO #First VALUES (1), (2), (3), (4), (5)
CREATE TABLE #Second ( ID INT NOT NULL )
INSERT INTO #Second VALUES (6), (7), (8), (9), (10)
GO

WITH first AS (SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum FROM #First)
   , second AS (SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum FROM #Second)
UPDATE #First
SET ID = s.ID
FROM second s
JOIN first f ON s.RowNum = f.RowNum
WHERE #First.ID = f.ID

SELECT * FROM #First

DROP TABLE #First;
DROP TABLE #Second;

And another solution, this time using CTE's. Table #First gets the id's from table #Second:

CREATE TABLE #First ( ID INT NOT NULL )
INSERT INTO #First VALUES (1), (2), (3), (4), (5)
CREATE TABLE #Second ( ID INT NOT NULL )
INSERT INTO #Second VALUES (6), (7), (8), (9), (10)
GO

WITH first AS (SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum FROM #First)
   , second AS (SELECT ID, ROW_NUMBER() OVER (ORDER BY ID) AS RowNum FROM #Second)
UPDATE #First
SET ID = s.ID
FROM second s
JOIN first f ON s.RowNum = f.RowNum
WHERE #First.ID = f.ID

SELECT * FROM #First

DROP TABLE #First;
DROP TABLE #Second;
浅忆 2024-12-06 17:59:31

由于其中只有 20 行,因此我将通过输入您的更改来手动执行更新。特别是因为这些记录并不像通常通过选择进行更新时那样相互关联。

Since you only have 20 rows in them, I would manually perform the update by typing in your changes. Especially since the records do not correlate to each other as they typically should for an update with a select.

请远离我 2024-12-06 17:59:31

正如评论中所述,如果这是一次性测试,则仅测试 20 行的最简单选项是在 SSMS 中打开 Table2 并输入 ID 值。

As stated in the comments, if this is a one-off for testing the easiest option for just 20 rows would be to open Table2 in SSMS and type the ID values in.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文