下面用while 和 if 条件写的SQL语句的四种九九乘法表
--9x9 左下角 DECLARE @I INT ,@J INT,@S VARCHAR(100)
SET @I=1
WHILE @I<10
BEGIN
SET @J=1
SET @S=''
WHILE @J<=@I
BEGIN
SET @S=@S+CAST(@J AS CHAR(1))+'*'+CAST(@I AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))
SET @J=@J+1
END
PRINT @S
SET @I=@I+1
END
结果:
--9X9 右下角
DECLARE @I INT ,@J INT,@S VARCHAR(100)
SET @I=1
WHILE @I<10
BEGIN
SET @J=9
SET @S=''
WHILE @J>0
BEGIN
IF @J>@I
BEGIN
SET @S=@S+' ' --七个空格
END
ELSE
BEGIN
SET @S=@S+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))
END
SET @J=@J-1
END
PRINT @S
SET @I=@I+1
END
结果:
--9X9右上角
DECLARE @I INT ,@J INT,@S VARCHAR(100)
SET @I=1
WHILE @I<10
BEGIN
SET @J=1
SET @S=''
WHILE @J<10
BEGIN
IF @J<@I
BEGIN
SET @S=@S+' '--
END
ELSE
BEGIN
SET @S=@S+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))
END
SET @J=@J+1
END
PRINT @S
SET @I=@I+1
END
结果:
--9X9左上角 DECLARE @I INT ,@J INT,@S VARCHAR(100)
SET @I=1
WHILE @I<10
BEGIN
SET @J=@I
SET @S=''
WHILE @J<10
BEGIN
SET @S=@S+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))
SET @J=@J+1
END
PRINT @S
SET @I=@I+1
END
结果: