SQL语句打印九九乘法表

declare @a int, @b int, @str varchar(1000)
set @a=1
while(@a<=9)
begin  
 
    set @b=1    
    set @str=''    

    while(@b<=@a)    
    begin        
        select @str=@str+convert(varchar(1),@b)+'*'+convert(varchar(1),@a)+'='+convert(char(2),@a*@b)+space(2)        
        set @b=@b+1    
    end   
    
print @str    
set @a=@a+1

end


space(2) 两个空格 或则写成 ' ' ,也可根据自己定义多个空格
convert(char(2),@a*@b) 在这里让不足两位的占两个字符 使结果整齐

posted @ 2015-04-06 23:24  星空夜  阅读(621)  评论(0编辑  收藏  举报