Solved

Evaluating a string into a decimal type

Posted on 2001-09-03
5
305 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
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
george74 earned 200 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now