SQL:打印带状矩阵

发布时间 2023-09-13 06:28:31作者: samrv

数据实现矩阵计算比较易容的,想想用SQL打印带状矩阵 小程序 

SQL SERVER 2022 实现代码:

declare @row int = 1 ,
@col int = 1 ,
@line int = 1 ,
@upper int = 7,  --矩阵维度
@zero nvarchar(20) ='0',
@star nvarchar(20) ='x',
@list nvarchar(200)='';

begin
  while @row<= @upper
  begin
    set @list = '';
	set @col = 1;
	while @col <=@upper 
	begin
	  if @col in (@line-1,@line, @line+1)
	     set @list = @list + ' x ';
       else 
	     set @list = @list +' 0 ';

	  set @col = @col+1;
	end; 
	print @list 
	set @line = @line +1
    set @row = @row+1;
  end;
end; 
/*
-- 带状矩阵 
 x  x  0  0  0  0 
 x  x  x  0  0  0 
 0  x  x  x  0  0 
 0  0  x  x  x  0 
 0  0  0  x  x  x 
 0  0  0  0  x  x 

完成时间: 2023-09-13T06:16:36.4146032+08:00
*/