Domanda

I'm trying to setup a demo of some software for a client and need live reporting data to do so. This is business software that will rely on information that would normally reside in an ERP type system where there are sales orders and purchase orders (and all the data to build those things).

I looked long and hard for a sample ERP database and the only thing I could come up with was MySQL port of the MS AdventureWorks DB. This is great and has a lot of data I can use to sample.

The problem is, all the dates on the orders are from 2001-2004.

What I'd like to do is write a simple MySQL script that will loop through every datetime field in the entire AdventureWorks DB and add 10 years to it. This will make the date range (2011-2014), which is much more reasonable.

I found this post that had a very similar script I thought I could borrow from, but for some reason I can't seem to port it over to MySQL.

I've boiled it down to this little piece of code. For some reason, my MySQL will not run the WHILE loop - and I am clueless why.

USE adventureworks;

CREATE TEMPORARY TABLE ColumnList 
       (
      TableName NVARCHAR (100),
      columnName NVARCHAR (100)
       );


INSERT INTO ColumnList
        select t.table_name,c.column_name
        from information_schema.tables t inner join information_schema.columns c on t.table_name=c.table_name
        where c.column_type = 'datetime';

SET @Counter = 1;
SET @TotalCount = (SELECT COUNT(*) FROM ColumnList C);

BEGIN
    -- do stuff here
    @Counter = @Counter + 1;
END
END WHILE;

Error that comes out:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHILE @Counter <= @TotalCount DO
BEGIN
  -- do stuff here
END
END WHILE' at line 1

Any help would be appreciated.

Final Update * The answer below was right, but in case anywhere stumbled upon this looking for an answer, I couldn't find one without getting very complicated with prepared statements/etc because I couldn't build the dyanmic SQL in MySQL. I ended up writing this little script inside my CodeIgniter app to handle it.

Hope it helps someone down the line:

$this->load->database();    

$sql = "select t.table_name,c.column_name
from information_schema.tables t inner join information_schema.columns c on t.table_name=c.table_name
where c.table_schema='adventureworks' and c.column_type = 'datetime';";

$columnlist = $this->db->query($sql);

foreach ($columnlist->result_array() as $row) 
{
  //Now you have list of columns and tables, need to go in to each table, get all
  //results and update
  $column = $row['column_name'];
  $table = $row['table_name'];

  $sql = 
    "select distinct $column as 'thisdate', DATE_ADD($column, INTERVAL 10 YEAR) as 'newdate'
    from $table";

  $valuelist = $this->db->query($sql);
  $results = $valuelist->result_array();

  foreach ($results as $value) 
  {        
    $thisdate = date ("Y-m-d H:i:s", strtotime($value['thisdate']));
    $newdate = date ("Y-m-d H:i:s", strtotime($value['newdate']));

    $updatestmt =
      'update '.$table.' set '.$column." = '". $newdate ."' where $column = '".$thisdate."'";

    echo $updatestmt;
    $this->db->query($updatestmt);
    echo ' -- rows affected '.$this->db->affected_rows().'<br/>'; 
  }
}
È stato utile?

Soluzione

You can only use a while loop in a stored procedure, function or a trigger.
Your code snippet does not seem to be a part of any such sp or trigger.
And hence is the error.

Please refer to WHILE Syntax for more details.

Altri suggerimenti

Perhaps add a delimiter after the BEGIN END?

WHILE @Counter <= @TotalCount DO
    BEGIN
      -- do stuff here
    END;
END WHILE;

Secondly, remember to increment @counter.

UPDATE mytable SET mydate = DATE_ADD(mydate, INTERVAL 10 YEAR)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top