你的位置:首页 > 数据库

[数据库]sql语句各种九九乘法表

 下面用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<[email protected] BEGIN 11 SET @[email protected]+CAST(@J AS CHAR(1))+'*'+CAST(@I AS CHAR(1))+'='+CAST((@[email protected])AS CHAR(3))12 SET @[email protected]+113 END14 PRINT @S15 SET @[email protected]+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 @[email protected]+'    ' --七个空格13 END14 ELSE 15 BEGIN16 SET @[email protected]+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@[email protected])AS CHAR(3))17 END18 SET @[email protected] END20 PRINT @S21 SET @[email protected]+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 @[email protected]+'    '--7ENDELSE BEGIN SET @[email protected]+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@[email protected])AS CHAR(3))ENDSET @[email protected]+1ENDPRINT @SSET @[email protected]+1END

结果:

 

--9X9左上角DECLARE @I INT ,@J INT,@S VARCHAR(100)SET @I=1WHILE @I<10BEGINSET @[email protected] @S=''WHILE @J<10BEGIN SET @[email protected]+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@[email protected])AS CHAR(3))SET @[email protected]+1ENDPRINT @SSET @[email protected]+1END

结果: