比较来自世界各地的卖家的域名和 IT 服务价格

如何使用其他表更新这些列 TSQL

我刚刚理解,我在我的表中收集了一个列的错误数据。 我纠正了这个问题,但到目前为止我收集的数据仍然是错误的。

让我们打电话给我的表
TableIWantToCorrect


TableWithIDs



TableIWantToCorrect

我有一个外部关键
TableWithIDs

. 这就是错误的。

通过比较列子字符串可以纠正数据
TableIWantToCorrect

柱B.
TableWithIDs

.

所以现在我有

TableIWantToCorrect


Name ForeignKey
123-abc-123 15
456-def-456 15
789-ghi-789 15


TableWithIDs


CompareName id
abc 1
def 2
ghi 3


所以我想更新
TableIWantToCorrect

, 有正确的价值 ForeignKey, 当名称中的子字符串等于按比较名称的子字符串时。 子字符串的位置始终相同,因此我可以使用该方法
Substring

.

我的尝试 :


Update TableIWantToCorrect
SET ForeignKey =
/SELECT id
FROM TableWithIDs
WHERE UPPER/CompareName/ = UPPER//SUBSTRING/TableIWantToCorrect.Name, 4, 3///


结果 :

子查询回到了更多 1 价值观。 不允许何时
子查询遵循 =, !=, <, <= , >, >= 或者当使用子查询作为
表达。 申请已停止。

我知道什么愚蠢了。 我在这里做错了什么 ?
已邀请:

龙天

赞同来自:

错误是您的子查询返回多个记录
UPDATE

. 要解决它,您可以用它
JOIN

跟你的
UPDATE



UPDATE t1
SET ForeignKey = t2.id
FROM TableIWantToCorrect t1
INNER JOIN TableWithIDs t2
ON UPPER/t2.CompareName/ = UPPER/SUBSTRING/t1.Name, 4, 3//

八刀丁二

赞同来自:

Update TableIWantToCorrect
SET ForeignKey = s.id
FROM TableIWantToCorrect , TableWithIDs as s
WHERE UPPER/s.CompareName/ = UPPER/ /SUBSTRING/TableIWantToCorrect.Name, 4, 3//

冰洋

赞同来自:

--CREATE FUNCTION dbo.ufn_FindReports 
--/@InEmpID INTEGER/
--RETURNS @retFindReports TABLE
--/
-- EmployeeID int primary key NOT NULL,
-- FirstName nvarchar/255/ NOT NULL,
-- LastName nvarchar/255/ NOT NULL,
-- JobTitle nvarchar/50/ NOT NULL

--/
----Returns a result set that lists all the employees who report to the
----specific employee directly or indirectly.*/
--AS
--BEGIN
--WITH EMP_cte/EmployeeID, OrganizationNode, FirstName, LastName, JobTitle, RecursionLevel/ -- CTE name and columns
-- AS /
-- SELECT e.EmployeeID, e.ManagerID, p.FirstName, p.LastName, P.JobTitle, 0 -- Get the initial list of Employees for Manager n
-- FROM HumanResources.Employee e
--INNER JOIN Person.Person p
--ON p.Employeeid = e.EmployeeID
-- WHERE e.EmployeeID = @InEmpID
-- UNION ALL
-- SELECT e.EmployeeID, e.ManagerID, p.FirstName, p.LastName, P.JobTitle, RecursionLevel + 1 -- Join recursive member to anchor
-- FROM HumanResources.Employee e
-- INNER JOIN EMP_cte
-- ON e.ORGANIZATIONNODE.GetAncestor/1/ = EMP_cte.OrganizationNode
--INNER JOIN Person.Person p
--ON p.Employeeid= e.EmployeeID
-- /
---- copy the required columns to the result of the function
-- INSERT @retFindReports
-- SELECT EmployeeID, FirstName, LastName, JobTitle, RecursionLevel
-- FROM EMP_cte
-- RETURN
--END;
--GO

>

要回复问题请先登录注册