gpt4 book ai didi

sql - 为什么对 @@rowcount 的两次调用会返回两个不同的值?

转载 作者:行者123 更新时间:2023-12-02 21:30:12 27 4
gpt4 key购买 nike

我有这个存储过程来保存两个表 ACDPrograms 和 DefaultCourses 中的值。这是我的全局参数

 create proc USPSvUpdtACDProgramFromTypes
@pPRGCode char(10),
@pPRGName varchar(100),
@pPRGDescr varchar(max),
@pFCTID int,
@pMinCredits int,
@pNZQA int,
@pNoOccrPerYear int,
@pFirstMonthOccr int,
@pSecondMonthOccr int,
@pDOMFee decimal(16,2),
@pINTLFee decimal(16,2),
@pIsAval int,
@pEmpID int,
@pSelCRSInfo as dbo.SelectedCourses READONLY,
@pPRGID int output
as
begin

如您所见,用户定义的表类型 SelectedCourses 具有以下结构

create type dbo.SelectedCourses
as table
(
CntrNo int not null IDENTITY(1, 1),
CRSID int,
YearNo int,
IsCompulsory int,
EntDT datetime,
IsAval int,
EmpID int
);
go

在我的存储过程中存在事务,因为它在 defualtCourses 表中存储了两行四行数据,其中一行存储在 ACDPrograms 中。我的存储过程没有 try/catch block 。当迭代循环来存储四行时,迭代的工作方式就像一个魅力,但在循环外的最后一次迭代之后,当我尝试访问 @@rowcount 时,当我将其调用为常规 SQL 语句(无循环)的两倍时

select 'row count' + convert(char(1), @@rowcount) -- This line prints 0 --> INCORRECT
select 'row count' + convert(char(1), @@rowcount) -- this line prints 1 --> CORRECT BUT WHY NOW

第一次调用它,打印 0 作为值,这不是真正的最坏情况,在第二次调用时,它打印应该在第一个调用中打印的值,即 1。为什么它会这样,为什么它在时打印两个值它被调用了两次,难道不应该在百万次调用之后打印相同的值事件吗?

这是循环部分及其周围:

    if @@ROWCOUNT > 0
begin
set @newPRGID = @@IDENTITY;

-- insert to DefualtCourses table
declare @cntr int
declare @cntr_max int
declare @CRSID int
declare @YearNo int
declare @IsCompulsory int
declare @EntDT datetime
declare @IsAval int
declare @EmpID int

set @cntr = 1

select @cntr_max = COUNT(*) from @pSelCRSInfo

--select 'Max No rows in UDDT' + convert(char(2), @cntr_max)

while(@cntr <= @cntr_max)
begin

select
@CRSID=CRSID,
@YearNo=YearNo,
@IsCompulsory=IsCompulsory,
@EntDT=EntDT,
@IsAval=IsAval,
@EmpID=EmpID
from @pSelCRSInfo where CntrNo=@cntr

--select @EmpID -- from @pSelCRSInfo-- where CntrNo= @cntr


insert into DefaultCourses
(PRGID,CRSID,YearNo,IsCompulsory,EntDT,IsAval,EmpID)
values (@newPRGID,@CRSID,@YearNo,@IsCompulsory,@EntDT,@IsAval,@EmpID)

select 'rowcount after insert' + CONVERT(char(1), @@rowcount) -- counts all four rows --> CORRECT

if @@rowcount > 0
begin
select 'counter value' + convert(varchar(12), @cntr) --prints all four iterations --> CORRECT
set @cntr = @cntr + 1
end
else
begin
rollback tran
return 3
end
end -- end of loop

--select 'last counter' + CONVERT(char(2),@cntr) THIS LINE PRINTS 5 --> CORRECT

select 'row count' + convert(char(1), @@rowcount) -- This line prints 0 --> INCORRECT
select 'row count' + convert(char(1), @@rowcount) -- this line prints 1 --> CORRECT BUT WHY NOW

if @@rowcount > 0
begin
select 'im in commit'
commit tran
return 1
end

end -- end of the top @@rowcount
else
begin
--select 'im in rollback'
rollback tran
return 3
end
end -- end of fresh insert block

用于执行存储过程的值

USE [SMSV100]
GO

--delete from ACDPrograms
--delete from DefaultCourses

DECLARE @return_value int,
@pPRGID int
declare @crs as dbo.SelectedCourses
set @pPRGID = -1

insert into @crs (CRSID,YearNo,IsCompulsory,EntDT,IsAval,EmpID)
values (1, 1, 1, GETDATE(), 1, 1),(2, 1, 1, GETDATE(), 1, 1),(3, 2, 1, GETDATE(), 1, 1),(4, 2, 1, GETDATE(), 1, 1)

EXEC @return_value = [dbo].[USPSvUpdtACDProgramFromTypes]
@pPRGCode = N'2323',
@pPRGName = N'sddsaada',
@pPRGDescr = N'asdda',
@pFCTID = 3,
@pMinCredits = 10,
@pNZQA = 7,
@pNoOccrPerYear = 2,
@pFirstMonthOccr = 2,
@pSecondMonthOccr = 7,
@pDOMFee = 4500,
@pINTLFee = 45000,
@pIsAval = 1,
@pEmpID = 1,
@pSelCRSInfo = @crs,
@pPRGID = @pPRGID OUTPUT

SELECT @pPRGID as N'@pPRGID'

SELECT 'Return Value' = @return_value

GO

这是结果窗口的屏幕截图 enter image description here

谢谢

最佳答案

你的问题很长,但这似乎是它的基础:

select 'row count' + convert(char(1), @@rowcount) -- This line prints 0 --> INCORRECT
select 'row count' + convert(char(1), @@rowcount) -- this line prints 1 --> CORRECT BUT WHY NOW

@@rowcount 指的是之前的select。第一个是转换为单字符字符串。这是一个坏主意。如果数字太大,它将返回'*'。但是,查询也成功了,因此第二个返回 @@rowcount

我建议这样做:

declare @rc int;
set @rc = @@rowcount;
select 'row count' + convert(varchar(255), @rc)

编辑:

你的循环需要是这样的:

declare @rc int;

insert into DefaultCourses(PRGID, CRSID, YearNo, IsCompulsory ,EntDT, IsAval, EmpID)
values (@newPRGID,@CRSID,@YearNo,@IsCompulsory,@EntDT,@IsAval,@EmpID) ;

set @rc = @@rowcount;

select 'rowcount after insert' + CONVERT(char(1), @rc) -- counts all four rows --> CORRECT

if @rc > 0
begin
select 'counter value' + convert(varchar(12), @cntr) --prints all four iterations --> CORRECT
set @cntr = @cntr + 1
end
. . .

也就是说,首先将 @@ROWCOUNT 复制到变量中。然后使用复制的值。

关于sql - 为什么对 @@rowcount 的两次调用会返回两个不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22495006/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com