19

I create a function to execute dynamic SQL and return a value. I am getting "Only functions and some extended stored procedures can be executed from within a function." as an error.

The function:

Create Function fn_GetPrePopValue(@paramterValue nvarchar(100))
returns int as
begin
declare @value nvarchar(500);

Set @SQLString  = 'Select Grant_Nr From Grant_Master where grant_id=' + @paramterValue

exec   sp_executesql
       @query = @SQLString,       
       @value = @value output

return @value   
end 

The execution:

Select dbo.fn_GetPrePopValue('10002618') from Questions Where QuestionID=114

and:

Select fn_GetPrePopValue('10002618') from Questions Where QuestionID=114

Is the function being called properly or is the function incorrect?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Chaka
  • 1,709
  • 11
  • 33
  • 58

3 Answers3

9

You cannot use dynamic SQL from a function, neither can you call stored procedures.

Create proc GetPrePopValue(@paramterValue nvarchar(100))
as
begin
declare @value nvarchar(500),
        @SQLString nvarchar(4000)

Set @SQLString = 'Select @value = Grant_Nr From Grant_Master where grant_id = @paramterValue'

exec sp_executesql @SQLString, N'@paramterValue nvarchar(100)', 
       @paramterValue, 
       @value = @value output

return @value   
end 
Aleksandr Fedorenko
  • 16,594
  • 6
  • 37
  • 44
2

Functions are limited in what they can use, so that you can use them in a query without accidentally make something that would give horrible performance. Using dynamic queries is one of those things, as that would cause a query planning for each execution, and also would keep the function from being able to be part of a query plan.

You don't need the dynamic query at all in this case, just return the value:

Create Function fn_GetPrePopValue(@paramterValue nvarchar(100))
returns int as
begin

return (select Grant_Nr From Grant_Master where grant_id = @paramterValue)

end 
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

I don't think you can use dynamic SQL from a function, nor do I think you need to in your case. Looks like you want something closer to this:

Create Function dbo.fn_GetPrePopValue(@paramterValue nvarchar(100))
returns int as
begin
  declare @value int
  declare @SQLString varchar(MAX)

  Select @value=Grant_Nr From Grant_Master where grant_id=@paramterValue

  return @value
end 

SQL Fiddle Demo

Also, please check your data types to make sure you're fields are correct. Seems odd to pass in a varchar for the id and return an int for the other field. Either way, this should help you get going in the right direction.

sgeddes
  • 62,311
  • 6
  • 61
  • 83