如何将其从TSQL转换为MYSQL?

时间:2011-07-07 16:34:37

标签: mysql tsql

我正在尝试设置日期查找表,如下所示:http://www.techrepublic.com/blog/datacenter/simplify-sql-server-2005-queries-with-a-dates-table/326但是作者使用的是tsql而不是mysql。

我创建了这个表,但现在我正试图让它填充。作者有这个:

创建表格语法:

CREATE TABLE DateLookup
 (
     DateKey INT PRIMARY KEY,
     DateFull DATETIME,
     CharacterDate VARCHAR(10),
     FullYear CHAR(4),
     QuarterNumber TINYINT,
     WeekNumber TINYINT,
     WeekDayName VARCHAR(10),
     MonthDay TINYINT,
     MonthName VARCHAR(12),
     YearDay SMALLINT,
     DateDefinition VARCHAR(30),
     WeekDay TINYINT,
     MonthNumber TINYINT
 )






DECLARE @Date DATETIME
 SET @Date = '1/1/1900'     

 WHILE @Date < '1/1/2100'
 BEGIN
     INSERT INTO DateLookup
     (
         DateKey, DateFull, FullYear,
         QuarterNumber, WeekNumber, WeekDayName,
         MonthDay, MonthName, YearDay,
         DateDefinition,
                CharacterDate,
                WeekDay,
                MonthNumber
     )
     SELECT
         CONVERT(VARCHAR(8), @Date, 112), @Date, YEAR(@Date),
         DATEPART(qq, @Date), DATEPART(ww, @Date), DATENAME(dw, @Date),
         DATEPART(dd, @Date), DATENAME(mm, @Date), DATEPART(dy,@Date),
               DATENAME(mm, @Date) + ' ' + CAST(DATEPART(dd, @Date) AS CHAR(2)) + ',   
           ' + CAST(DATEPART(yy, @Date) AS CHAR(4)),
           CONVERT(VARCHAR(10), @Date, 101),
           DATEPART(dw, @Date),
           DATEPART(mm, @Date)

     SET @Date = DATEADD(dd, 1, @Date)
 END

我删除了所有@符号,看起来它可能不喜欢日期作为变量名称但是无法让它运行。

BEGIN
  DECLARE dateInsert DATETIME
     SET dateInsert = "1900-01-01"
     WHILE dateInsert < "2100-01-01" DO

         INSERT INTO DateLookup
         (
             DateKey, DateFull, FullYear,
             QuarterNumber, WeekNumber, WeekDayName,
             MonthDay, MonthName, YearDay,
             DateDefinition,
                    CharacterDate,
                    WeekDay,
                    MonthNumber
         )
         SELECT
             CONVERT(VARCHAR(8), dateInsert, 112), dateInsert, YEAR(@dateInsert),
             DATEPART(qq, dateInsert), DATEPART(ww, dateInsert), DATENAME(dw, dateInsert),
             DATEPART(dd, dateInsert), DATENAME(mm, dateInsert), DATEPART(dy,dateInsert),
                   DATENAME(mm, dateInsert) + ' ' + CAST(DATEPART(dd, dateInsert) AS CHAR(2)) + ',   
               ' + CAST(DATEPART(yy, dateInsert) AS CHAR(4)),
               CONVERT(VARCHAR(10), dateInsert, 101),
               DATEPART(dw, dateInsert),
               DATEPART(mm, dateInsert)

         SET dateInsert = DATEADD(dd, 1, dateInsert)
     END WHILE;
END;

编辑:我试过这个应用程序:http://sourceforge.net/projects/tsql2mysql/但它似乎不能在我的任何机器上运行。

编辑:它失败了 'DECLARE dateInsert DATETIME SET dateInsert ='1900-01-01' 而' 有或没有@符号,有或没有Date被重命名为dateInsert。

我尝试删除'set dateInsert ='语句并替换为在声明结尾处添加default =“1900-01-01”,但这是不行的。

我尝试过单次和双次琐事,这似乎没什么区别。

2 个答案:

答案 0 :(得分:1)

将日期更改为yyyy-mm-dd格式:

DECLARE dateInsert DATETIME
 SET dateInsert = '1900-01-01'

 WHILE dateInsert < '2100-01-01'

...

答案 1 :(得分:1)

来自文档:DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

<强>更新

这是更接近的事情。您需要理清CONVERT的语法,这在MySQL中是不同的。

DELIMITER $$
CREATE PROCEDURE test()
BEGIN
    DECLARE dateInsert DATETIME;
     SET dateInsert = '1900-01-01';
     WHILE dateInsert < '2100-01-01' DO
        BEGIN
             INSERT INTO DateLookup
             (
                 DateKey, DateFull, FullYear,
                 QuarterNumber, WeekNumber, WeekDayName,
                 MonthDay, MonthName, YearDay,
                 DateDefinition,
                        CharacterDate,
                        WeekDay,
                        MonthNumber
             )

             SELECT
                 CONVERT(VARCHAR(8), dateInsert, 112), dateInsert, YEAR(@Date),
                 DATEPART(qq, dateInsert), DATEPART(ww, dateInsert), DATENAME(dw, dateInsert),
                 DATEPART(dd, dateInsert), DATENAME(mm, dateInsert), DATEPART(dy,@Date),
                       DATENAME(mm, dateInsert) + ' ' + CAST(DATEPART(dd, dateInsert) AS CHAR(2)) + ',   
                   ' + CAST(DATEPART(yy, dateInsert) AS CHAR(4)),
                   CONVERT(VARCHAR(10), dateInsert, 101),
                   DATEPART(dw, dateInsert),
                   DATEPART(mm, dateInsert)

             SET dateInsert = DATEADD(dd, 1, dateInsert)
        END
     END WHILE;
END $$