gpt4 book ai didi

sql-server - SQL Server 2008 中的 Do while 循环

转载 作者:行者123 更新时间:2023-12-01 16:47:41 25 4
gpt4 key购买 nike

SQL Server 2008中有没有实现do while循环的方法?

最佳答案

我不确定 MS SQL Server 2008 中的 DO-WHILE,但您可以更改 WHILE 循环逻辑,以便像 DO-WHILE 循环一样使用。

示例取自此处:http://blog.sqlauthority.com/2007/10/24/sql-server-simple-example-of-while-loop-with-continue-and-break-keywords/

  1. Example of WHILE Loop

    DECLARE @intFlag INT
    SET @intFlag = 1
    WHILE (@intFlag <=5)
    BEGIN
    PRINT @intFlag
    SET @intFlag = @intFlag + 1
    END
    GO

    ResultSet:

    1
    2
    3
    4
    5
  2. Example of WHILE Loop with BREAK keyword

    DECLARE @intFlag INT
    SET @intFlag = 1
    WHILE (@intFlag <=5)
    BEGIN
    PRINT @intFlag
    SET @intFlag = @intFlag + 1
    IF @intFlag = 4
    BREAK;
    END
    GO

    ResultSet:

    1
    2
    3
  3. Example of WHILE Loop with CONTINUE and BREAK keywords

    DECLARE @intFlag INT
    SET @intFlag = 1
    WHILE (@intFlag <=5)
    BEGIN
    PRINT @intFlag
    SET @intFlag = @intFlag + 1
    CONTINUE;
    IF @intFlag = 4 -- This will never executed
    BREAK;
    END
    GO

    ResultSet:

    1
    2
    3
    4
    5

但请尝试在数据库级别避免循环Reference .

关于sql-server - SQL Server 2008 中的 Do while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4487546/

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