Solved

Evaluating a string into a decimal type

Posted on 2001-09-03
5
309 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Syntax 17 33
MS SQL + Insert Into Table - If Doesnt Exist 9 33
CPU high usage when update statistics 2 28
sql server service accounts 4 21
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
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…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

776 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