在MSSQL中将某些字段从一个表复制到另一个表
我正在尝试将某些行的字段从一个表复制到另一个表。我正在通过以下查询尝试:在MSSQL中将某些字段从一个表复制到另一个表,sql,sql-server,Sql,Sql Server,我正在尝试将某些行的字段从一个表复制到另一个表。我正在通过以下查询尝试: update table1 set goal = t2.Goal, notes = t2.Notes from Table2 AS t2 join Table3 AS t3 ON t3.ID = t2.PID join table1 as t1 on t1.title = Title and Name like t1.name + '%' 我需要
update table1
set goal = t2.Goal, notes = t2.Notes
from
Table2 AS t2
join Table3 AS t3
ON t3.ID = t2.PID
join table1 as t1
on t1.title = Title
and Name like t1.name + '%'
我需要将前两个表连接起来,以获得名称和标题,第三个表使用标题和名称作为标识符。此查询有效,但并非适用于表1中的所有行-有一些行没有复制的数据。
我做错了什么?我想你需要这样做:
update t1
set t1.goal = t2.Goal, t1.notes = t2.Notes
from
Table2 AS t2
JOIN Table3 AS t3 ON t3.ID = t2.PID
JOIN table1 as t1 ON t1.title = t3.Title
AND t2.Name like t1.name + '%'
我认为你需要这样做:
update t1
set t1.goal = t2.Goal, t1.notes = t2.Notes
from
Table2 AS t2
JOIN Table3 AS t3 ON t3.ID = t2.PID
JOIN table1 as t1 ON t1.title = t3.Title
AND t2.Name like t1.name + '%'
试试这样的
update Table1
set Goal = t2.Goal,
Notes = t2.Notes
from
Table2 AS t2
join Table3 AS t3
ON t3.ID = t2.PID
where
Table1.Title = t3.Title AND
t2.Name like (Table1.Name + '%')
下面是一个基于您的模式的示例请尝试以下内容
update Table1
set Goal = t2.Goal,
Notes = t2.Notes
from
Table2 AS t2
join Table3 AS t3
ON t3.ID = t2.PID
where
Table1.Title = t3.Title AND
t2.Name like (Table1.Name + '%')
下面是一个基于您的模式的示例名称和标题属于哪些表?名称来自表2,标题来自表3只需映射您的列:将表1作为t1连接到t1上。Title=t3。Title和t2。名称类似t1.Name+“%”名称和标题属于哪些表?名称来自表2,标题来自表3只需映射您的列:将表1作为t1连接到t1上t1.title=t3.title和t2.Name,比如t1.Name+'%'我认为这会引发语法错误。根据我在
UPDATE
关键字之后的经验,您必须指定要修改数据的表的别名。然后你在查询的部分中“调用”这个表。是的,你是对的,我完全忘记了……它已经编辑好了,现在应该可以工作了,谢谢你的建议。我想这会引发语法错误。根据我在UPDATE
关键字之后的经验,您必须指定要修改数据的表的别名。然后你在查询的部分“调用”这个表。是的,你是对的,我完全忘记了……它已经编辑好了,现在应该可以工作了,谢谢你的建议