下面用while 和 if 条件写的SQL语句的四种九九乘法表
1 --9x9 左下角 2 3 DECLARE @I INT ,@J INT,@S VARCHAR(100) 4 SET @I=1 5 WHILE @I<10 6 BEGIN 7 SET @J=1 8 SET @S='' 9 WHILE @J<=@I10 BEGIN 11 SET @S=@S+CAST(@J AS CHAR(1))+'*'+CAST(@I AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))12 SET @J=@J+113 END14 PRINT @S15 SET @I=@I+116 END
结果:
1 --9X9 右下角 2 DECLARE @I INT ,@J INT,@S VARCHAR(100) 3 SET @I=1 4 WHILE @I<10 5 BEGIN 6 SET @J=9 7 SET @S='' 8 WHILE @J>0 9 BEGIN10 IF @J>@I11 BEGIN12 SET @S=@S+' ' --七个空格13 END14 ELSE 15 BEGIN16 SET @S=@S+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))17 END18 SET @J=@J-119 END20 PRINT @S21 SET @I=@I+122 END
结果:
--9X9右上角DECLARE @I INT ,@J INT,@S VARCHAR(100)SET @I=1WHILE @I<10BEGIN SET @J=1SET @S=''WHILE @J<10BEGIN IF @J<@IBEGINSET @S=@S+' '--7ENDELSE BEGIN SET @S=@S+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))ENDSET @J=@J+1ENDPRINT @SSET @I=@I+1END
结果:
--9X9左上角DECLARE @I INT ,@J INT,@S VARCHAR(100)SET @I=1WHILE @I<10BEGINSET @J=@ISET @S=''WHILE @J<10BEGIN SET @S=@S+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))SET @J=@J+1ENDPRINT @SSET @I=@I+1END
结果: