Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 calculate expession in sql-function

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2004-07-13 : 10:12:56
Lutz writes "Hello,

i have a lot of expressions in varchar-Format like

'122.2 + 3 > 22.0 and 7 < 9'.

I got this expressions out of replaces. Now I have to check if the expression is true or false. In a procedure it is easy like this:

declare @expression varchar(500)
set @expression = '1<2'
DECLARE @sql nvarchar(4000), @min varchar(20)

SELECT @sql = N'select @min = case when (' + @expression + N') then 1 else 0 end'
EXEC sp_executesql @sql, N' @min varchar(20) OUTPUT', @min OUTPUT

select @min


But I am working in a Funktion and you can't use execute.

Do you know an other way to get the result of expressions, whitch works in funktions?

Thanks Lutz(Germany)!"

ditch
Master Smack Fu Yak Hacker

1466 Posts

Posted - 2004-07-13 : 11:03:35
A stored proc with an output parameter and a temp table might do the trick:

create procedure test (@Formula varchar(100), @Res INT OUTPUT)
as
CREATE TABLE #Res(col1 INT)
exec('INSERT INTO #Res SELECT ' + @Formula)

SET @Res = (SELECT * FROM #Res)

go

DECLARE @A INT

exec test '10 + 8 * 5', @A OUTPUT

SELECT @A

Duane.
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2004-07-13 : 11:04:42
I don't. I've tried many many times. Do you have to use a function?

Corey
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-07-13 : 11:29:09
The SQL Guru book (the Sprocs/XML one, not the T-SQL one) has an xp_SQL that wraps sp_executeSQL as a extended procedure, which you can then call from a function. I think ... <g> Sorry, I haven't got the book near by

Kristen
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-07-13 : 20:05:19
Here ya go. Be careful.

I sorta feel like I am making a mistake posting it...


declare @expression varchar(500)
set @expression = '1<2'
DECLARE @sql nvarchar(4000), @min varchar(20)
SELECT @sql = N'select case when (' + @expression + N') then 1 else 0 end min'
select dbo.f_ExceuteAdodbScalar(@sql)

---
1

------------------------------------
Function to create adodb connection and execute SQL.
Originally found here: [url]http://www.davidpenton.com/testsite/scratch/dbo.sp_ExecuteAdodbScalar.txt[/url]

drop function [dbo].[f_ExceuteAdodbScalar]
GO
/************************************************************************

This function acts just like the ExecuteScalar in ADO.Net, but this uses ADODB.

It takes a single SQL statement, executes it, then takes the first value.

****TO BE ADDED****

IT HAS NO CHECKS FOR EOF!!!

it wouldn't be hard to add this...

************************************************************************/
create function [dbo].[f_ExceuteAdodbScalar](
@sql varchar(2048)
)
returns varchar(255)
as
begin
declare
@ret varchar(255)
, @hr int
, @conn int
, @rs int
, @fields int
, @field int

EXEC @hr = master.dbo.sp_OACreate 'ADODB.Connection', @conn OUTPUT
IF @hr <> 0 RETURN 'ERROR: -1'

-- this conn string is for the local machine, trusted connection, master database
-- it could very easily be modified to use the current database
-- so this UDF could be used in multiple DBs
-- or, the SQL passed in should be DB qualified
EXEC @hr = master.dbo.sp_OAMethod @conn, 'Open', NULL, 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=master;Data Source=localhost'
IF @hr <> 0 RETURN 'ERROR: -2'

EXEC @hr = master.dbo.sp_OAMethod @conn, 'Execute', @rs OUTPUT, @sql
IF @hr <> 0 RETURN 'ERROR: -3'

EXEC @hr = master.dbo.sp_OAMethod @rs, 'Fields', @fields OUTPUT
IF @hr <> 0 RETURN 'ERROR: -4'

EXEC @hr = master.dbo.sp_OAMethod @fields, 'Item', @field OUTPUT, 0
IF @hr <> 0 RETURN 'ERROR: -5'

EXEC master.dbo.sp_OAGetProperty @field, 'Value', @ret OUTPUT

-- destroy in reverse order
EXEC @hr = master.dbo.sp_OADestroy @field
EXEC @hr = master.dbo.sp_OADestroy @fields

-- must close!!!
EXEC @hr = master.dbo.sp_OAMethod @rs, 'Close', NULL
IF @hr <> 0 RETURN 'ERROR: -6'

EXEC @hr = master.dbo.sp_OADestroy @rs

EXEC @hr = master.dbo.sp_OAMethod @conn, 'Close', NULL
IF @hr <> 0 RETURN 'ERROR: -7'

EXEC @hr = master.dbo.sp_OADestroy @conn

return @ret
end




--Ken
Your Kung-Fu is not strong. -- 'The Core'
Go to Top of Page
   

- Advertisement -