2
DELETE dbo.bBoxDetail
FROM dbo.bBoxDetail AS BD
INNER JOIN dbo.bBoxHeader AS BH ON LTRIM(RTRIM(BD.bBoxDetailId)) = LTRIM(RTRIM(BH.bBoxId))
WHERE LTRIM(RTRIM(BD.ESNs)) = (SELECT LTRIM(RTRIM(IMEI)) FROM dbo.tmpIMEI)

我收到此错误:

消息 512,级别 16,状态 1,第 1 行子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

BD.ESNsisNVARCHAR(50)IMEIisNVARCHAR(30)我有重复ESNs的,我想删除所有ESNs匹配的SELECT ... IMEI

4

2 回答 2

0

在 WHERE 子句中使用IN运算符

DELETE dbo.bBoxDetail
FROM dbo.bBoxDetail AS BD INNER JOIN dbo.bBoxHeader AS BH ON LTRIM(RTRIM(BD.bBoxDetailId)) = LTRIM(RTRIM(BH.bBoxId))
WHERE LTRIM(RTRIM(BD.ESNs)) IN (SELECT LTRIM(RTRIM(IMEI)) FROM dbo.tmpIMEI)

但首选选项是EXISTS运算符

DELETE dbo.bBoxDetail
FROM dbo.bBoxDetail AS BD INNER JOIN dbo.bBoxHeader AS BH ON LTRIM(RTRIM(BD.bBoxDetailId)) = LTRIM(RTRIM(BH.bBoxId))
WHERE EXISTS (
              SELECT 1
              FROM dbo.tmpIMEI
              WHERE LTRIM(RTRIM(BD.ESNs)) = LTRIM(RTRIM(IMEI))
              )
于 2013-07-09T16:06:04.657 回答
0

如果您阅读错误

消息 512,级别 16,状态 1,第 1 行子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的。

注意子查询返回超过 1 个值,因为这是错误,您从子查询返回任意数量的行:

正确的 SQL 如下:

DELETE dbo.bBoxDetail
FROM dbo.bBoxDetail AS BD
INNER JOIN dbo.bBoxHeader AS BH ON LTRIM(RTRIM(BD.bBoxDetailId)) = LTRIM(RTRIM(BH.bBoxId))
WHERE LTRIM(RTRIM(BD.ESNs)) = (SELECT TOP 1 LTRIM(RTRIM(IMEI)) FROM dbo.tmpIMEI)
于 2013-07-09T16:07:21.137 回答