读取sql文件中的文本文件

时间:2016-04-25 20:30:15

标签: mysql sql

我有一个.sql文件,其中包含一组分配和源文件:
该文件如下所示:

SET @NAME='ABC'
SOURCE c:/dev/test.sql
SOURCE c:/dev/test1.sql
.
.
.
SOURCE c:/dev/test100.sql

SET @NAME='MNO'
SOURCE c:/dev/test.sql
SOURCE c:/dev/test1.sql
.
.
.
SOURCE c:/dev/test100.sql

使用SET命令为每个作业重复SOURCE c:/dev/*中提到的文件。

有没有办法从不同的测试文件中读取文件名,这样我就可以避免我的.sql文件中的代码冗余。

任何帮助将不胜感激!

由于

1 个答案:

答案 0 :(得分:1)

尝试:

档案:/path/to/file/test.sql

SELECT CONCAT('FROM test.sql @`NAME`: ', @`NAME`);

档案:/path/to/file/allTestFiles.sql

SOURCE /path/to/file/test.sql
SOURCE /path/to/file/test1.sql
SOURCE /path/to/file/test2.sql
# .
# .
# .
# SOURCE /path/to/file/test100.sql

档案:/path/to/file/master.sql

SET @`NAME` := 'ABC';
SOURCE /path/to/file/allTestFiles.sql

SET @`NAME` := 'MNO';
SOURCE /path/to/file/allTestFiles.sql

测试:

mysql> SOURCE /path/to/file/master.sql
Query OK, 0 rows affected (0.00 sec)

+--------------------------------------------+
| CONCAT('FROM test.sql @`NAME`: ', @`NAME`) |
+--------------------------------------------+
| FROM test.sql @`NAME`: ABC                 |
+--------------------------------------------+
1 row in set (0.00 sec)

+---------------------------------------------+
| CONCAT('FROM test1.sql @`NAME`: ', @`NAME`) |
+---------------------------------------------+
| FROM test1.sql @`NAME`: ABC                 |
+---------------------------------------------+
1 row in set (0.00 sec)

+---------------------------------------------+
| CONCAT('FROM test2.sql @`NAME`: ', @`NAME`) |
+---------------------------------------------+
| FROM test2.sql @`NAME`: ABC                 |
+---------------------------------------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

+--------------------------------------------+
| CONCAT('FROM test.sql @`NAME`: ', @`NAME`) |
+--------------------------------------------+
| FROM test.sql @`NAME`: MNO                 |
+--------------------------------------------+
1 row in set (0.00 sec)

+---------------------------------------------+
| CONCAT('FROM test1.sql @`NAME`: ', @`NAME`) |
+---------------------------------------------+
| FROM test1.sql @`NAME`: MNO                 |
+---------------------------------------------+
1 row in set (0.00 sec)

+---------------------------------------------+
| CONCAT('FROM test2.sql @`NAME`: ', @`NAME`) |
+---------------------------------------------+
| FROM test2.sql @`NAME`: MNO                 |
+---------------------------------------------+
1 row in set (0.00 sec)