hi all,
I would like to create a function which can eval string expressiion
select dbo.fn_eval_string('1+2+3')
return 6
and if I do dbo.fn_eval_string('2-(5-3)')
return 0
is that possible? it should also support multiply and division.Kevin,
One possible solution is to use dynamic sql, but you can not use it in a
udf. You will have to use a stored procedure instead.
Example:
create procedure p1
@.s varchar(50),
@.i int output
as
set nocount on
declare @.sql varchar(4000)
set @.sql = N'select @.i = ' + @.s
exec sp_executesql @.sql, N'@.i int output', @.i output
go
declare @.i int
exec p1 '2 * (3 + 5)', @.i output
print @.i
go
The code has not been tested.
Be careful with the use of dynamic sql, there are pros and cons that you
should be aware of, before using it.
http://www.sommarskog.se/dynamic_sql.html
AMB
"Kevin" wrote:
> hi all,
> I would like to create a function which can eval string expressiion
> select dbo.fn_eval_string('1+2+3')
> return 6
> and if I do dbo.fn_eval_string('2-(5-3)')
> return 0
> is that possible? it should also support multiply and division.
>
>
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment