|
我在一个表中有触发器,并且想UserId在插入,更新或删除行时读取值。怎么做?下面的代码不起作用,出现错误UPDATED2 F) n( N5 o6 V3 h% R
ALTER TRIGGER [dbo].[UpdateUserCreditsLeft] + A( }6 ~& h$ I0 w* h( U! u, b, R" r
ON [dbo].[Order]
; h- }$ V O% G F: w! Y AFTER INSERT,UPDATE,DELETE
) A6 R. V1 X7 \+ Q0 sAS 1 F' n) [% ~, p9 ]7 J: `
BEGIN
" j4 S" g: A. r% G! u; V -- SET NOCOUNT ON added to prevent extra result sets from
9 j6 U+ L& Q+ L" b -- interfering with SELECT statements.
7 W' c7 R- ~& z8 w2 X9 y SET NOCOUNT ON;
- ?0 I& F# P( B: K% n1 ^( _ DECLARE ) C( O h1 `, T+ B i
@UserId INT,
6 w7 B1 N& Z; T0 c7 ? n# ^: a, F SELECT @UserId = INSERTED.UserId FROM INSERTED, DELETED! |3 R8 `% |3 f+ T2 ^! P
UPDATE dbo.[User] SET CreditsLeft = CreditsLeft - 1 WHERE Id = @UserId
! Q9 c% I i0 Q" r) ~ WEND
+ k7 Q: Y# x2 d$ \$ y! H
- o e {. m1 c4 O% l$ N- F0 ~" `解决方案:" s/ z. p8 H0 ? W
3 N$ X; d+ ]& L \9 S* U8 ]
e3 D+ w8 l4 O4 E0 i5 W- x; w P N2 T _
请注意,其inserted, deleted含义与相同,inserted CROSS JOIN
2 n. _: f+ w, ? v/ |6 ]deleted并给出每一行的每种组合。我怀疑这就是你想要的。
?- t2 h6 R5 D2 i) H0 z. ~0 Y这样的事情可能会帮助您入门…) `5 P9 \) R/ z1 r" M1 ]
SELECT1 P8 `% X" Y% P' V, O
CASE WHEN inserted.primaryKey IS NULL THEN 'This is a delete'
* _9 I6 a$ u; ?% ^( K1 F WHEN deleted.primaryKey IS NULL THEN 'This is an insert'
3 b2 E5 [( f' O7 r7 L ELSE 'This is an update'
0 b1 k N5 [5 E) ^0 o! G2 C: b0 A END as Action,
/ M8 T8 S) y4 H0 O *
8 u' F) N% ~+ g; {FROM8 K% r( O1 H5 |% \3 H3 Y
inserted
3 {$ }8 R6 ~' h1 yFULL OUTER JOIN6 H0 W7 {8 z; g8 A: q7 }
deleted) f) o/ Y! P, g( ]1 {/ H! g* [- x
ON inserted.primaryKey = deleted.primaryKey7 [2 I7 A `$ U: j% W; y4 h
然后根据您要执行的操作,使用inserted.userID或引用您感兴趣的表deleted.userID,等等。1 y+ K; B& f9 R8 z' u0 r
最后,请注意inserted和deleted是 表, 并且可以(并且确实)包含多个记录。
9 h- k" ?, k2 W3 q/ }+ u1 L如果您一次插入10条记录,该inserted表将包含所有10条记录。删除和deleted表也??是如此。并且两个表都在更新的情况下。
; D7 b* f4 \9 u1 f编辑 示例OP编辑后触发。
3 ] z: K$ i9 k1 p2 s" s2 I) LALTER TRIGGER [dbo].[UpdateUserCreditsLeft] % I+ l# E5 i _8 Y
ON [dbo].[Order]
) t" n( I* g" G5 ~ AFTER INSERT,UPDATE,DELETE
- L1 c9 _! X, d# nAS 6 O0 a" o- h, \( S8 V
BEGIN# l& r1 B3 ^$ g$ x' {" d$ O& I6 E+ Q( D/ w
-- SET NOCOUNT ON added to prevent extra result sets from# z# X) o. |/ v0 p6 L
-- interfering with SELECT statements.
, B2 K. O( W9 H' W0 J% k SET NOCOUNT ON;9 A9 [1 O. H; j5 q
UPDATE
Z; y: a3 Q( R6 W1 `, `5 ? User" w0 X- k# O' J
SET: L5 U9 Y" p; E' f2 Q/ y
CreditsLeft = CASE WHEN inserted.UserID IS NULL THEN
) x) L& }/ P! ~4 L8 u9 l8 ] WHEN deleted.UserID IS NULL THEN
) ]0 r+ P$ l% m( V( E ELSE 4 @& ]2 k; c B9 J- e% d0 a0 _' s
END
4 z% J5 l, { X+ c3 \* ~" c% c FROM, M v1 v( Z2 \/ K
User
: G* i9 N" j# N% w INNER JOIN
# U" G7 h5 c- _* j5 W ($ c% G4 x# @, H( R. Q- g$ h- c% J
inserted
8 m, a. A- z, I3 G8 W FULL OUTER JOIN! @. }# j* W# p* g5 v1 c$ o) i& i
deleted7 W P7 N D3 U9 A
ON inserted.UserID = deleted.UserID -- This assumes UserID is the PK on UpdateUserCreditsLeft
' q4 e- _/ V+ h# H W# E )
3 d; N; o( U1 H0 A5 U' z/ |# } ON User.UserID = COALESCE(inserted.UserID, deleted.UserID)" e& |8 ~+ ]4 J3 l. R$ `4 l# J) ?
END2 c7 J/ y ~3 F+ F6 c% [! V3 M7 W
如果的PrimaryKeyUpdateUserCreditsLeft不是UserID,则在FULL OUTER JOIN中使用它。 |
|