t-sql 做 1 ~ 12 個月份

 

以下範例示範用 t-sql 做 1 ~ 12 個月份

DECLARE @Table AS TABLE
  (
     [month] VARCHAR(2)
  );
DECLARE @id INT;

SET @id = 1;

WHILE @id <= 12
  BEGIN
      INSERT INTO @Table
                  (month)
      VALUES      (RIGHT('0' + Ltrim(Str(@id)), 2));

      SET @id += 1;
  END

SELECT *
FROM   @Table; 

結果畫面