Get row count of table while inserting data in table

こ雲淡風輕ζ 提交于 2021-02-10 18:02:37

问题


Here is Sample Data and Schemas

CREATE TABLE [dbo].[Customer]( [Id] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](500) NOT NULL, [IsDelete] [bit] NOT NULL, 
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED 
( [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]
GO
CREATE TABLE [dbo].[Invoice]([Id] [int] IDENTITY(1,1) NOT NULL,[CustomerId] [int] NOT NULL, [Amount] [decimal](18, 2) NOT NULL,
 CONSTRAINT [PK_Invoice] PRIMARY KEY CLUSTERED 
( [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]
GO

Insert Into Customer (Name)
Values
('Jhon'),('-Jack'),('Mary'),('-Sam');

Insert into Invoice (CustomerId,Amount)
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)

ALTER TABLE [dbo].[Customer] ADD  CONSTRAINT [DF_Customer_IsDelete]  DEFAULT ((0)) FOR [IsDelete]
GO
ALTER TABLE [dbo].[Invoice]  WITH CHECK ADD  CONSTRAINT [FK_Invoice_Invoice] FOREIGN KEY([CustomerId])
REFERENCES [dbo].[Customer] ([Id])
GO
ALTER TABLE [dbo].[Invoice] CHECK CONSTRAINT [FK_Invoice_Invoice]
GO

I have following SQL Query

DECLARE @invoiceTable TABLE (
Id INT NOT NULL,
CustName NVARCHAR(MAX) NOT NULL,
FilteredRecords INT DEFAULT 0 NOT NULL
)

Insert into @invoiceTable(
Id,
CustName,
FilteredRecords -- ****Here I need help****
)
Select 
I.Id,
R.Name,
(Select Count(Id) from @invoiceTable)
from Invoice I Inner Join Customer R on I.CustomerId = R.Id
UPDATE @invoiceTable SET CustName= 'Anonymous' WHERE CustName Like'%-%'
Select * from @invoiceTable
where Id In (1,4)

Now I am getting following Obvious result.

Id       CustName   FilteredCount  
-------- ---------- ----------------
1        John       0
4        Anonymous  0

But Instead of it I want the Total Rows of @invoiceTable in FilteredCount Column.

The Expected Result is

Id       CustName   FilteredCount  
-------- ---------- ----------------
1        John       2
4        Anonymous  2

As I have only two rows in @invoiceTable I want 2 in the FilteredCount Column

Is there any way to achieve this, Help me with this.


回答1:


If you are only interested in Id 1 and 4, you can filter it in the INSERT query

As for the Anonymous, you can use a case statement to do it

window function count(*) over() will gives you total rows in the query result

Insert into @invoiceTable ( Id, CustName, FilteredRecords )
Select  Id = I.Id,
        CustName = CASE WHEN R.Name Like '%-%' then 'Anonymous' else R.Name end,
        FilteredRecords = count(*) over()
from   Invoice I 
       Inner Join Customer R on I.CustomerId = R.Id
where  I.Id In (1,4)



回答2:


It's not clear if you want the count of the whole table or only the final select, I've gone for the latter.

You don't actually need a temporary table here at all:

Select 
    I.Id,
    Name = CASE WHEN CustName Like '%-%' THEN 'Anonymous' ELSE R.Name END,
    FilteredRecords = Count(*) over ()
from Invoice I
Inner Join Customer R on I.CustomerId = R.Id
where Id In (1,4);


来源:https://stackoverflow.com/questions/65770534/get-row-count-of-table-while-inserting-data-in-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!