带有注释的SQL查询从文件导入到R中

 嘎嘎19850820 发布于 2023-02-09 19:56

一些海报在这里提出了类似的问题,这些已经把我80%的方法用于读取带有sql查询的文本文件到R中用作RODBC的输入:

将多行SQL查询导入单个字符串

连接到MS SQL Server时出现RODBC临时表问题

但是,我的sql文件中有相当多的注释(作为--comment on this and that).我的问题是,如何在导入时从查询中剥离注释行,或者确保结果字符串保持换行符,从而不会将实际查询附加到注释?

例如,query6.sql:

--query 6
select a6.column1, 
    a6.column2,
    count(a6.column3) as counts
--count the number of occurences in table 1 
from data.table a6
group by a6.column1

变为:

sqlStr <- gsub("\t","", paste(readLines(file('SQL/query6.sql', 'r')), collapse = ' '))
sqlStr 
"--query 6select a6.column1, a6.column2, count(a6.column3) as counts --count the number of occurences in table 1from data.table a6 group by a6.column1"

读入R时

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有