我在MySQL中的这个循环做错了什么

时间:2015-04-23 00:48:55

标签: mysql sql

以下是我要回答的问题:

  1. 编写一个计算10到20之间公共因子的脚本。要查找公因子,可以使用模运算符(%)检查数字是否可以均匀地分成两个数字。然后,此脚本应打印显示如下公共因子的行:

    10和20的常见因素

    1

    2

    5

  2. 这是我的代码:

        USE MyGuitarShop;
    
        IF OBJECT_ID ('test') IS NOT NULL
        DROP PROCEDURE test;
    
        CREATE PROCEDURE test ()
        BEGIN 
        DECLARE counter INT
        DECLARE fact10 INT;
        DECLARE fact20 INT;
        DECLARE factors varchar (100);
    
        SET fact10 = 10;
        SET fact20 = 20;
        SET counter = 1;
        SET factors = 'Factors of 10 and 20';
    
        WHILE (counter <= 10/2) DO
            IF (fact10 % OF counter = 0 AND fact20 % OF counter = 0) THEN
            SET factors = CONTACT (factors, counter, ' ');
        END;
    

    这是我不断得到的错误:

        Msg 102, Level 15, State 1, Procedure test, Line 6
        Incorrect syntax near ')'.
        Msg 155, Level 15, State 2, Procedure test, Line 8
        'INT' is not a recognized CURSOR option.
        Msg 155, Level 15, State 2, Procedure test, Line 9
        'INT' is not a recognized CURSOR option.
        Msg 155, Level 15, State 2, Procedure test, Line 10
        'INT' is not a recognized CURSOR option.
        Msg 155, Level 15, State 2, Procedure test, Line 11
        'varchar' is not a recognized CURSOR option.
        Msg 102, Level 15, State 1, Procedure test, Line 18
        Incorrect syntax near 'DO'.
        Msg 156, Level 15, State 1, Procedure test, Line 19
        Incorrect syntax near the keyword 'OF'.
    

    我不确定我做错了什么,任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

错误消息以及代码表明您正在使用MS SQL Server。如果是这种情况,您需要使用@符号为局部变量添加前缀,并且计数器需要在while循环中递增,否则您将陷入无限循环。

此代码适用于MSSQL:

CREATE PROCEDURE test AS
BEGIN 
    DECLARE @counter INT
    DECLARE @fact10 INT
    DECLARE @fact20 INT
    DECLARE @factors varchar (100)

    SET @fact10 = 10;
    SET @fact20 = 20;
    SET @counter = 1;
    SET @factors = 'Factors of 10 and 20: ' + char(13);

    WHILE (@counter <= 10/2) -- this really should be <= 10
       BEGIN
          IF ( @fact10 %  @counter = 0 AND @fact20 % @counter = 0)
            SET @factors = CONCAT (@factors, @counter, ' ', char(13));
       SET @counter = @counter + 1;
       END
       SELECT @factors
END;

输出结果为:

Factors of 10 and 20: 
1 
2 
5 

为什么有人会想在SQL中做这个,除了作为我不理解的编程练习。

使用数字表可以执行与简单选择查询相同的操作:

-- the table master..spt_values.numbers holds integers 0-2047
select number from master..spt_values where type = 'P'
and number > 0 and 10 % number = 0 and 20 % number = 0

答案 1 :(得分:0)

首先,10和20的常见因子应该是1,2,5和10.假设你正在使用mysql并且需要创建一个程序来执行此操作,这里有一个工作示例:

create procedure foobar(var1 int, var2 int)
begin
  declare counter int default 1;
  declare msg varchar(100) default '';

  while counter <= least(var1,var2) do
    if var1 mod counter = 0 and var2 mod counter = 0 then
      set msg = concat(msg, if(msg='','',','), counter);
    end if;
    set counter = counter + 1;
  end while;

  select msg;
end

这是SQL Server的等价物:

create procedure foobar 
  @var1 int, @var2 int
as 
begin
  declare @counter int = 1;
  declare @msg varchar(100) = '';

  while @counter <= @var1 and @counter <= @var2
  begin
    if (@var1 % @counter = 0 and @var2 % @counter = 0)
      set @msg = concat(@msg, case when @msg='' then '' else ',' end, @counter);
    set @counter = @counter + 1;
  end

  select @msg;
end