openGauss支持从文本文件执行SQL语句。openGauss提供了gsql工具实现SQL语句的批量处理。
以下场景建议使用批处理:
- 如果您重复运行查询(例如,每天或每周),将其设为脚本可以让您避免每次进行重复输入。
- 您可以通过复制和编辑脚本文件从现有的相似查询生成新查询。
- 对于多行语句或多语句序列,如果中间出现错误,不必重新键入所有内容,只需编辑脚本以更正错误,然后再次执行它。
- 您可以将脚本分发给其他人,以便他们也可以运行这些语句。
- 某些情况不允许交互式使用时,您必须使用批处理模式。
语法格式
gsql -d dbname -p port -f filename
参数说明
- dbname
- 指定想要连接的数据库名称。
- port
- 指定数据库服务器的端口号。
- -f filename
- 使用文件作为命令源而不是交互式输入。该参数指定读取文本文件的路径以及名称。
示例
1. 创建本地sql文本件。文件内容如下:
CREATE TABLE customer( c_customer_sk integer, c_customer_id char(5), c_first_name char(6), c_last_name char(8), Amount integer);INSERT INTO customer(c_customer_sk, c_customer_id, c_first_name,Amount) VALUES (3769, 'hello', 'Grace', 1000);INSERT INTO customer (c_customer_sk, c_first_name) VALUES (3769, 'Grace');INSERT INTO customer (c_customer_sk, c_customer_id, c_first_name) VALUES (3769, 'hello', DEFAULT);INSERT INTO customer (c_customer_sk, c_customer_id, c_first_name,Amount) VALUES (6885, 'maps', 'Joes',2200), (4321, 'tpcds', 'Lily',3000), (9527, 'world', 'James',5000);
2. 使用如下命令执行sql.txt中的SQL语句。
gsql -d postgres -p 21013 -f /home/user/sql.txt
结果如下。
CREATE TABLEINSERT 0 1INSERT 0 1INSERT 0 1INSERT 0 3
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.