Solved

Evaluating a string into a decimal type

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

759 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

22 Experts available now in Live!

Get 1:1 Help Now