SQL更新语句语法错误

发布于 2024-08-04 14:26:48 字数 240 浏览 1 评论 0原文

如何更正以下内容,以便在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

北方的巷 2024-08-11 14:26:48

要为名称添加别名,您必须使用 FROM:

UPDATE Emp 
SET UserName = Left(FirstName,1)+LastName
FROM Emp E
WHERE NOT EXISTS(   
  SELECT * FROM Emp    
  WHERE UserName=Left(E.FirstName,1)+E.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)

To alias the name you must use FROM:

UPDATE Emp 
SET UserName = Left(FirstName,1)+LastName
FROM Emp E
WHERE NOT EXISTS(   
  SELECT * FROM Emp    
  WHERE UserName=Left(E.FirstName,1)+E.LastName)

Or alias the sub-query:

UPDATE Emp 
SET UserName = Left(FirstName,1)+LastName
WHERE NOT EXISTS(   
  SELECT * FROM Emp E    
  WHERE E.UserName=Left(Emp.FirstName,1)+Emp.LastName)
九命猫 2024-08-11 14:26:48

未经测试...

UPDATE E
SET UserName = Left(FirstName,1)+LastName
FROM Emp E
WHERE NOT EXISTS(   
                 SELECT * FROM Emp    
                 WHERE UserName=Left(E.FirstName,1)+E.LastName
                )

Untested...

UPDATE E
SET UserName = Left(FirstName,1)+LastName
FROM Emp E
WHERE NOT EXISTS(   
                 SELECT * FROM Emp    
                 WHERE UserName=Left(E.FirstName,1)+E.LastName
                )
摘星┃星的人 2024-08-11 14:26:48

这里有 2 种语法。要使用别名作为更新目标,请执行以下操作:

UPDATE e
    SET UserName = Left(FirstName,1)+LastName
    FROM Emp e
    WHERE NOT EXISTS(
       SELECT * FROM Emp
       WHERE UserName=Left(E.FirstName,1)+E.LastName
    )
    AND EmpID=1

there are 2 syntaxes here. To use an alias as the target of the update you do the following:

UPDATE e
    SET UserName = Left(FirstName,1)+LastName
    FROM Emp e
    WHERE NOT EXISTS(
       SELECT * FROM Emp
       WHERE UserName=Left(E.FirstName,1)+E.LastName
    )
    AND EmpID=1
离笑几人歌 2024-08-11 14:26:48

如果我理解正确的话,这就是你想要做的。不过,我不确定 WHERE 子句的第一部分是否真的必要,除非有大量的行......

UPDATE Emp
SET UserName = Left(FirstName,1)+LastName
WHERE UserName<>Left(FirstName,1)+LastName
AND EmpID=1

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...

UPDATE Emp
SET UserName = Left(FirstName,1)+LastName
WHERE UserName<>Left(FirstName,1)+LastName
AND EmpID=1
破晓 2024-08-11 14:26:48

更新员工
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
)

终弃我 2024-08-11 14:26:48

我已经有一段时间没有尝试过这种语法了...但是在 SQL Server 中,您可以在更新时指定 from 。

UPDATE Emp SET 
   UserName = Left(FirstName,1)+LastName
FROM Emp e1
WHERE NOT EXISTS (
   SELECT * 
   FROM Emp e2
   WHERE e2.UserName=Left(e1.FirstName,1)+e1.LastName
)

编辑:我的语法当然可以运行,但我不确定它是否正确。不管它是否正确,我建议在更新语句中使用别名只是为了确保其他人可以更好地理解你在做什么。

UPDATE e1 SET 
...
FROM Emp e1
...

It's been a while since I've tried this syntax... but in SQL Server you can specify a from on an update.

UPDATE Emp SET 
   UserName = Left(FirstName,1)+LastName
FROM Emp e1
WHERE NOT EXISTS (
   SELECT * 
   FROM Emp e2
   WHERE e2.UserName=Left(e1.FirstName,1)+e1.LastName
)

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.

UPDATE e1 SET 
...
FROM Emp e1
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文