下面用while 和 if 条件写的SQL语句的四种九九乘法表
sql语句实现--x 左下角九九乘法表
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(2))+'='+CAST((@I*@J)AS CHAR(3)) SET @J=@J+1 END PRINT @S SET @I=@I+1 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=1WHILE @I<10BEGINSET @J=1SET @S=''WHILE @J<10BEGINIF @J<@IBEGINSET @S=@S+' '--7ENDELSEBEGINSET @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
sql语句实现--9X9左上角
DECLARE @I INT ,@J INT,@S VARCHAR(100)SET @I=1WHILE @I<10BEGINSET @J=@ISET @S=''WHILE @J<10BEGINSET @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