Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Evaluating a string into a decimal type

Posted on 2001-09-03
5
Medium Priority
?
319 Views
Last Modified: 2012-06-27
This is my problem:

Server: Msg 8164, Level 16, State 1, Procedure proc_Evaluate_Formula, Line 73
An INSERT EXEC statement cannot be nested.

I have a procedure that calls another stored procedure.  In the first procedure, I have an INSERT EXEC statement.  In the seconde procedure, I also have an INSERT EXEC statement.  Now, this is not allowed.  Is there any other way to get around this?


More Info:

In the second procedure, I use INSERT EXEC to evaluate a string that looks like this 'SELECT 100 + 200 + 300' to a decimal type in the table where I INSERT EXEC.  This is done to evaluate '100 + 200 + 300'.  Is there any other way to evaluate a string that looks similar to a formula into it's decimal value?
0
Comment
Question by:yurrea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
george74 earned 800 total points
ID: 6450515
yurrea,

how about this:

set nocount on
declare @f varchar(200)
declare @sql varchar(200)
declare @i decimal

set @f = '100 + 200 + 300'

set @sql = 'insert into #t1 select cast((' + @f + ') as decimal) as result'

create table #t1(result decimal)
exec (@sql)
select @i = (select top 1 result from #t1)
drop table #t1

print @i

(in this example i didn't deal with error handling/checking if all characters in @f can be converted to decimal / ie. if @f contains letters, etc.)

Cheers.
0
 

Author Comment

by:yurrea
ID: 6451865
Hey george74!

I'm a little curious about the code that you gave.  Why is it that when you do it your way (executing a dynamic query), it works.  But when I try to play around with it - I hard-coded the dynamic query, it doesn't work? Why is that?  The '100 + 200 + 300' is not evaluated when it's hard-coded.
0
 

Author Comment

by:yurrea
ID: 6451866
Hey don't worry, I'll accept your answer anyway. :)
0
 
LVL 1

Expert Comment

by:george74
ID: 6452937
hi yurrea,

thanks for the points. sorry, i was occupied by development so couldn't check here before.
i don't know what you do wrong when hard-coding (unless you enclose the formula between quotes), because the code below works as well.

set nocount on
declare @i decimal
create table #t1(result decimal)
--exec (@sql)
exec ('insert into #t1 select 100+200+300  as result')
select @i = (select top 1 result from #t1)
drop table #t1

print @i

this would not work:
exec ('insert into #t1 select '100+200+300' as result')
nor this
exec ('insert into #t1 select cast('100+200+300' as decimal) as result')

but anyway, i guess you need a general formula evaluator, that takes the formula as a parameter, right?

cheers,
george
0
 

Author Comment

by:yurrea
ID: 6454998
you're right george!
thanks again.

Yvann
0

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question