Implementation of four kinds of multiplication tables with SQL

Time:2020-12-3

Here are four kinds of multiplication tables of SQL statements written with while and if conditions

Implementation of SQL statement


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

result:

Implementation of SQL statement

DECLARE @I INT ,@J INT,@S VARCHAR()
 SET @I=
 WHILE @I<
 BEGIN
 SET @J=
 SET @S=''
 WHILE @J>
 BEGIN
 IF @J>@I
 BEGIN
 SET @ [email protected]+ '-- seven spaces
 END
 ELSE 
 BEGIN
 SET @[email protected]+CAST(@I AS CHAR())+'*'+CAST(@J AS CHAR())+'='+CAST((@I*@J)AS CHAR())
 END
 SET @[email protected]
 END
 PRINT @S
 SET @[email protected]+
 END

result:

SQL statement implementation — 9×9 upper right corner


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 @[email protected]+'    '--7
END
ELSE 
BEGIN 
SET @[email protected]+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))
END
SET @[email protected]+1
END
PRINT @S
SET @[email protected]+1
END

result:

SQL statement implementation — 9×9 upper left corner


DECLARE @I INT ,@J INT,@S VARCHAR(100)
SET @I=1
WHILE @I<10
BEGIN
SET @[email protected]
SET @S=''
WHILE @J<10
BEGIN 
SET @[email protected]+CAST(@I AS CHAR(1))+'*'+CAST(@J AS CHAR(1))+'='+CAST((@I*@J)AS CHAR(3))
SET @[email protected]+1
END
PRINT @S
SET @[email protected]+1
END

result:

The above is a multiplication table written in four ways. I hope you like it.

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]