日期:2014-05-18 浏览次数:20462 次
CREATE PROC test as begin declare @i smallint,@j smallint,@t smallint,@str varchar(100) set @i=1 set @j=1 set @str='' while @i<=9 begin while @j<=@i begin set @t=@i*@j set @str=@str+cast(@j as char(2))+'*'+cast(@i as char(2))+'='+cast(@t as char(5)) set @j=@j+1 end print @str set @i=@i+1 set @j=1 set @str='' end end exec test /* 1 *1 =1 1 *2 =2 2 *2 =4 1 *3 =3 2 *3 =6 3 *3 =9 1 *4 =4 2 *4 =8 3 *4 =12 4 *4 =16 1 *5 =5 2 *5 =10 3 *5 =15 4 *5 =20 5 *5 =25 1 *6 =6 2 *6 =12 3 *6 =18 4 *6 =24 5 *6 =30 6 *6 =36 1 *7 =7 2 *7 =14 3 *7 =21 4 *7 =28 5 *7 =35 6 *7 =42 7 *7 =49 1 *8 =8 2 *8 =16 3 *8 =24 4 *8 =32 5 *8 =40 6 *8 =48 7 *8 =56 8 *8 =64 1 *9 =9 2 *9 =18 3 *9 =27 4 *9 =36 5 *9 =45 6 *9 =54 7 *9 =63 8 *9 =72 9 *9 =81 */
------解决方案--------------------
SELECT CAST(CASE WHEN 1 <= number THEN RTRIM(number) + ' * 1 = ' + RTRIM(number) ELSE '' END AS varchar(20)), CAST(CASE WHEN 2 <= number THEN RTRIM(number) + ' * 2 = ' + RTRIM(number*2) ELSE '' END AS varchar(20)), CAST(CASE WHEN 3 <= number THEN RTRIM(number) + ' * 3 = ' + RTRIM(number*3) ELSE '' END AS varchar(20)), CAST(CASE WHEN 4 <= number THEN RTRIM(number) + ' * 4 = ' + RTRIM(number*4) ELSE '' END AS varchar(20)), CAST(CASE WHEN 5 <= number THEN RTRIM(number) + ' * 5 = ' + RTRIM(number*5) ELSE '' END AS varchar(20)), CAST(CASE WHEN 6 <= number THEN RTRIM(number) + ' * 6 = ' + RTRIM(number*6) ELSE '' END AS varchar(20)), CAST(CASE WHEN 7 <= number THEN RTRIM(number) + ' * 7 = ' + RTRIM(number*7) ELSE '' END AS varchar(20)), CAST(CASE WHEN 8 <= number THEN RTRIM(number) + ' * 8 = ' + RTRIM(number*8) ELSE '' END AS varchar(20)), CAST(CASE WHEN 9 <= number THEN RTRIM(number) + ' * 9 = ' + RTRIM(number*9) ELSE '' END AS varchar(20)) FROM ( SELECT number = 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9 ) AS A /* -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- 1 * 1 = 1 2 * 1 = 2 2 * 2 = 4 3 * 1 = 3 3 * 2 = 6 3 * 3 = 9 4 * 1 = 4 4 * 2 = 8 4 * 3 = 12 4 * 4 = 16 5 * 1 = 5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 6 * 1 = 6 6 * 2 = 12 6 * 3 = 18 6 * 4 = 24 6 * 5 = 30 6 * 6 = 36 7 * 1 = 7 7 * 2 = 14 7 * 3 = 21 7 * 4 = 28 7 * 5 = 35 7 * 6 = 42 7 * 7 = 49 8 * 1 = 8 8 *