[英]Stored Procedure for reference table data matching and insert
I am working with SQL Server 2008. I have 2 tables here. 我正在使用SQL Server2008。这里有2个表。
shades_table color_name category location Aqua Blue House A Denim Blue House B Azure Blue House B Zaffre Blue House A Scarlet Red House C Ruby Red House D Chili Red House D Crimson Red House A objects_table object_name color_cat location2 Super Aqua shoes Null Null Azure wallet Null Null Awesome Scarlet chair Null Null Ruby socks Null Null Sparkly Denim chalk Null Null Chili paper Null Null Zaffre vase Null Null Sharp Crimson eraser Null Null Crimson watch Null Null Excellent Scarlet bucket Null
I have a reference table called shades_table. 我有一个称为shades_table的参考表。 A column contains color_name, and the other column contains the category of the color.
一列包含color_name,另一列包含颜色的类别。
I am working on the objects_table. 我正在上objects_table。 The object_name column contains the color name.
object_name列包含颜色名称。 I would want a procedure/program to automatically match the color_names to the Object_name, and then update the Null values in the color_cat column.
我想要一个过程/程序自动将color_names与Object_name匹配,然后更新color_cat列中的Null值。
My current method is to export the shades_table to a csv file. 我当前的方法是将shades_table导出到一个csv文件。 Then I wrote a Java program which reads the csv file, and output to a sql file to be run by the server:
然后,我编写了一个Java程序,该程序读取csv文件,并输出到要由服务器运行的sql文件:
UPDATE objects_table SET color_cat='Blue', location2 = 'House A' WHERE object_name LIKE '%Aqua%' AND color_cat IS NULL UPDATE objects_table SET color_cat='Blue', location2 = 'House B' WHERE object_name LIKE '%Denim%' AND color_cat IS NULL UPDATE objects_table SET color_cat='Blue', location2 = 'House B' WHERE object_name LIKE '%Azure%' AND color_cat IS NULL etc...
This method works, but it's stupid, cumbersome. 这种方法有效,但是笨拙,繁琐。 I just don't know how to write the SQL statements which can do what I want.
我只是不知道如何编写可以执行我想要的SQL语句。
Update: I have added an extra column which I also want to match.(Refer to my Java program output). 更新:我添加了一个我也想匹配的额外列。(请参阅Java程序输出)。 UPDATE statement with 2 SET doesn't work.
带有2 SET的UPDATE语句不起作用。 example
例
update o set o.color_cat = s.category, set o.location2 = s.location ...
doesn't work, gives "Incorrect syntax near the keyword 'set'. 无效,在关键字“设置”附近给出“语法不正确。
Here is the update query. 这是更新查询。
UPDATE a
SET a.color_cat=b.category
FROM objects_table a
JOIN shades_table b ON a.[object_name] LIKE '%'+b.[color_name]+'%'
WHERE a.color_cat IS null
If you want it as a procedure 如果您希望将其作为程序
CREATE PROCEDURE Updateobjectstable
AS
BEGIN
UPDATE a
SET a.color_cat=b.category
FROM objects_table a
JOIN shades_table b ON a.[object_name] LIKE '%'+b.[color_name]+'%'
WHERE a.color_cat IS null
END
I don't see a reason to use procedure for this task. 我看不出为此过程使用过程的理由。 Plain SQL should work.
普通SQL应该可以工作。
UPDATE objects_table ot
SET color_cat = st.category
FROM shades_table st
WHERE ot.object_name ILIKE concat('%', st.color_name, '%')
AND ot.color_cat IS NULL
Simple update
statement will do that for you: 简单的
update
语句将为您做到这一点:
update o
set o.color_cat = s.category
from objects_table o
left join shades_table s
on(o.[object_name] like '%' + s.color_name + '%')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.