我在mssql服务器上有一个删除工作。但我希望在删除之前更新另一个表。 我有两个表通过“user_id”列连接,我的表名称是“UserInfo”和“AdvertInfo”,它们与user_id列连接。我在7天后删除giving_advert_day。但是当它被删除时,ı想增加(+1)user_advert_number列。如果在user_id中删除,从表中删除3或4等数据,则来自它们的连接数据将增加+ 3,+ 4或蚀刻。有办法吗? 这是我的删除命令
Delete from AdvertInfo where advert_id In (
select advert_id from AdvertInfo
where advert_begin_date < DATEADD(day, -7, GETDATE()))
答案 0 :(得分:2)
您可UPDATE
使用JOIN
:
UPDATE u
SET u.user_advert_number = u.user_advert_number + 1
FROM UserInfo as u
INNER JOIN AdvertInfo AS a ON u.user_id = a.user_id
WHERE a.advert_begin_date < DATEADD(day, -7, GETDATE()))
BTW,您的删除子句可以写成如下:
Delete
from AdvertInfo
where advert_begin_date < DATEADD(day, -7, GETDATE()))
不需要IN
谓词。
您还可以创建create a trigger,以执行after delete
,例如以您希望的方式更新列。
答案 1 :(得分:1)
使用以下查询来实现您想要的目标:
DECLARE @Temp TABLE (
[user_id] INT
)
DELETE i
OUTPUT deleted.[user_id] INTO @Temp
FROM AdvertInfo i
WHERE advert_begin_date < DATEADD(day, -7, GETDATE())
UPDATE u
SET u.user_advert_number = u.user_advert_number + d.cnt
FROM UserInfo as u
INNER JOIN (
SELECT [user_id], COUNT([user_id]) AS cnt
FROM @Temp
GROUP BY [user_id]
) d ON d.[user_id] = u.[user_id]
答案 2 :(得分:1)
Merhaba Kaya, 也许你可以使用SQL OUTPUT clause如下 您可能已经知道,在执行DELETE命令时,您可以将带有OUTPUT子句的受影响数据放入变量表中,如下面的示例所示,您可以在以后使用它
SELECT FOLIO_EXP,
FOLIO,
DOC,
REFERENCIA,
TARJETA,
CUENTA,
NOMBRE,
IMPORTE,
IMP_REC,
IMP_REPRE,
IMP_QUEB,
FECHA_PAGO,
MCC,
COMERCIO,
PCC,
FECHA_COM,
ADQUIRENTE,
ESTADO,
"TIPO DE FRAUDE",
F_FRAUDE,
F_RECLAMO,
F_REEMBOLSO,
DICAMINO
FROM
(SELECT E.FOLIO_EXP,
A.FOLIO,
'D' AS DOC,
A.REFERENCIA,
A.TARJETA,
A.ACCT_NUM AS CUENTA,
B.NOMBRE_TH AS NOMBRE,
TO_CHAR(A.MONTO,'999,999.99') IMPORTE,
D.IMP_REC,
F.IMP_REPRE,
DECODE((A.MONTO -D.IMP_REC),NULL,A.MONTO,(A.MONTO -D.IMP_REC)) AS IMP_QUEB,
D.FECHA_PAGO,
A.SIC_CDE AS MCC,
A.COMERCIO,
C.PCC,
C.FECHA_COM,
C.ADQUIRENTE,
C.ESTADO,
'INTERNET' AS "TIPO DE FRAUDE",
A.FECHA_TRANS AS F_FRAUDE,
A.FECHA_RECLAMO AS F_RECLAMO,
A.FECHA_BONIFICACION AS F_REEMBOLSO,
A.USUARIO AS DICAMINO
FROM OPPF.T00EMISOR A
LEFT JOIN
(SELECT A.FECHA,
A.TARJETA,
C.PCC,
C.FECHA_COM,
C.ADQUIRENTE,
C.ESTADO
FROM
(SELECT TARJETA,
MAX(FECHA_INFO) FECHA
FROM OPPF.T00_PCC
GROUP BY TARJETA) A
LEFT JOIN
(SELECT FECHA_INFO,
TARJETA,
PCC,
FECHA_COM,
ADQUIRENTE,
ESTADO
FROM OPPF.T00_PCC)C ON(A.TARJETA = C.TARJETA
AND A.FECHA = C.FECHA_INFO)) C ON(TO_CHAR(C.TARJETA) = TO_CHAR(A.TARJETA))
LEFT JOIN
(SELECT MAX(SE_REVISO) SE_REVISO,
NOMBRE_TH,
TARJETA
FROM OPPF.T00CASOSRESUELTOS
GROUP BY TARJETA,
NOMBRE_TH) B ON(SUBSTR(A.TARJETA,1,16) = SUBSTR(B.TARJETA,1,16)
AND A.FECHA_BONIFICACION = B.SE_REVISO)
LEFT JOIN
(SELECT MAX(FECHA_PAGO) FECHA_PAGO,
TARJETA,
REFERENCIA,
COUNT(REFERENCIA) N,
SUM(MONTO_LIQ) AS IMP_REC--, FECHA_TRXS
FROM OPPF.T00_CC_RECUP
GROUP BY TARJETA,
REFERENCIA,
FECHA_TRXS) D ON(TO_CHAR(D.TARJETA) = TO_CHAR(A.TARJETA)
AND D.REFERENCIA = A.REFERENCIA)
LEFT JOIN
(SELECT TARJETA,
COUNT(TARJETA) TRANS,
COUNT(REFERENCIA) NR,
REFERENCIA,
SUM(IMPORTE) IMP_REPRE
FROM OPPF.T00_REPRESENTACIONES
GROUP BY TARJETA,
REFERENCIA) F ON(A.TARJETA = F.TARJETA
AND LPAD(TO_CHAR(A.REFERENCIA),23,'0') = LPAD(TO_CHAR(F.REFERENCIA),23,'0'))
LEFT JOIN
(SELECT MIN(FOLIO) FOLIO_EXP,
TARJETA,
FECHA_BONIFICACION
FROM OPPF.T00EMISOR --WHERE FECHA_BONIFICACION BETWEEN '02/09/2015' AND '31/12/2015'
GROUP BY FECHA_BONIFICACION,
TARJETA
ORDER BY FECHA_BONIFICACION,
TARJETA,
FOLIO_EXP) E ON(E.TARJETA = A.TARJETA
AND E.FECHA_BONIFICACION = A.FECHA_BONIFICACION)
WHERE A.ENTRY_MODE IN (' ',
'1',
'01',
'001',
'0',
'00',
'012',
'010',
'12',
'10',
'11',
'011'))
WHERE F_REEMBOLSO = '04/07/2017'
ORDER BY FOLIO,
REFERENCIA