How do I convert this from TSQL to MYSQL?

2019-08-04 11:19发布

问题:

I'm trying to setup a date lookup table as in this: http://www.techrepublic.com/blog/datacenter/simplify-sql-server-2005-queries-with-a-dates-table/326 but the author is using tsql and not mysql.

I have created the table but now I'm trying to get it to populate. The author has this:

Create table syntax:

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

I've removed all of the @ signs and it kind of looks like it might not like date as a variable name but haven't been able to get it to run.

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;

EDIT: I tried this app: http://sourceforge.net/projects/tsql2mysql/ but it doesn't seem to run on any of my machines.

EDIT: It fails near 'DECLARE dateInsert DATETIME SET dateInsert = '1900-01-01' WHILE' with or without the @ signs, with or without Date being renamed as dateInsert.

I tried removing the 'set dateInsert =' statement and replacing with adding default = "1900-01-01" at the end of the declaration but that's a no go.

I've tried single and double quores, that didn't seem to make a difference.

回答1:

From the documentation: DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

Update

Here's something closer. You'll need to sort out the syntax for CONVERT which is different in 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 $$


回答2:

Change the dates to yyyy-mm-dd format:

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

 WHILE dateInsert < '2100-01-01'

....



标签: mysql tsql