回答

收藏

在表中插入数据时获取表的行数

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

这是示例数据和模式
0 I2 S# L: d; g' O: q3 wCREATE TABLE [dbo].[Customer]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](500) NOT NULL, [IsDelete] [bit] NOT NULL,
3 X6 H/ M# z4 vCONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
5 @  `2 N4 D& D8 y( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]. M7 l5 p, s, |
GO/ T5 V; ~* h' I$ Z. ~% L+ B8 R
CREATE TABLE [dbo].[Invoice]([Id] [int] IDENTITY(1,1) NOT NULL,[CustomerId] [int] NOT NULL, [Amount] [decimal](18, 2) NOT NULL,( j4 x2 @% U) o+ n1 Y7 }" ?. Y/ @$ U
CONSTRAINT [PK_Invoice] PRIMARY KEY CLUSTERED
2 _2 J: V2 s: {4 F- v( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY]8 o' y- [7 Z7 E, U2 K" V, g0 D+ P3 ?
GO
( e2 T, W" O. n% I0 G" {Insert Into Customer (Name)
* U0 D7 ?0 n* x# y* Q4 [  d0 JValues  O. x& K+ g* I. f, \9 t1 _6 e
('Jhon'),('-Jack'),('Mary'),('-Sam');9 N3 q9 h3 d: d; d! J
Insert into Invoice (CustomerId,Amount)  b$ }- [; _, ]7 o- j. K
Values (1, 22.00),(1, 34.50),(2,20.67),(2,34.67),(3,45.89),(3,20.00),(4,30.00),(4,20.00)1 Y' M: D/ l; ?7 ^( k( |8 @4 L6 d; O
ALTER TABLE [dbo].[Customer] ADD  CONSTRAINT [DF_Customer_IsDelete]  DEFAULT ((0)) FOR [IsDelete]
0 t" ?' l; I1 x7 O% B: }" n; r; ?GO- l4 C- W) N5 [. s
ALTER TABLE [dbo].[Invoice]  WITH CHECK ADD  CONSTRAINT [FK_Invoice_Invoice] FOREIGN KEY([CustomerId])' {: j' P. k8 z# q6 M
REFERENCES [dbo].[Customer] ([Id])+ @7 U& {: |) k5 v7 c
GO- w% V7 ^4 d! J# q: q  y) [4 ~: a9 T
ALTER TABLE [dbo].[Invoice] CHECK CONSTRAINT [FK_Invoice_Invoice]
$ A" b& x9 a' }1 S9 X; RGO
" ~2 R  I* W* D- [我有以下SQL查询# B6 @9 A1 b: ]3 q5 s
DECLARE @invoiceTable TABLE () C3 F6 Q( N6 Q$ c1 h% l- Q. ~
Id INT NOT NULL,
) m8 y$ _; D1 A+ u6 L1 v2 g4 [CustName NVARCHAR(MAX) NOT NULL,( L5 B& u* ]. o: l1 u+ M
FilteredRecords INT DEFAULT 0 NOT NULL
: k2 {- c8 V6 o0 H3 Y)' K' C3 F* C* P! Y
Insert into @invoiceTable(( q7 Q* `: @& G1 K8 G5 l6 D; j, i
Id,! V- S3 p+ \2 f& r
CustName,
# E3 v  d: m  ]FilteredRecords -- ****Here I need help****' x# w1 [* J; Q" @
)2 x' H' D+ n2 b. [- D( g! ]
Select " ^9 v7 q+ t' [7 @* x7 F
I.Id,! x& |! g( h3 ^( Q' F
R.Name,; S3 m: F3 y/ F" s/ k
(Select Count(Id) from @invoiceTable)  c7 Q1 a6 d( [. U( m% p
from Invoice I Inner Join Customer R on I.CustomerId = R.Id6 Q5 m. e% a3 X( D. G9 F+ d- D0 Y: y
UPDATE @invoiceTable SET CustName= 'Anonymous' WHERE CustName Like'%-%'
. E8 b+ B! ^  \  y8 t# RSelect * from @invoiceTable
4 p9 o+ ~1 c0 O& bwhere Id In (1,4)
: X. M1 {" X2 z+ F0 d! @现在我正在关注明显的结果。
: |- L( B3 @) U+ nId       CustName   FilteredCount  
- `9 _% X. u! U- U4 L7 T$ e-------- ---------- ----------------
. r4 i  M+ W7 F( d! h1        John       0- C) G3 E3 Z5 b6 r! J
4        Anonymous  0+ m  V  ~( S) d( c, P4 t) d2 N4 h
但是相反,我需要FilteredCount列中@invoiceTable的总行数。, @: `, B9 B/ z5 b
预期结果是
/ i3 Q% H  o0 L/ EId       CustName   FilteredCount  
$ @% r( k, R1 H- C' ]7 y! }9 ~-------- ---------- ----------------
# }) u( S& @' J7 m- x: Y1        John       21 V1 h! d! a. o2 S0 |
4        Anonymous  2: \- r! E. A. r* [& d
因为我在@invoiceTable中只有两行,所以我希望2在FilteredCount列中( B; s5 @. f' ], ^0 A
有什么方法可以实现这一目标,请帮帮我。0 i& S- v5 d5 m5 C
                5 `9 A* n/ M0 v' H$ D! j. s
解决方案:
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则