클라이언트에 대한 일부 소프트웨어의 데모를 설정하려고하며 라이브보고 데이터가 필요합니다. 이것은 일반적으로 판매 주문 및 구매 주문 (및 해당 데이터를 작성하는 데 필요한 모든 데이터)이있는 ERP 유형 시스템에 상주하는 정보에 의존하는 비즈니스 소프트웨어입니다.while 루프 사용/MySQL 데이터베이스의 모든 날짜에 10 년 추가
샘플 ERP 데이터베이스가 길어 보였습니다. 내가 생각할 수있는 유일한 것은 MySQL port of the MS AdventureWorks DB입니다. 이것은 훌륭하고 샘플로 사용할 수있는 많은 데이터가 있습니다.
문제는 모든 주문 날짜가 2001-2004 년입니다.
전체 AdventureWorks DB에서 모든 datetime 필드를 반복하고 10 년을 추가하는 간단한 MySQL 스크립트를 작성하고 싶습니다. 이렇게하면 날짜 범위 (2011-2014)가 훨씬 합리적입니다.
this post 나는 차용 할 수 있다고 생각했던 스크립트와 비슷한 스크립트를 가지고 있지만, 어떤 이유로 MySQL로 포팅 할 수없는 것 같습니다.
나는이 작은 코드 조각을 끓여 냈다. 어떤 이유로 MySQL은 WHILE 루프를 실행하지 않을 것이며 그 이유는 알 수 없습니다. 나오는
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 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
.
최종 업데이트 * 대답은 아래를 잘했지만, 경우에 어디 내가 등 난을 구축 할 수 없기 때문에 준비된 문/매우 복잡한받지 않고 하나를 찾을 수 없습니다 대답을 찾고이 우연히 MySQL의 다이나믹 SQL. 나는 CodeIgniter 앱 안에이 작은 스크립트를 작성하여 처리했다.
$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/>';
}
}
고마워요. – VPel