回答

收藏

如何从临时表中获取最近插入的列的值?

技术问答 技术问答 235 人阅读 | 0 人回复 | 2023-09-12

我无法在Select语句中使用表的最近插入的Quantity值#ACCT来像
3 a' K% ]+ L) j2 k3 Jser.ServiceRate * Quantity- `$ L! ^, q& i7 x+ B% P
每次我得到错误
$ h/ N% E: U; d! e4 r! L/ A8 I
; B. J* a% s/ W  Y' F1 p5 g: t无法将值NULL插入“金额”列中
) h) [2 v5 R9 Q% P

8 U' V( |2 k' l我只是一个初学者,所以解决这个问题的任何指示都将有所帮助。: E& g# ]  n4 B; s
DECLARE @MinReservationId INT = (SELECT MIN(f.ReservationId) FROM dbo.Reservation AS f)
% U# a- h8 r! `; U  |2 YDECLARE @MaxReservationId INT = (SELECT MAX(f.ReservationId) FROM dbo.Reservation AS f)9 _& n* p1 }! s" A
DECLARE @QuantityNew INT  C- A$ |, F5 C3 d+ F
WHILE @MinReservationId  I WANT THIS VALUE TO BE USED BELOW
0 e1 G  t6 n# I: A9 a    )
: ^1 g+ C6 A. z% W9 d    INSERT INTO dbo.[Transaction]
8 A4 @/ x6 X+ Q& `. p* ?4 ^7 i(
+ s5 ]7 o% @% f% A! y& L# q9 c    ReservationId,/ [1 ^. G% E! C6 L8 g7 B% x3 W, |
    ServiceId,( [; P2 \9 Q3 [& O- ]
    Rate,# a  U# p! i9 A' a# `7 ^$ L5 W
    Quantity,0 r7 R, o: X8 u' {; {
    Amount      
0 v8 v8 [9 w5 S1 f)   
! Y4 {+ q# t( ?3 J5 S( ZOUTPUT inserted.ServiceId,Inserted.Quantity! ~* r0 ?- R/ f( t6 p! F/ @* t
INTO #Acct# B- Y6 h: _' Q+ x
(, }+ W7 }4 y) M, q1 I+ y
    ServiceId,+ o$ b6 M6 h' j( G, i
    Quantity- p0 U+ N3 b  I4 v" k
)1 @( g6 O+ Q7 N
SELECT
( i( z* y- R! i    @MinReservationId,
# f. }1 S$ E7 k    ser.ServiceId,
$ x% u! @# `6 |9 x, s    ser.ServiceRate,
* U. j3 |1 x! Z, o8 L' g    ABS(CHECKSUM(NEWID())%3) + 1,
: q5 D" k$ s- R# e7 ^    ser.ServiceRate * (SELECT acc.Quantity from #Acct as acc) -> QUANTITY from #ACCT should be used here
7 y2 i' v8 G& O9 D& ~# YFROM dbo.[Service] AS ser1 v9 U7 n5 k" `" v, `
SELECT @MinReservationId=@MinReservationId+1
; S0 ^  ~" L4 c, X+ SDrop table #Acct" Q  P/ P3 F% S7 w5 E, M# y
END, D: `( H6 A# h- d* E5 w& t& ?& @
                / K5 {8 c3 n! h9 P0 f( m+ L
解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则