一些海报在这里提出了类似的问题,这些已经把我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时