472,356 Members | 2,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

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 10041

"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 =...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.