From e91b163571a0d06bd599089d3ae68086d8af7885 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 31 May 2024 10:07:23 -0500 Subject: [PATCH] Add Echo A Message From A SQL File as a MySQL TIL --- README.md | 3 ++- mysql/echo-a-message-from-a-sql-file.md | 32 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 mysql/echo-a-message-from-a-sql-file.md diff --git a/README.md b/README.md index beb7422..a525d74 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1433 TILs and counting..._ +_1434 TILs and counting..._ --- @@ -607,6 +607,7 @@ _1433 TILs and counting..._ - [Display Output In A Vertical Format](mysql/display-output-in-a-vertical-format.md) - [Doing Date Math](mysql/doing-date-math.md) - [Dump A Database To A File](mysql/dump-a-database-to-a-file.md) +- [Echo A Message From A SQL File](mysql/echo-a-message-from-a-sql-file.md) - [Ignore Duplicates When Inserting Records](mysql/ignore-duplicates-when-inserting-records.md) - [List Databases And Tables](mysql/list-databases-and-tables.md) - [Run Statements In A Transaction](mysql/run-statements-in-a-transaction.md) diff --git a/mysql/echo-a-message-from-a-sql-file.md b/mysql/echo-a-message-from-a-sql-file.md new file mode 100644 index 0000000..8d415d0 --- /dev/null +++ b/mysql/echo-a-message-from-a-sql-file.md @@ -0,0 +1,32 @@ +# Echo A Message From A SQL File + +Let's say we have a SQL file that we run to seed our database. We want to echo +a message to stdout at the beginning of that file's execution. We can do this +with [a MySQL client _shell +command_](https://dev.mysql.com/doc/mysql-shell/8.0/en/mysql-shell-commands.html). +Specifically, we need to use the `\system` or `\!` command to run our system's +`echo` command. + +Here is what that could look like: + +```sql +\! echo '*****************************************' +\! echo '* *' +\! echo '* Loading seed data into the database *' +\! echo '* *' +\! echo '*****************************************' + +insert into products ... +``` + +That message banner will be output when you run the script. + +```bash +$ mysql -h ::1 -P 3306 -u root -D local_database < seed_data.sql + +***************************************** +* * +* Loading seed data into the database * +* * +***************************************** +```