我有以下更新声明:
UPDATE D
SET D.Xtra1 = CASE
when ExtendedProperties.PropertyName="Transmittal Number" then ExtendedProperties.PropertyValue
else NULL
END,
D.Xtra2 = CASE
when (ExtendedProperties.PropertyName="File Identification") then ExtendedProperties.PropertyValue
else NULL
END,
D.Xtra3 = CASE
when (ExtendedProperties.PropertyName="File Owner") then ExtendedProperties.PropertyValue
else NULL
END,
D.Xtra4 = case
when (ExtendedProperties.PropertyName="Dept File Cabinet") then ExtendedProperties.PropertyValue
else NULL
END ,
D.Xtra5 = case
when (ExtendedProperties.PropertyName="Record Storage Box Number") then ExtendedProperties.PropertyValue
else NULL
END ,
D.Xtra6 = case
when (ExtendedProperties.PropertyName="ImportCode") then extendedProperties.PropertyValue
else NULL
END ,
D.Xtra7 = case
when (ExtendedProperties.PropertyName="ImageKey") then extendedProperties.PropertyValue
else NULL
END ,
D.Xtra8 = case
when (ExtendedProperties.PropertyName="Boundary") then ExtendedProperties.PropertyValue
else NULL
END ,
D.Xtra9 = case
when (ExtendedProperties.PropertyName="Offset") then ExtendedProperties.PropertyValue
else NULL
END ,
D.Xtra10 = case
when (ExtendedProperties.PropertyName="UniqueFilename") then ExtendedProperties.PropertyValue
else NULL
END
来自ExtendedProperties内连接数据D的在D.ItemID = ExtendedProperties.ItemID
上更新无效,我还没弄清楚原因。
如果我执行单个直接更新语句,它允许我写入该字段。 如果我只选择其中一个case语句,它会正确绘制字段。
任何想法?
答案 0 :(得分:2)
您的问题是您在此处转置表格。源表具有项目的 N 行(ItemId,PropertyName和PropertyValue),但目标表有一行,其中 N 列(Xtra1 ... Xtra10)。
左连接可用于将源从行转置为列,并且所有值可以同时使用,而不是按顺序。
UPDATE
data
SET
data.Xtra1 = ep01.PropertyValue,
data.Xtra2 = ep02.PropertyValue,
data.Xtra3 = ep03.PropertyValue,
data.Xtra4 = ep04.PropertyValue,
data.Xtra5 = ep05.PropertyValue,
data.Xtra6 = ep06.PropertyValue,
data.Xtra7 = ep07.PropertyValue,
data.Xtra8 = ep09.PropertyValue,
data.Xtra9 = ep09.PropertyValue,
data.Xtra10 = ep10.PropertyValue
FROM
data
LEFT JOIN ExtendedProperties ep01 ON data.ItemID = ep01.ItemID AND ep01.PropertyName = 'Transmittal Number'
LEFT JOIN ExtendedProperties ep02 ON data.ItemID = ep02.ItemID AND ep02.PropertyName = 'File Identification'
LEFT JOIN ExtendedProperties ep03 ON data.ItemID = ep03.ItemID AND ep03.PropertyName = 'File Owner'
LEFT JOIN ExtendedProperties ep04 ON data.ItemID = ep04.ItemID AND ep04.PropertyName = 'Dept File Cabinet'
LEFT JOIN ExtendedProperties ep05 ON data.ItemID = ep05.ItemID AND ep05.PropertyName = 'Record Storage Box Number'
LEFT JOIN ExtendedProperties ep06 ON data.ItemID = ep06.ItemID AND ep06.PropertyName = 'ImportCode'
LEFT JOIN ExtendedProperties ep07 ON data.ItemID = ep07.ItemID AND ep07.PropertyName = 'ImageKey'
LEFT JOIN ExtendedProperties ep08 ON data.ItemID = ep08.ItemID AND ep08.PropertyName = 'Boundary'
LEFT JOIN ExtendedProperties ep09 ON data.ItemID = ep09.ItemID AND ep09.PropertyName = 'Offset'
LEFT JOIN ExtendedProperties ep10 ON data.ItemID = ep10.ItemID AND ep10.PropertyName = 'UniqueFilename'
如果你有很多记录,就有一个超过(ItemID, PropertyName)
的联合收购指数,以便顺利运作。
编辑:或者,你可以这样做:
UPDATE
data
SET
data.Xtra1 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'Transmittal Number'),
data.Xtra2 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'File Identification'),
data.Xtra3 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'File Owner'),
data.Xtra4 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'Dept File Cabinet'),
data.Xtra5 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'Record Storage Box Number'),
data.Xtra6 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'ImportCode'),
data.Xtra7 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'ImageKey'),
data.Xtra8 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'Boundary'),
data.Xtra9 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'Offset'),
data.Xtra10 = (SELECT PropertyValue FROM ExtendedProperties WHERE ItemID = data.ItemID AND PropertyName = 'UniqueFilename')
代码少,可能更容易理解,也许不那么快(这是猜测)。
答案 1 :(得分:0)
您需要在FROM子句中包含data
表
from data
join ExtendedProperties on data.ItemID = ExtendedProperties.ItemID