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.
Author |
Topic |
marginerazvan
Starting Member
14 Posts |
Posted - 2007-06-14 : 07:17:27
|
Hi there.I have a table (FORMULA_DEFINITIONS) that containsPROFILE intFORMULA varchar(100)The formula COLUMN contains different formulas for different profiles.For example1 5*3+122 10*5*4etcI have a function dbo.ReturnResultOfFormula that must return the result of the formula applied for a profile.Ex. ReturnResultOfFormula(1) must return 27ReturnResultOfFormula(2) must return 200Here is the function definition:CREATE FUNCTION dbo.ReturnResultOfFormula(@Profile int)RETURNS int as BEGIN DECLARE @Result int, @SQL varchar(100)SELECT @SQL = FORMULA FROM FORMULA_DEFINITIONS WHERE PROFILE = @ProfileSET @Result = @SQL(it will not work because it will return '5*3+12')SET @SQL = N'SELECT @Result = ' + @SQLEXEC sp_executesql @SQL(it will not work because I cannot use EXEC inside of user-defined-function)RETURN @ResultGOWhat should I do? How can I get the results of formula? How can I correctly use the Dynamic SQLI will appreciate any suggestions/solutions.Thank you for help.PS. The real situation is more complicated, but I summarized it. |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2007-06-14 : 08:34:14
|
can't do it in an in-line statement (without an extended sp or clr in v2005).You would have to loop through and do it in an SPordeclare @result int, @sql nvarchar(1000)select @sql = 'select @result = ' + @formulaexec sp_executesql @sql, N'@result int out'), @result out(Looks very much like something I came across to value financial instruments)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
marginerazvan
Starting Member
14 Posts |
Posted - 2007-06-14 : 09:19:29
|
The error message says:Only functions and extended stored procedures can be executed from within a function.quote: Originally posted by nr declare @result int, @sql nvarchar(1000)select @sql = 'select @result = ' + @formulaexec sp_executesql @sql, N'@result int out'), @result out(Looks very much like something I came across to value financial instruments)==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy.
|
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-14 : 09:24:02
|
You cant use Dynamic SQL in a Function. Make use of Stored procedure insteadMadhivananFailing to plan is Planning to fail |
 |
|
marginerazvan
Starting Member
14 Posts |
Posted - 2007-06-14 : 10:20:28
|
The problem is that my function is used in SELECT query statementEx. SELECT dbo.ReturnResultOfFormula(MATERIAL.PROFILE), MATERIAL.MATERIAL_ID, etcIf I will transform it in a SP, I will not be able to use it in SELECT queries.Or am I wrong?quote: Originally posted by madhivanan You cant use Dynamic SQL in a Function. Make use of Stored procedure insteadMadhivananFailing to plan is Planning to fail
|
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-15 : 03:33:16
|
One of methods I can think of iscreate table #formula (formula varchar(1000))insert into #formula select 'select '+cast(col as varchar(10))+' as col,'+value+' as value union all ' from yourtableselect * from #formula Declare @sql varchar(8000)select @sql=''Select @sql=@sql+ formula from #formulaSelect @sql=left(@sql,len(@sql)-len(' union all '))exec(@sql) You can export the result to another temp table and match it with your original tableMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|