The SQL Server function (2000/2005) takes the table name and field name as parameters and returns the results of the dynamic query inside the function. The results should be assigned to the Table variable, which will be used later in the stored procedure. How to achieve this?
I get an error: "Only functions and extended stored procedures can be executed inside a function."
Declare @Data as table (FieldValue varchar(100)) insert into @Data select * from MyFunction ('Person.Address','AddressID') -- Function Alter function MyFunction ( @TableName varchar(100), @FieldName varchar(100) ) returns @GetData table ( FieldValue varchar(100) ) as begin Declare @SQL varchar(250) Set @SQL = 'Select ' +@FieldName + ' from '+ @TableName Exec sp_executesql @SQL return end
source share