SQL 返回相同的数据或重复
SQL Returning the same Data or Repeats
从另一个 table 更新一个列,我的尝试是对已经在 Speedlink_ID 中的相同数据进行排队 我正在尝试更新 CON_SP_ID 来自 CON_SP_ID
Table 1 CON_SP_ID 所有行都是空白或 NULL
Table 2*Speedlink_ID* 有数据
两列的行数相同。
尝试 1 - Returns 没有更新数据。
UPDATE t1
SET CON_SP_ID = t2.Speedlink_ID
FROM dbo.Data1 AS t1
INNER JOIN dbo.Data2 AS t2
ON t1.CON_SP_ID = t2.[Speedlink_ID]
SELECT TOP (1000) [CON_SP_ID]
FROM Data1
尝试 2
UPDATE t -- alias of table you are updating
SET CON_SP_ID = d.Speedlink_ID
FROM Data1 t
join Data2 d on d.Speedlink_ID = t.CON_SP_ID
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]
尝试 3
UPDATE [Data1]
SET [Data1].CON_SP_ID = (
SELECT CON_SP_ID
FROM [DATA2]
WHERE [Data1].CON_SP_ID = [DATA2].CON_SP_ID)
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]
ATTEMPT 4 - 重复 [DATA2] 的第一行。[Speedlink_ID] 到 table
的末尾
UPDATE [Data1]
SET [Data1].CON_SP_ID = [DATA2].[Speedlink_ID]
FROM [DATA2]
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]
Shows Repeats
您的脚本正在更新列 CON_SP_ID。这不是我认为您想要的 - 如果我错了请纠正我。相反,我预计您想要更新 table 中的 other 列。您没有提供任何其他列名,因此我在示例代码中使用了通用列:untested
UPDATE t1
SET column_A = t2.column_A, column_B = t2.column_B, column_C = t2.column_C
FROM dbo.Data1 AS t1
INNER JOIN dbo.Data2 AS t2 ON t1.CON_SP_ID = t2.Speedlink_ID
谢谢@avery_larry
我确实意识到您需要一个在两个 table 上都相同的列。子网的排序顺序/输入方式与我的第二个 table 相同。我需要使用子网和我的 KEY 逐行匹配,因为它是唯一编号。
还在学习中,谢谢大家
UPDATE [Data1]
SET [Data1].[CON_SP_ID] = [Data2].[Con_SP_ID]
FROM
dbo.Data1 as Data1
INNER JOIN
dbo.Data2 AS Data2
ON
Data1.Subnet = Data2.Subnet
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]
从另一个 table 更新一个列,我的尝试是对已经在 Speedlink_ID 中的相同数据进行排队 我正在尝试更新 CON_SP_ID 来自 CON_SP_ID Table 1 CON_SP_ID 所有行都是空白或 NULL Table 2*Speedlink_ID* 有数据
两列的行数相同。
尝试 1 - Returns 没有更新数据。
UPDATE t1
SET CON_SP_ID = t2.Speedlink_ID
FROM dbo.Data1 AS t1
INNER JOIN dbo.Data2 AS t2
ON t1.CON_SP_ID = t2.[Speedlink_ID]
SELECT TOP (1000) [CON_SP_ID]
FROM Data1
尝试 2
UPDATE t -- alias of table you are updating
SET CON_SP_ID = d.Speedlink_ID
FROM Data1 t
join Data2 d on d.Speedlink_ID = t.CON_SP_ID
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]
尝试 3
UPDATE [Data1]
SET [Data1].CON_SP_ID = (
SELECT CON_SP_ID
FROM [DATA2]
WHERE [Data1].CON_SP_ID = [DATA2].CON_SP_ID)
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]
ATTEMPT 4 - 重复 [DATA2] 的第一行。[Speedlink_ID] 到 table
的末尾UPDATE [Data1]
SET [Data1].CON_SP_ID = [DATA2].[Speedlink_ID]
FROM [DATA2]
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]
Shows Repeats
您的脚本正在更新列 CON_SP_ID。这不是我认为您想要的 - 如果我错了请纠正我。相反,我预计您想要更新 table 中的 other 列。您没有提供任何其他列名,因此我在示例代码中使用了通用列:untested
UPDATE t1
SET column_A = t2.column_A, column_B = t2.column_B, column_C = t2.column_C
FROM dbo.Data1 AS t1
INNER JOIN dbo.Data2 AS t2 ON t1.CON_SP_ID = t2.Speedlink_ID
谢谢@avery_larry
我确实意识到您需要一个在两个 table 上都相同的列。子网的排序顺序/输入方式与我的第二个 table 相同。我需要使用子网和我的 KEY 逐行匹配,因为它是唯一编号。
还在学习中,谢谢大家
UPDATE [Data1]
SET [Data1].[CON_SP_ID] = [Data2].[Con_SP_ID]
FROM
dbo.Data1 as Data1
INNER JOIN
dbo.Data2 AS Data2
ON
Data1.Subnet = Data2.Subnet
SELECT TOP (1000) [CON_SP_ID]
FROM [Data1]