T-SQL R language - return fewer columns

Peter Bishop 161 Reputation points
2024-02-01T13:47:50.79+00:00

I'm running an R language script under T-SQL where I use REGEX to validate data in two columns. However I only want to output the id column in my result set. My script is:

EXECUTE sp_execute_external_script
 	@language = N'R',
 	@script = N'
pattern = "^(?:[\\p{L}\\p{M}\\,\\-\\_\\.\\''…`\\s\\d]){1,255}+$"
firstnameOK <- subset(inData, grepl(pattern, FirstName, perl = TRUE))
lastnameOK <- subset(firstnameOK, grepl(pattern, LastName, perl = TRUE))
id <- levels(lastnameOK$id)
x <- data.frame(id)
outData <- x
 	',
	@input_data_1 = N'SELECT [id], [FirstName], [LastName] FROM [dummy]',
 	@input_data_1_name = N'inData',
 	@output_data_1_name = N'outData'
WITH RESULT SETS ((id int))

However this fails with the error:

EXECUTE statement failed because its WITH RESULT SETS clause specified 1 result set(s), but the statement only sent 0 result set(s) at run time.

The data.frame() code came from a web search so there may be a better way to pull only the "id" column from the input. Any ideas? Thanks.

SQL Server Transact-SQL
SQL Server Transact-SQL
SQL Server: A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.Transact-SQL: A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
102 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Bishop 161 Reputation points
    2024-02-01T14:06:39.1+00:00
    EXECUTE sp_execute_external_script
     	@language = N'R',
     	@script = N'
    pattern = "^(?:[\\p{L}\\p{M}\\,\\-\\_\\.\\''…`\\s\\d]){1,255}+$"
    firstnameOK <- subset(inData, grepl(pattern, FirstName, perl = TRUE))
    lastnameOK <- subset(firstnameOK, grepl(pattern, LastName, perl = TRUE))
    outData = data.frame(lastnameOK$id)
     	',
    	@input_data_1 = N'SELECT [id], [FirstName], [LastName] FROM [dummy]',
     	@input_data_1_name = N'inData',
     	@output_data_1_name = N'outData'
    WITH RESULT SETS ((id int))
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.