专业编程基础技术教程

网站首页 > 基础教程 正文

Shell实现SQL删除语句(shell sqlplus)

ccvgpt 2024-08-03 12:26:35 基础教程 17 ℃

场景:

  1. hive删除指定库中的表
  2. 生成删除、清空表语句等
  3. Linux语法学习

一、生成删除语句

以hive数据库为例

Shell实现SQL删除语句(shell sqlplus)

export_drop.sh

#!/bin/sh
echo '' > temp_tables.txt # 每次执行清空文本
beeline -u jdbc:hive2://192.168.11.110:10000/default -ntest -ptest -e "use adp_usr; show tables ;" | while read line
do
echo "drop table if exists $line;" >> temp_tables.txt
done
# 去除冗余内容;多个匹配替换用 ;隔开
sed -i 's/|//g;s/-//g;s/+//g;s/drop table if exists ;//g' ./temp_tables.txt

二、执行删除脚本

方法一

使用beeline命令(推荐)

mv temp_tables.txt  drop_tables.sql
beeline -u jdbc:hive2://192.168.11.110:10000/default -ntest -ptest -f drop_tables.sql

方法二

编写shell脚本

run_drop.sh

#!/bin/sh
tables=`cat ./temp_tables.txt`
echo $tables
beeline -u jdbc:hive2://192.168.11.110:10000/default -ntest -ptest -e "use adp_usr;$tables;"

Tags:

最近发表
标签列表