I'm using MS SQL Server 2005.I want to simulate a table, using a Multi-statement Table-Value User-Defined Function, but I need the function build the SQL statement from scratch each time so I can dynamically define values like the table it references. The only way I know how to run the query after it has been defined in this manner is to run an EXEC command. However I'm getting an error basically saying that the EXEC command is off limits in a User Defined Function.The Exsact Error is:>[Error] Script lines: 1-108 ------------------------ Invalid use of side-effecting or time-dependent operator in 'EXECUTE STRING' within a function.
Is there some way to get around this limitation of User Defined Functions. Or perhaps a way to simulate the functionality of Multi-statement Table-Value User-Defined Functions in a Store Procedure, specifically the ability to run where statements, or transform the data on the fly without re-writing the Stored Procedure every time.The code I’m trying to run is below.(Note: The code works as a stored procedure, so I'm sure that the core of the statment is correct)CREATE FUNCTION [dbo].[TrendLine] ( @Summary as smallint, @Start as datetime, @End as datetime, @Table as varchar(100), @X as varchar(100), @Count as varchar(100), @Duration as varchar(100))RETURNS @TrendLineTable table ( DATE_DAY datetime , EQ_REGION varchar(25) , EQ_MARKET_CLUSTER varchar(30) , Y float , X int , DURATION float , FORMULA varchar(100) , a float , b float , EX int , EY float , EX2 int , EXY float , N int )ASBEGINDECLARE @SQL as varchar(3000) , @CountText as varchar(150) , @StartText as varchar(50) , @EndText as varchar(50)SET @StartText = 'cast( ' + char(39) + cast( @Start as varchar(20) ) + char(39) + ' as datetime ) 'SET @EndText ='cast( ' + char(39) + cast( @End as varchar(20) ) + char(39) + ' as datetime ) 'IF @Summary = 1BEGIN SET @CountText = 'sum'ENDELSEBEGIN SET @CountText = 'count'ENDSET @SQL = 'INSERT INTO @TrendLineTableDECLARE TrendlineC cursor forSELECT a.DATE_DAY , s2.EQ_REGION , s2.EQ_MARKET_CLUSTER , ( ( EY - ( b * EX ) ) / N ) + ( b * X ) AS Y , X , Y AS DURATION , cast( b as varchar(100) ) + ' + char(39) + 'x + ' + char(39) + ' + cast( ( EY - ( b * EX ) ) / N as varchar(100) ) AS FORMULA , ( EY - ( b * EX ) ) / N AS a , b , EX , EY , EX2 , EXY , N FROM ( SELECT EQ_REGION , EQ_MARKET_CLUSTER , sum( X ) AS EX , sum( Y ) AS EY , sum( X2 ) AS EX2 , sum( XY ) AS EXY , count( X ) AS N , ( ( count( X ) * sum( XY ) ) - ( sum( X ) * sum( Y ) ) ) / ( ( count( X ) * sum( X2 ) ) - POWER( sum( X ), 2 ) ) AS b FROM ( SELECT ' + @X + ' AS DATE_DAY , EQ_REGION , EQ_MARKET_CLUSTER , row_number() over (partition by EQ_MARKET_CLUSTER order by ' + @X + ' ) AS X , cast( sum( ' + @Duration + ' ) as float ) / ' + @CountText + '( ' + @Count + ' ) AS Y , POWER( row_number() over (partition by EQ_MARKET_CLUSTER order by ' + @X + ' ), 2) X2 , row_number() over (partition by EQ_MARKET_CLUSTER order by ' + @X + ' ) * cast( sum( ' + @Duration + ' ) as float ) / ' + @CountText + ' ( ' + @Count + ' ) AS XY FROM ' + @Table + ' WHERE ' + @X + ' >= ' + @StartText + ' AND ' + @X + ' < ' + @EndText + ' GROUP BY ' + @X + ' , EQ_REGION , EQ_MARKET_CLUSTER ) s1 GROUP BY EQ_REGION , EQ_MARKET_CLUSTER) s2INNER JOIN ( SELECT ' + @X + ' AS DATE_DAY , EQ_MARKET_CLUSTER , row_number() over (partition by EQ_MARKET_CLUSTER order by ' + @X + ' ) AS X , cast( sum( ' + @Duration + ' ) as float ) / ' + @CountText + '( ' + @Count + ' ) AS Y FROM ' + @Table + ' WHERE ' + @X + ' >= ' + @StartText + ' AND ' + @X + ' < ' + @EndText + ' GROUP BY ' + @X + ' , EQ_REGION , EQ_MARKET_CLUSTER ) a ON s2.EQ_MARKET_CLUSTER=a.EQ_MARKET_CLUSTER'EXEC ( @SQL ) RETURNEND