[comp.databases] Sybase <--> Unix C

ericco@ssl.berkeley.edu (Eric C. Olson) (01/10/91)

Has anyone had any experience in attaching C code to Sybase?  We are
running Sybase on SunOS4.0.3.  I've had great success in using the
Sybase interface for making SQL calls from C programs.  However, I
have not been able to figure out how to add a C function to Sybase,
although I've heard that this is possible.

For example, I'd like to create a stored procedure called
"SphericalDistance" which would compute the distance between two
points on a sphere.  The calculation is somewhat complex, so I'd like
to have the efficiency of C.  If I had this function available in
Sybase, then could I make queries like:

	select * from points
	where
	SphericalDistance (points.alpha,points.delta,42.5,-97.4) < 5.0

to locate points that are within 5 degrees of position (42.5, -97.4)?

Thanks in advance,
Eric


--
Eric
ericco@ssl.berkeley.edu

ben@vamp.sybase.com (benjamin e. von ullrich) (01/15/91)

ericco@ssl.berkeley.edu (Eric C. Olson) wanted to know how to call a C function
from Sybase SQL (TRANSACT-SQL).

One can do this most expediently with Sybase's Open Server product.  This
allows the user to call a stored procedure on what the calling SQL Server
believes is another SQL Server, but is actually a user-programmed
general-purpose server.  Open server is a bunch of libraries which give you the
networking, interrupt handlers and other things necessary to chat with SQL
Servers.  You program the rest.  This gives you all the flexibility a 3GL
offers, including access to complex calculations, other databases' query
facilities, system devices, etc., to use these factors as part of the SQL Server
database environment.  Open Server lets you write your own SQL Server, as
Sybase has done to write a SQL Server which dos all that fancy database stuff.

In your case, you can program Open Server to contain a function which takes the
appropriate parameters for your function, does the calculation, and returns the
value to SQL Server.  You then use TRANSACT-SQL to deal with it appropriately.
Here is example T-SQL code which sets up a call to the open server, executes
the call, and deals with the result.

	declare @result int
	declare @p1 float
	declare @p2 int

	select @p1 = col1, @pr=col2 from your_table

	execute @result=openserver.master..degree_calculation @p1, @p2

	if @result > 5
	begin
		select xx, yy ...
	end
	else begin
		select zz, oo ...
	end

You can contact your sales rep to purchase Open Server.  If you don't know who
that is, call 415 596 3500 and ask for Sales Administration.  Have your site ID
handy.

Note that SQL Server has extensive trig functions built in... the calculation
you seek may not be all that difficult for SQL Server to make.  Look under
Functions in your Command Reference Manual.

As with most requests for help on Sybase questions, I would also recommend a
call to Sybase Technical Support at 415 596 3555.


..ben
------
benjamin e. von ullrich		 only i do the talking here -- not my employer.
ben@sybase.com			       {pyramid,pacbell,sun,lll-tis}!sybase!ben
  "why waste time learning when ignorance is instantaneous?" --hobbes on calvin