Does the MySQL Shell client support passing SQL parameters when executing SQL scripts?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 使用mysql shell客户端执行SQL脚本是否支持传入sql参数?

| username: TiDBer_rYOSh9JN

When running SQL batch processing using hive-cli, you can use --hivevar to pass parameters to the SQL statement, replacing ${param}. Does TiDB support similar functionality?

Example:
– SQL content:
vi job1.sql
select * from t where dt = ${param}
– Shell script:
sql=$(cat job1.sql)
mysql -uroot -proot -e “${sql}” --hivevar param=‘2023-08-01’

| username: 像风一样的男子 | Original post link

You can write a shell script to call SQL execution and pass parameters into SQL.

| username: TiDBer_vfJBUcxl | Original post link

Use a shell script to pass SQL statements as parameters.

| username: TiDBer_rYOSh9JN | Original post link

What do you mean? Is it something like this?
– shell script
param=‘2023-08-01’
sql=“select * from t where dt = ${param}”
mysql -uroot -proot -e “${sql}”

| username: TiDBer_rYOSh9JN | Original post link

Is it something like this?

| username: ShawnYan | Original post link

This is the use of shell, what does it have to do with TiDB? Your example is quite good, you can use it.

| username: tidb菜鸟一只 | Original post link

You can also do it this way: mysql -u root -p -e "set @1:='tabname'; source test.sql;"

The content of test.sql is like this:

select * from tab
where tname = '@1';
| username: TiDBer_vfJBUcxl | Original post link

It’s this kind.

| username: redgame | Original post link

Learned… Thank you.

| username: TiDBer_rYOSh9JN | Original post link

Is there a way to pass parameters through SQL? Without passing parameters through shell, similar to the usage of --hivevar.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.