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

前端之家收集整理的这篇文章主要介绍了将多行SQL查询导入到单个字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
R中,如何将多行文本文件(包含sql)的内容导入单个字符串?

sql.txt文件如下所示:

SELECT TOP 100 
 setpoint,tph 
FROM rates

我需要将该文本文件导入到R字符串中,如下所示:

> sqlString
[1] "SELECT TOP 100 setpoint,tph FROM rates"

就这样,我可以把它喂给RODBC

> library(RODBC)
> myconn<-odbcConnect("RPM")
> results<-sqlQuery(myconn,sqlString)

我已经尝试了readLines命令如下,但没有给出RODBC需要的字符串格式.

> filecon<-file("sql.txt","r")
> sqlString<-readLines(filecon,warn=FALSE)
> sqlString
[1] "SELECT TOP 100 "                              "\t[Reclaim Setpoint Mean (tph)] as setpoint,"
[3] "\t[Reclaim Rate Mean (tph)] as tphmean "       "FROM [Dampier_RC1P].[dbo].[Rates]"           
>

解决方法

通用的paste()命令可以使用参数collapse =“”:
lines <- readLines("/tmp/sql.txt")
lines
[1] "SELECT TOP 100 " " setpoint,"     " tph "           "FROM rates"     

sqlcmd <- paste(lines,collapse="")
sqlcmd
[1] "SELECT TOP 100  setpoint,tph FROM rates"

猜你在找的MsSQL相关文章