sql server - Using R, return SQL results but not message from a stored proc -
is there way mute print
statement occurs within stored procedure, without editing procedure?
i run sql query using rodbc r. other answers have addressed set nocount on
required in order return results of query , not messages.
my issue query runs stored proc has print
statements in it, , set nocount on
not mute them. means r script returns character(0)
when it's hitting stored proc.
is there way return results , mute stored proc's print
statement?
my code like:
library(rodbc) # sql execution function execsql = function(sql){ db=odbcdriverconnect('driver={sql server};server=<server>;database=<database>;trusted_connection=true') on.exit(odbcclose(db)) sqlquery(db, sql) } # sql query sqlfull <- " set ansi_warnings off; set nocount on; declare @myvar int insert @myvar (select top 1 blah mytable) ; create table #temp (out1 int, out2 float) insert #temp (out1, out2) exec sp_myproc @myvar ; set nocount off; select * #temp t1 join othertable t2 on t1.out1 = t2.out2" # cleanup query sql <- gsub(pattern = '\n', replacement = " ", x = sqlfull) # execute sql sqloutput <- execsql(sql) # returns character(0)
it important note ran without issue prior until stored proc had print statement added. sql query runs on ssms without issue.
i have print statements in sql stored procedures time. has worked me:
get_table_from_sql = function(sql) { conn <- rodbc::odbcconnect(dsn = "mysqlserver") sql_data = rodbc::sqlquery(conn, sql) rodbc::odbcclose(conn) return(sql_data) }
where mysqlserver
replaced own server name. you'd call this:
get_table_from_sql("exec my_procedure")
Comments
Post a Comment