How to Write a Loop in TiDB

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

Original topic: tidb循环要怎么写

| username: Jjjjayson_zeng

【TiDB Usage Environment】Production Environment
【TiDB Version】
【Reproduction Path】
【Encountered Problem: Problem Phenomenon and Impact】Don’t know how to write a TiDB loop, hoping for an example from experts
【Resource Configuration】
【Attachments: Screenshots/Logs/Monitoring】

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

Write a loop from the host level, like this:

for i in `seq 10000`; do 
  mysql -uroot -h 10.10.10.16 -P4000 -pxxxxxx -e "insert into test.t1 values($i, floor(rand()*10000000))"; 
done;

Use backticks for seq 10000.

| username: Jjjjayson_zeng | Original post link

How to write loop data results?

| username: xingzhenxiang | Original post link

for i in seq 10; do echo “insert into test.t1 values($i,floor(rand()*10000000))”; done;

| username: xingzhenxiang | Original post link

for i in `seq 10`; do
  exec_sql="insert into test.t1 values($i,floor(rand()*10000000))"
  mysql -uroot -h 10.10.10.16 -P4000 -pxxxxxx --comments -e "${exec_sql}"
done;
| username: cassblanca | Original post link

Currently, it can only be supported through external scripts.

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

More complex requirements can be written using shell scripts.

| username: Jjjjayson_zeng | Original post link

What I mean is whether there is something similar to MySQL while or stored procedures.

| username: Jjjjayson_zeng | Original post link

I want to write an SQL right now.

| username: 有猫万事足 | Original post link

Currently, stored procedures are not supported.
Use shell or python instead.

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

TiDB does not support stored procedures. Currently, this can only be achieved through shell, or Java, Python, etc. It cannot be done purely with SQL.

| username: cassblanca | Original post link

TiDB currently does not support Procedures, UDF functions, etc. You can achieve your needs through scripting languages like Shell, Perl, Python. I believe that in the future, the stored procedure and function features will be improved, and Ti/SQL (similar to Oracle PL/SQL) might be introduced as well. :smiley:

| username: zhanggame1 | Original post link

Awesome, I really don’t know how to do this. How about using Python to write it?

| username: redgame | Original post link

Use external scripts.

| username: system | Original post link

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