0

我正在研究一个数据库(学校项目)。我需要对该数据库进行一些测试(SQL Server 2008 R2)。

我正在尝试测试它的恢复情况。在那里我正在构建一个存储过程,以便它需要足够长的时间来使我的电脑崩溃。

问题是我正在使用的 while 循环 doenst 似乎有效。

存储过程:

USE [OnderzoekSQL]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Test_pro_opnemen] 
-- Add the parameters for the stored procedure here
@bnummer int OUT,
@i int

AS
BEGIN

SET NOCOUNT ON;
WHILE(@i <= @@ROWCOUNT )
    -- Insert statements for procedure here
    SELECT TOP 1 @bnummer = accountnumber
    FROM dbo.bank 
    ORDER BY saldo DESC

    PRINT @bnummer

    UPDATE bank
    SET saldo = '0'
    WHERE accountnumber = @bnummer
    SET @i = @i+1

END

和表:

CREATE TABLE [dbo].[bank](
[accountnumber] [nvarchar](50) NOT NULL,
[saldo] [real] NULL,
[owner_id] [int] NULL;

以及关于 nvarchar 和 accountnumber 的 int 的区别。这并不重要,因为我只在帐号中使用数字。

如果我删除 While 循环,该过程将起作用

4

5 回答 5

1

在第一个循环迭代@@Rowcount中将是 1 - 您可以在新的查询窗口中自己测试。

假设@i通常 >= 1,循环将在其第一次迭代时退出。

于 2013-01-14T10:49:33.193 回答
1

你为什么这样做

WHILE(@i <= @@ROWCOUNT )

?

@@ROWCOUNT返回受最后一条语句影响的行数。所以你UPDATE把那个数字放进去@@ROWCOUNT,然后你增加@i。你想达到什么目的?只要您UPDATE更新的行数少于@i+1,您的WHILE循环就会终止。

于 2013-01-14T10:50:16.033 回答
0

我同意其他所有人的观点,但我建议您以某种方式指定您的 MAX 条款,这可能会解决问题:

而不是返回最后受影响的语句行的@@Rowcount,而是得到一些你可以明确声明和保持的东西。我通常使用一个变量。请注意,有时会出现复杂的循环,您可能需要三个变量一个开始和结束,以及一个谓词变量。有时我发现您可能正在从语句更新复杂查询,例如开始日期,并且您需要将其与将从集合中增加的变量分开。

这是我将使用的循环方法的一个简单示例:

declare @Table Table ( personID int identity, person varchar(8));

insert into @Table values ('Brett'),('John'),('Peter');

-- say I want to affect a whole table.  I need to get it's count and HOLD it.  You could just select an expression but a variable is more clean IMHO.
declare @Max int;

-- I should set a beginning variable and statically set it, however if you are doing an update in the middle of something you can set it with 
-- a select expression as well.
declare @Current int = 1;

-- bind the variable to the count of a table I want to update.  My example is simple, it could work with a table that is very large as well though.
select @Max = count(*) from @Table

-- see data before loop
select * From @Table;

while @Current <= @Max  -- @Current is explicitly set and so is Max.  However @Current will increment in the BEGIN END BLOCK.
BEGIN
    update @Table set person = person + 'New' where personID = @Current -- update from @Current variable 

    set @Current += 1;  -- increment up one in the loop AFTER OPERATION
END

-- see data after the loop
select *
from @Table
于 2013-01-14T16:49:03.127 回答
0

这可能是旧的,但在您原来的 WHILE 语句中,您只执行 WHILE 语句之后的第一行代码。循环中必须有 BEGIN 和 END。

你的循环应该是这样的:

WHILE(@i <= @@ROWCOUNT )

   BEGIN

    -- Insert statements for procedure here
       SELECT TOP 1 @bnummer = accountnumber
       FROM dbo.bank 
       ORDER BY saldo DESC

       PRINT @bnummer

       UPDATE bank
       SET saldo = '0'
       WHERE accountnumber = @bnummer
       SET @i = @i+1

   END
于 2014-08-20T17:46:30.497 回答
0

试试这个 for While 循环....

SET NOCOUNT ON
 DECLARE @LoopCounter INT , @MaxEmployeeId INT, 
    @EmployeeName NVARCHAR(100)

SELECT @LoopCounter = min(id) , @MaxEmployeeId = max(Id) FROM #Employee

WHILE  ( @LoopCounter IS NOT NULL AND  @LoopCounter <= @MaxEmployeeId)

BEGIN
 UPDATE TOP(1) #Employee
 SET  Status = 1, @EmployeeName = Name WHERE Id = @LoopCounter  AND Status = 0 

打印@EmployeeName

   SELECT @LoopCounter  = min(id) FROM #Employee WHERE Id >= @LoopCounter AND Status = 0
END
于 2016-02-26T09:37:21.760 回答