我有这个存储过程来保存两个表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块。当迭代遍历循环以存储四行时,迭代的工作方式就像一个魅力,但是当我尝试访问@@ rowcount时,在循环之外的最后一个iteraton之后,当我将它称为常规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.为什么它像这样的bahves为什么它打印两个值时它被调用了两次,不应该在百万次调用之后打印相同的值事件吗?
这是循环部分及其周围:
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
这是结果窗口的屏幕截图
感谢
答案 0 :(得分:3)
你有一个很长的问题,但这似乎是它的基础:
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
复制到变量中。然后使用复制的值。