简体   繁体   English

在一个临时表T-SQL中更新2个值

[英]Update 2 values in one temporary table T-SQL

I would like your help for a query. 希望您的查询有所帮助。 I have a temporary table with value(float), attribute(nvarchar) and system(int). 我有一个临时表,其中包含value(float),attribute(nvarchar)和system(int)。 I need to update the values depending the attribute, but using the system. 我需要根据属性更新值,但要使用系统。 So... 所以...

UPDATE #inventory (value, attribute)
SET (value, attribute) = (SELECT SUM(value), 'Actual'
                        FROM ReportValue v, ReportValueType t, ReportProducts ti
                        WHERE v.type_id = t.id
                        AND v.voyage_id = ti.id
                        AND t.value_code = 'total'
                        AND t.category_code = 'cold'
                        AND ti.end_time BETWEEN @start AND @end)
UPDATE #inventory (value, attribute)
SET (value, attribute) = (SELECT SUM(value), 'Actual'
                        FROM ReportValue v, ReportType t, Reportprod ti
                        WHERE v.type_id = t.id
                        AND v.voyage_id = ti.id
                        AND t.field_name = 'Total'
                        AND t.slot_type = 'COLD'
                        AND t.xml_id = -2000
                        AND ti.end_time BETWEEN @start AND @end)
else (value, attribute)
end

This is the wrong form. 这是错误的形式。 How can I get it working? 我该如何运作? Thank you in advance for your answers! 预先感谢您的回答!

OK so it semms you are trying too get your head around update syntax... (that is compliant): 好的,这样您就可以尝试着更新语法...(兼容):

UPDATE tblNameCanBeTempTable
SET Column1 = S.Value1, Column2 = S.Value2, etc
FROM (anyValidSelectTableStatement) S
    WHERE tblNameCanBeTempTable.KeyValue =  S.keyValue

So the route is make your select statment, check it produces the values you want, then tack the update ont the beginning and the row matching WHERE on the end 因此,路由是使您选择语句,检查它是否产生所需的值,然后在开始处添加更新,并在末尾匹配WHERE的行

You Can do things like 你可以做类似的事情

Col1 = CASE WHEN X=1 THEN S.Value1 ELSE Col1 END

for each row. 每行。

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

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