sql server:如何用查询结果批量替换字段
情况如下,数据库已创建但未标准化。表LUT_ProductInfo 包含一个名为flavor 的字段,该字段填充有实际的风味名称(即Coke Classic)。我创建了一个查找表 LUT_Flavors,其中包含所有带有 PK 的风味名称。我需要将 LUT_Product 信息中的所有风味名称替换为 LUT_Flavors 表中相应的外键。目前,每个表中的风味名称都是相同的,因此交换应该是干净的。
我尝试了这样的事情:
update LUT_ProductInfo
set flavor =
(select LUT_Flavor.id, LUT_Flavor.flavor
from LUT_ProductInfo prod
join LUT_Flavor on LUT_Flavor.flavor = prod.flavor).ID
where
LUT_ProductInfo.flavor = (select LUT_Flavor.id,LUT_Flavor.flavor
from LUT_ProductInfo prod
join LUT_Flavor
on LUT_Flavor.flavor = prod.flavor).flavor
但这违反了我认为的一些规则并且没有做任何事情。有人有任何见解吗?
Heres the situation, a database was created and not normalized. The table LUT_ProductInfo contains a field called flavor that is populated with an actual flavor name (ie Coke Classic). I created a lookup table LUT_Flavors with all of the flavor names complete with PKs. I need to replace all of the flavor names in LUT_Product info with the corresponding foreign key from the LUT_Flavors table. At the moment, the flavor names in each table are identical so the swap should be clean.
I tried something like this:
update LUT_ProductInfo
set flavor =
(select LUT_Flavor.id, LUT_Flavor.flavor
from LUT_ProductInfo prod
join LUT_Flavor on LUT_Flavor.flavor = prod.flavor).ID
where
LUT_ProductInfo.flavor = (select LUT_Flavor.id,LUT_Flavor.flavor
from LUT_ProductInfo prod
join LUT_Flavor
on LUT_Flavor.flavor = prod.flavor).flavor
But that broke some rules I guess and didn't do anything. Does anyone have any insight?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
抱歉,我正在路上,所以没有数据库可供尝试 - 但请尝试:
Sorry, am on the road, so no database to try this on - but try:
这是我将如何做的一个例子。请注意,我向 LUT_ProductInfo 表添加了一个flavorid 列。这只是为了确保传输顺利进行,您可以稍后删除/重命名它,或者如果您勇敢的话,可以直接进入该列。不过,您可能想在更新每条记录之前备份表,但您不想丢失任何内容。
无论如何,这是我的测试代码:
Here's an example how I would do it. Note that I add a flavourid column to the LUT_ProductInfo table. This is just to ensure that the transfer went smoothly, you can delete/rename it later or just go straight into the column if you're brave. You might want to back up your table before updating every record though, you wouldn't want to lose anything.
Anyway, here's my test code:
如果没有错误消息很难判断。
也许可以尝试:
(select LUT_Flavor.id from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor.flavor = prod.flavor)
而不是
(select LUT_Flavor.id,LUT_Flavor.flavor from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor .flavor = prod.flavor).ID
如果没有帮助,请粘贴错误消息。
Hard to tell without the error message.
Maybe try:
(select LUT_Flavor.id from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor.flavor = prod.flavor)
instead of
(select LUT_Flavor.id,LUT_Flavor.flavor from LUT_ProductInfo prod join LUT_Flavor on LUT_Flavor.flavor = prod.flavor).ID
If it doesn't help please paste error message.
更好的方法是:
使用 from 子句并以这种方式加入。此外,此解决方案假定风味和 ID 之间的数据类型是兼容的,如果 id 是 int 并且风味是 nvarchar(x),您可能不会得到您想要的结果。您可能必须进行更新,然后更改数据类型。
A better way to do this is:
having a from clause and joining that way. Also this solution presumes that the data types between flavor and ID are compatible, if id is an int and the flavor is nvarchar(x) you might not get the results you want. You'll probably have to do the update, then change the data type.