[英]How to multiple update lines using values from the same register?
I have the following scenario:我有以下情况:
TOS服务条款
codigo![]() |
U_TIPO_REFERENCIA ![]() |
U_PICKING_3 ![]() |
---|---|---|
1 ![]() |
PK ![]() |
1234 ![]() |
2 ![]() |
PK ![]() |
5678 ![]() |
TOS_AUX TOS_AUX
codigo![]() |
Cod_OS![]() |
Subtitulo![]() |
U_TIPO_DOC_REFERENCIA ![]() |
U_PICKING_3 ![]() |
---|---|---|---|---|
1 ![]() |
1 ![]() |
Description test 1![]() |
||
2 ![]() |
1 ![]() |
Description test 2![]() |
||
3 ![]() |
1 ![]() |
Description test 3![]() |
||
4 ![]() |
2 ![]() |
Description test 4![]() |
||
5 ![]() |
2 ![]() |
Description test 5![]() |
||
6 ![]() |
2 ![]() |
Description test 6![]() |
I want to place a trigger on Table 1, that updates table 02 Description with a concat:我想在表 1 上放置一个触发器,用 concat 更新表 02 描述:
table 1. field 1, ' - ',table1.field2,' - ',table2.description表1.字段1,'-',table1.field2,'-',table2.description
My problem is how to "mention" table 2 "same line" in the concat我的问题是如何在 concat 中“提及”表 2“同一行”
Here is what I have:这是我所拥有的:
ALTER TRIGGER [dbo].[atualizaPKnoUpdate]
ON [dbo].[TOS]
AFTER update
AS
BEGIN
SET NOCOUNT ON;
declare
@Servico int,
@PK float,
@TipoPK varchar(200)
select
@servico = codigo,
@pk = U_PICKING_3,
@TipoPK = U_TIPO_REFERENCIA
FROM inserted
UPDATE DBO.TOS_AUX
SET U_PICKING_3 = @PK,
U_TIPO_DOC_REFERENCIA = @TipoPK
WHERE
TOS_AUX.COD_OS=@Servico
AND TOS_AUX.U_PICKING_3 IS NULL
UPDATE DBO.TOS_AUX
SET
subtitulo = CONCAT(@TipoPK,'-',@PK,' - ',--this part is missing--)
WHERE
TOS_AUX.COD_OS=@Servico
END
So, for因此对于
UPDATE TOS
SET U_TIPO_REFERENCIA = 'PV'
WHERE CODIGO = 1
I expect to have:我希望有:
TOS服务条款
codigo![]() |
U_TIPO_REFERENCIA ![]() |
U_PICKING_3 ![]() |
---|---|---|
1 ![]() |
PV![]() |
1234 ![]() |
2 ![]() |
PK ![]() |
5678 ![]() |
TOS_AUX TOS_AUX
codigo![]() |
Cod_OS![]() |
Subtitulo![]() |
U_TIPO_DOC_REFERENCIA ![]() |
U_PICKING_3 ![]() |
---|---|---|---|---|
1 ![]() |
1 ![]() |
PV - 1234 - Description test 1 ![]() |
PV![]() |
1234 ![]() |
2 ![]() |
1 ![]() |
PV - 1234 - Description test 2 ![]() |
PV![]() |
1234 ![]() |
3 ![]() |
1 ![]() |
PV - 1234 - Description test 3 ![]() |
PV![]() |
1234 ![]() |
4 ![]() |
2 ![]() |
Description test 4![]() |
||
5 ![]() |
2 ![]() |
Description test 5![]() |
||
6 ![]() |
2 ![]() |
Description test 6![]() |
Thanks in advance.提前致谢。
Ignoring whether or not your approach is correct for the moment, I believe you just need a JOIN
ed UPDATE
eg something similar to the following where you join the Inserted
pseudo-table onto the table you want to update and then conditionally update as required.忽略你的方法目前是否正确,我相信你只需要一个
JOIN
ed UPDATE
例如类似于下面的东西,你将Inserted
伪表连接到你想要更新的表上,然后根据需要有条件地更新。 I have done the best I can to match your existing logic, but you'll have to test it.我已尽我所能来匹配您现有的逻辑,但您必须对其进行测试。 Note this also solves your issue of handling multiple rows being updated at the same time.
请注意,这也解决了您处理同时更新的多行的问题。
UPDATE AUX SET
U_PICKING_3 = CASE WHEN AUX.U_PICKING_3 IS NULL THEN I.U_PICKING_3 ELSE AUX.U_PICKING_3 END
, U_TIPO_DOC_REFERENCIA = CASE WHEN AUX.U_PICKING_3 IS NULL THEN I.U_TIPO_REFERENCIA ELSE AUX.U_TIPO_REFERENCIA END
, Subtitulo = CONCAT(I.U_TIPO_REFERENCIA, '-', U_PICKING_3, ' - ', AUX.Subtitulo)
FROM DBO.TOS_AUX AUX
INNER JOIN Inserted I ON I.codigo = TOS.COD_OS;
However for the sake of completeness, it would be remiss of me not to mention that this is most likely a bad approach.然而,为了完整起见,如果我不提这很可能是一个糟糕的方法,那将是我的疏忽。 One should avoid storing the same data in multiple locations at all costs because it leads to all sorts of issues that are easily avoided by maintaining the data in a single location and querying it as required.
人们应该不惜一切代价避免将相同的数据存储在多个位置,因为这会导致各种问题,而这些问题可以通过将数据保存在一个位置并根据需要进行查询来轻松避免。
ALTER TRIGGER [dbo].[atualizapknoUpdatetos]
ON [dbo].[TOS]
AFTER update
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for trigger here
UPDATE AUX SET
U_PICKING_3 = CASE WHEN AUX.U_PICKING_3 IS NULL THEN I.U_PICKING_3 ELSE AUX.U_PICKING_3 END
, U_TIPO_DOC_REFERENCIA = CASE WHEN AUX.U_PICKING_3 IS NULL THEN I.U_TIPO_REFERENCIA ELSE AUX.U_TIPO_DOC_REFERENCIA END
, Subtitulo = case
when aux.SUBTITULO like 'PK%' then aux.SUBTITULO
when aux.SUBTITULO like 'PVK%' then aux.SUBTITULO
else CONCAT( CASE WHEN AUX.U_PICKING_3 IS NULL THEN I.U_TIPO_REFERENCIA ELSE AUX.U_TIPO_DOC_REFERENCIA END, '-', CASE WHEN AUX.U_PICKING_3 IS NULL THEN I.U_PICKING_3 ELSE AUX.U_PICKING_3 END, ' - ', AUX.Subtitulo)
end
FROM DBO.TOS_AUX AUX
INNER JOIN Inserted I ON I.codigo = AUX.COD_OS;
END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.