简体   繁体   English

是否有 SQL function 通过用另一列(在相关表中)的数据(不同类型)替换一列来更新表?

[英]Is there SQL function to update table by replacing one column with data (DIFFERENT TYPE) from another column (in a related table)?

I run into a problem in SQL, I don't know how to update table A by replacing its column "Edu" with another column "Level" of table B.我在 SQL 中遇到问题,我不知道如何通过将表 A 的列“Edu”替换为表 B 的另一列“级别”来更新表 A。

  • Two tables have a relationship in column "Edu"两个表在“Edu”列中有关系
  • Different data types bt "Edu" (number) & "Level" (Short Text) Can anyone explain why the below code does not work?不同的数据类型bt "Edu" (number) & "Level" (Short Text) 谁能解释为什么下面的代码不起作用? And kindly suggest a solution.并请提出解决方案。 Thanks!谢谢!

UPDATE A SET A.Edu= CAST(B.Level AS Varchar(Max)) FROM A INNER JOIN B ON A.Edu=B.Edu; UPDATE A SET A.Edu= CAST(B.Level AS Varchar(Max)) FROM A INNER JOIN B ON A.Edu=B.Edu;

Maybe an idea.也许是一个想法。 I think your query doesnt work because you try to update a key using a the same time this key, A.Edu.我认为您的查询不起作用,因为您尝试使用此密钥 A.Edu 更新密钥。

My solution: create in the table A a column EduBis with same value as Edu.我的解决方案:在表 A 中创建与 Edu 具有相同值的 EduBis 列。

Then update with A.EduBis=B.Edu in you clause in the join.然后在连接中的 you 子句中使用 A.EduBis=B.Edu 进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM