只是想知道是否有人可以帮我弄清楚为什么这个T-SQL脚本不起作用。我在SQL方面的经验充其量只是新手。
DELETE FROM ActiveUsers
WHERE HostName + CAST(HostId AS CHAR) IN (SELECT HostName + CAST(HostId AS CHAR)
FROM ActiveUsers ACTUSR
WHERE NOT EXISTS (SELECT NULL)
FROM master.dbo.sysprocesses SYSPRC
WHERE SYSPRC.HostName = ACTUSR.HostName COLLATE database_default
AND SYSPRC.HostProcess = ACTUSR.HostId
GROUP BY SYSPRC.HostName, SYSPRC.HostProcess))
错误消息是:
Executed as user: . Incorrect syntax near ' '. [SQLSTATE 42000] (Error 102) Incorrect syntax near ' '. [SQLSTATE 42000] (Error 102). The step failed.
答案 0 :(得分:0)
看起来你在行中有一个额外的右括号
WHERE NOT EXITS (SELECT NULL)
<强>更新强>
好的,你说你在选择null之后删除了括号,在这种情况下,以下2点也适用
GROUP BY
子句是多余的,可以安全删除,因为您没有聚合函数,而只是检查结果NOT EXISTS
。ActiveUsers
表中删除HostName
表中未找到HostId
和the master.dbo.sysprocesses
的记录。试试这个,我已经测试过,它对我来说很好。
DELETE FROM ActiveUsers
WHERE NOT EXISTS (SELECT NULL
FROM master.dbo.sysprocesses SYSPRC
WHERE SYSPRC.HostName = ActiveUsers.HostName COLLATE database_default
AND SYSPRC.HostProcess = ActiveUsers.HostId)