Skip to content

Commit e91b163

Browse files
committed
Add Echo A Message From A SQL File as a MySQL TIL
1 parent 84e2c9c commit e91b163

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1433 TILs and counting..._
13+
_1434 TILs and counting..._
1414

1515
---
1616

@@ -607,6 +607,7 @@ _1433 TILs and counting..._
607607
- [Display Output In A Vertical Format](mysql/display-output-in-a-vertical-format.md)
608608
- [Doing Date Math](mysql/doing-date-math.md)
609609
- [Dump A Database To A File](mysql/dump-a-database-to-a-file.md)
610+
- [Echo A Message From A SQL File](mysql/echo-a-message-from-a-sql-file.md)
610611
- [Ignore Duplicates When Inserting Records](mysql/ignore-duplicates-when-inserting-records.md)
611612
- [List Databases And Tables](mysql/list-databases-and-tables.md)
612613
- [Run Statements In A Transaction](mysql/run-statements-in-a-transaction.md)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Echo A Message From A SQL File
2+
3+
Let's say we have a SQL file that we run to seed our database. We want to echo
4+
a message to stdout at the beginning of that file's execution. We can do this
5+
with [a MySQL client _shell
6+
command_](https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-commands.html).
7+
Specifically, we need to use the `\system` or `\!` command to run our system's
8+
`echo` command.
9+
10+
Here is what that could look like:
11+
12+
```sql
13+
\! echo '*****************************************'
14+
\! echo '* *'
15+
\! echo '* Loading seed data into the database *'
16+
\! echo '* *'
17+
\! echo '*****************************************'
18+
19+
insert into products ...
20+
```
21+
22+
That message banner will be output when you run the script.
23+
24+
```bash
25+
$ mysql -h ::1 -P 3306 -u root -D local_database < seed_data.sql
26+
27+
*****************************************
28+
* *
29+
* Loading seed data into the database *
30+
* *
31+
*****************************************
32+
```

0 commit comments

Comments
 (0)