[英]SQL Server Updating a column in one table with column from another
noob SQL question here. 菜鸟SQL问题在这里。 I have a database in SQL Server 2012 and access to another MySQL database with information I'd like to pull. 我在SQL Server 2012中有一个数据库,并且可以访问包含我想提取的信息的另一个MySQL数据库。
I would like to update a column representing quantities of a workstation, and that information is present on the other database. 我想更新一个代表工作站数量的列,该信息存在于另一个数据库中。 I would like to structure a query that updates quantities of all items where model numbers match in each database. 我想构建一个查询,以更新每个数据库中型号匹配的所有项目的数量。
I'll include two select statements to demonstrate what I'd like to work with. 我将包含两个select语句来演示我想使用的内容。
The data from the other database I want to pull from. 我要从另一个数据库中提取的数据。
SELECT *
FROM OPENQUERY(S3MONITOR,
'SELECT count(BuildDriver) AS ''# of Workstations'', BuildDriver AS ''Workstation Model''
FROM workstation
GROUP BY BuildDriver')
Which produces this result 哪个产生这个结果
My database that I'd like to update. 我要更新的数据库。
SELECT NumOfDevices, Model
FROM dbo.Currency
INNER JOIN dbo.SubCategory
ON Currency.SubCategoryId = SubCategory.SubCategoryId
WHERE SubCategory.SubCategoryName = 'WorkStation';
Which produces this result 哪个产生这个结果
The models will be updated in my database to make sure model names correspond to one another but in the interim I'd like to test a query using the M93z. 这些模型将在我的数据库中更新,以确保模型名称彼此对应,但是在此期间,我想使用M93z测试查询。 So what would be the most ideal way of updating NumofDevices in my database using # of Workstations in the other database, where the model names are equal? 那么,使用模型名称相同的其他数据库中的#个工作站更新数据库中NumofDevices的最理想方法是什么?
Sorry if this is an obvious question, I'm still an SQL noob and it's still not intuitive to me when it comes to these kinds of things. 抱歉,如果这是一个显而易见的问题,我仍然是SQL菜鸟,对这类事情,我还是不直观。
EDIT: The end goal is to routinely update all Workstation quantities nightly through an SQL Server Agent scheduled job but I'll cross that bridge when I come to it (I actually have some faith that I'll be able to apply the query solution to this job as soon as it's figured out) but I want to include this information in the event that it changes any of your suggestions. 编辑:最终目标是通过SQL Server Agent计划的作业定期每晚更新所有工作站数量,但是当我遇到它时,我将跨越那座桥(我实际上相信我将能够将查询解决方案应用于这项工作一经确定就可以解决),但如果此信息更改了您的任何建议,我想将其包括在内。
You can use join
in the update
statement. 您可以在update
语句中使用join
。
with newvals as (
SELECT *
FROM OPENQUERY(S3MONITOR,
'SELECT count(BuildDriver) AS ''# of Workstations'', BuildDriver AS ''Workstation Model''
FROM workstation
GROUP BY BuildDriver'
) oq
)
update c
set NumOfDevices = newvals.num_workstations
from dbo.Currency c join
dbo.SubCategory sc
on c.SubCategoryId = sc.SubCategoryId join
newvals
on newvals.model = c.model
where sc.SubCategoryName = 'WorkStation';
Note: This updates values already in the table. 注意:这将更新表中已有的值。 You might want to use merge
if you want to add new rows as well. 如果还想添加新行,则可能要使用merge
。 If this is the case, ask another question, because this question is specifically about updating. 在这种情况下,请问另一个问题,因为该问题专门与更新有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.