Board logo

标题: [系统相关] 求助批处理查询数据库并赋值给变量 [打印本页]

作者: 343355202    时间: 2020-10-16 10:45     标题: 求助批处理查询数据库并赋值给变量

想做一个bat,从数据库中查出一个字段值,然后赋值被bat的变量。以下脚本可以登录数据库,但是不能成功输入查询命令?大神帮看看哪里有问题?
  1. for /f "delims=" %%a in ('echo select date from product where productid='01' ;^|sqlplus product/qazwsx123@productinfo') do echo %%a
  2. pause
复制代码

作者: 343355202    时间: 2020-10-19 16:30

求高手支招
作者: Batcher    时间: 2020-10-19 20:24

回复 2# 343355202


for /f "delims=" %%a in ('echo select date from product where productid^="01"^; ^| sqlplus product/qazwsx123@productinfo') do echo %%a
试试这样可以吗?
作者: 343355202    时间: 2020-10-20 11:20

回复 3# Batcher

再将双引号改成单引号即可。非常感谢Batcher!!
执行结果
  1. 连接到:Oracle Database ...
  2. SQL>
  3.    DATE
  4. ----------
  5.   20201020
  6. SQL> 从 Oracle Database ... 断开
复制代码
在请教下如何将select出来的date的值“20201020”,赋值给bat中的其它变量?
作者: Batcher    时间: 2020-10-21 00:19

回复 4# 343355202


for /f "delims=" %%a in ('echo select date from product where productid^="01"^; ^| sqlplus product/qazwsx123@productinfo ^| findstr [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]')
试试这样能否取到日期那一行
作者: 343355202    时间: 2020-10-21 17:51

回复 5# Batcher


    感谢@Batcher的指导,功能已全部实现!附上代码,共享~
  1. for /f "delims=" %%a in ('echo select date from product where productid^='01'^; ^| sqlplus product/qazwsx123@productinfo ^| findstr [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]') do(
  2. set date=%%a
  3. )
  4. set ldate=%date: =%
  5. echo DB中的日期是%ldate%
复制代码

作者: Batcher    时间: 2020-10-21 22:05

回复 6# 343355202


如果需要执行的SQL比较长、特殊字符比较多,也可以试试这个思路:
https://mp.weixin.qq.com/s/QxbMDwWiuWJIBublB1Dp9A




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2