SQL更新语句语法错误
如何更正以下内容,以便在 Microsoft SQL Server 2005 中不会收到语法错误?
UPDATE Emp E
SET UserName = Left(FirstName,1)+LastName
WHERE EmpID=1
AND NOT EXISTS(
SELECT * FROM Emp
WHERE UserName=Left(E.FirstName,1)+E.LastName
)
How can I correct the following so that I don't receive a syntax error in Microsoft SQL Server 2005?
UPDATE Emp E
SET UserName = Left(FirstName,1)+LastName
WHERE EmpID=1
AND NOT EXISTS(
SELECT * FROM Emp
WHERE UserName=Left(E.FirstName,1)+E.LastName
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要为名称添加别名,您必须使用 FROM:
或为子查询添加别名:
To alias the name you must use FROM:
Or alias the sub-query:
未经测试...
Untested...
这里有 2 种语法。要使用别名作为更新目标,请执行以下操作:
there are 2 syntaxes here. To use an alias as the target of the update you do the following:
如果我理解正确的话,这就是你想要做的。不过,我不确定 WHERE 子句的第一部分是否真的必要,除非有大量的行......
If I'm understanding correctly, this is what you're trying to do. Though, I'm not sure the first part of the WHERE clause is really necessary unless there's a ton of rows...
更新员工
SET 用户名 = 左(名字,1)+姓氏
不存在的地方(
选择 *
来自 Emp e
WHERE e.UserName=Left(emp.FirstName,1)+emp.LastName
)
UPDATE Emp
SET UserName = Left(FirstName,1)+LastName
WHERE NOT EXISTS (
SELECT *
FROM Emp e
WHERE e.UserName=Left(emp.FirstName,1)+emp.LastName
)
我已经有一段时间没有尝试过这种语法了...但是在 SQL Server 中,您可以在更新时指定 from 。
编辑:我的语法当然可以运行,但我不确定它是否正确。不管它是否正确,我建议在更新语句中使用别名只是为了确保其他人可以更好地理解你在做什么。
It's been a while since I've tried this syntax... but in SQL Server you can specify a from on an update.
EDIT: My syntax certainly runs but I'm not certain that it's correct. Regardless of whether or not it's right, I would suggest using the alias in the update statement just to ensure that others can better understand what you are doing.