473,461 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to get back the value from exec() ?

For example, I have a table FORMULA_TABLE with the column FORMULA which
contains some function y=f(x) in its symbol description like '@X*2+1'

create table FORMULA_TABLE(
ID int,
FORMULA varchar(100)
)

This is a sample how I'm going to calculate the function f(x) by its
number @ID:

declare @FORMULA varchar(100)
select @FORMULA = FORMULA
from FORMULA_TABLE
where F.ID=@ID
set @FORMULA =
'declare @X int '
+' declare @Y int '
+' set @X='+convert(varchar(10),x)
+' set @Y='+@FORMULA
exec(@FORMULA)
-- some manipulations with @Y expected to be there

How to retrieve back the value of @Y to the T-SQL context?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #1
1 10108

"Evgeny Gopengauz" <ev***@ucs.ru> wrote in message
news:41**********************@news.newsgroups.ws.. .
For example, I have a table FORMULA_TABLE with the column FORMULA which
contains some function y=f(x) in its symbol description like '@X*2+1'

create table FORMULA_TABLE(
ID int,
FORMULA varchar(100)
)

This is a sample how I'm going to calculate the function f(x) by its
number @ID:

declare @FORMULA varchar(100)
select @FORMULA = FORMULA
from FORMULA_TABLE
where F.ID=@ID
set @FORMULA =
'declare @X int '
+' declare @Y int '
+' set @X='+convert(varchar(10),x)
+' set @Y='+@FORMULA
exec(@FORMULA)
-- some manipulations with @Y expected to be there

How to retrieve back the value of @Y to the T-SQL context?

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


You can use sp_executesql with an output parameter:

declare @FORMULA nvarchar(100)
declare @sql nvarchar(100)
declare @x int, @y int

set @FORMULA = '@X*2+1'
set @sql = 'select @y = ' + @formula

exec sp_executesql @sql, N'@x int, @y int OUTPUT', @x = 1, @y = @y OUTPUT
select @y

This assumes that all your formulae return an integer, of course. A more
general solution would be difficult, since you don't know in advance what
the data type of the return value will be, but in a limited case it should
work OK. If you have a small number of formulae, it might be worth
converting them to UDFs, although scalar UDFs perform poorly on large data
sets, so this might not be an option anyway.

See here for more details on output parameters:

http://support.microsoft.com/default...;EN-US;q262499
http://www.sommarskog.se/dynamic_sql.html#sp_executesql

Simon
Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: PeterF | last post by:
Hi, I've got the following problem: exec() and shell_exec(), etc. don't give anything back, just the error code 127 (command not found?!) For example, the following script: <?php exec("cat...
2
by: mirza i | last post by:
thanks for the previous replies. here is the new question (i'm absolutely sure that this should be VERY easy for a good js coder) ok: from asp i call: <img src="pics/cal.gif"...
56
by: Raphi | last post by:
Hi, I've been using an Access application I wrote for an office with the front-end stored on all computers and the back-end on one of them serving as an Access file server. Now we're moving...
0
by: athos | last post by:
Hi guys, Got a problem now :( please help... now we got a project handling records saved in a table in a sql 2000(will upgraded to 2005 soon) server. every month around a million records will...
4
by: Cylix | last post by:
I have a SP, which will exec other SP depend on the input. the "other SP" need to return a integer back. How to do this? Thanks for give me a hand.
5
by: =?Utf-8?B?RkxEYXZlTQ==?= | last post by:
I'm developing an application that gets data from 100 sources (via telnet connections, but you can think stock quotes from a webservice if you like). I was planning on using the thread pool (25...
2
by: Mick Walker | last post by:
Public Sub CheckProduct(ByVal _ConnString As String, ByVal ProductList As List(Of Import_ImportLines.Lines)) Dim ReturnValue As Integer = 0 ' Our Return Value Dim conn As New SqlConnection Dim...
0
by: flyingchen | last post by:
using System; using System.Windows.Forms; using System.Threading; using System.ComponentModel; namespace ProgressControl.Core { public delegate object Execute(params object args); public...
8
by: roundcrisis | last post by:
Hi all: I m trying to get the return value of a stored procedure with .... DbCommand command = connection.CreateCommand(); command.CommandText = "Exec some_SP"; command.CommandType =...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.