sql语句实现四种九九乘法表

下面用while 和 if 条件写的SQL语句的四种九九乘法表

 sql语句实现--x 左下角九九乘法表

DECLARE @I INT ,@J INT,@S VARCHAR()
 SET @I=
 WHILE @I<
 BEGIN
 SET @J=
 SET @S=''
 WHILE @J<=@I
 BEGIN 
 SET @S=@S+CAST(@J AS CHAR())+'*'+CAST(@I AS CHAR())+'='+CAST((@I*@J)AS CHAR())
 SET @J=@J+
 END
 PRINT @S
 SET @I=@I+
 END

结果:

 sql语句实现--X 右下角九九乘法表

DECLARE @I INT ,@J INT,@S VARCHAR()
 SET @I=
 WHILE @I<
 BEGIN
 SET @J=
 SET @S=''
 WHILE @J>
 BEGIN
 IF @J>@I
 BEGIN
 SET @S=@S+'    ' --七个空格
 END
 ELSE 
 BEGIN
 SET @S=@S+CAST(@I AS CHAR())+'*'+CAST(@J AS CHAR())+'='+CAST((@I*@J)AS CHAR())
 END
 SET @J=@J-
 END
 PRINT @S
 SET @I=@I+
 END

结果:

sql语句实现--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+'    '--7
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

结果:

sql语句实现--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

结果:


以上内容就是通过四种方式写的九九乘法表,希望大家喜欢。