Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 sp_executesql in User Defined Funection

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2003-03-06 : 07:34:08
Nikhil writes "The purpose of this function is to pass the tablename as a variable to the function and then select the OUTPUT variable into @items. This works as a stored procedure but in case of UDF it gives the following error:
"Only functions and extended stored procedures can be executed from within a function".

CREATE Function dbo.func_test (@rule_def varchar(50))

Returns varchar(50)

AS

BEGIN
Declare @items varchar(50)
Declare @strsql nvarchar(100), @paramdef nvarchar(100)
Set @strsql = N'Select @_items = Select items From '+@rule_def+' Where rule_no = @_rule_no AND LorRHS = @_LorR'
@_LorR, @_items OUTPUT'

Exec sp_executesql @strsql

Return @items

END"

X002548
Not Just a Number

15586 Posts

Posted - 2003-03-06 : 10:48:24
Check out BOL:

CREATE FUNCTION

Creates a user-defined function, which is a saved Transact-SQL routine that returns a value. User-defined functions cannot be used to perform a set of actions that modify the global database state. User-defined functions, like system functions, can be invoked from a query. They also can be executed through an EXECUTE statement like stored procedures.

User-defined functions are modified using ALTER FUNCTION, and dropped using DROP FUNCTION.





Go to Top of Page
   

- Advertisement -