运维日志

sql server 语句检测替换

#!/bin/bash
#检测是什么语句
#检测条件前面是否有;
#替换语句为select,进行查询备份
user=
pass=
host=
db=$1
port=1433
i=1
>/tmp/sql_resu.txt
[ $# -ne 2 ] && echo “Usage: `basename $0` dbname sqltxt” && exit
while read line
do
sql_type=`echo $line | awk ‘{print $1}’`
[ -z “$sql_type” ] && exit && echo “error…… 1 ”

if [[ “$sql_type” = “UPDATE” || “$sql_type” = “UPDATE” ]];then
table=`echo $line | awk ‘{print $2}’`
resu=`echo $line | sed -r ‘s#(.*)(where)(.*)#\1#i’ | grep “;”`
[ $? -ne 1 ] && echo “error, sql have ; …. please check…” && exit
rep_sql=`echo $line | sed -r “s#(.*)(where)(.*)#select * from $table \2 \3#i”`
echo $rep_sql >> /tmp/sql_resu.txt
# /opt/mssql-tools/bin/sqlcmd -U $user -P${pass} -S ${host},${port} -d ${db} -W -Q ‘”${rep_sql}”‘

fi

if [[ “$sql_type” = “DELETE” || “$sql_type” = “delete” ]];then
# table=`echo $line | sed -r ‘s#.*from (.*) where#\1#i’`

table=`echo $line | awk ‘{print $3}’`
resu=`echo $line | sed -r ‘s#(.*)(where)(.*)#\1\2#i’ | grep “where”`
[ $? -ne 0 ] && echo “error, sql have ; …. please check…” && exit
rep_sql=`echo $line | sed -r “s#(.*)(where)(.*)#select * from $table \2 \3#”`
echo $rep_sql >> /tmp/sql_resu.txt sqlcmd -U $user -P${pass} -S ${host},${port} -d ${db} -W -Q “${rep_sql}”
#/opt/mssql-tools/bin/sqlcmd -U $user -P${pass} -S ${host},${port} -d ${db} -W -Q “${rep_sql}”
echo “1”
fi

let i++
done < delete.txt
echo ” $i sql is finish….”
/opt/mssql-tools/bin/sqlcmd -U $user -P${pass} -S ${host},${port} -d ${db} -W –

评论 抢沙发

  • QQ号
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活