Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

UDFs for beginners 1

Status
Not open for further replies.

sebes

Programmer
Apr 5, 2011
45
RO
Hello,

MySQL documentation on this page :
says:
This process is described below for the example UDF file sql/udf_example.c that is included in MySQL source distributions

Where can I find these examples and any beginner level instuctions on how to write / complie / load UDFS ?

Thanks.
 
Hi

sebes said:
Where can I find these examples and any beginner level instuctions on how to write / complie / load UDFS ?
Just as it says, "included in MySQL source distributions". Not sure if the Windows installer ships source code. You will probably have to go to the download page, choose "Source Code" from Select Platform and download Generic Linux (Architecture Independent), Compressed TAR Archive. The example themselves should be the same on all platforms, the compilation steps probably not.

Alternatively You can use Google code search to find the source codes of popular open source projects. For example the requested sql/udf_example.c.

Honestly I think this is too advanced for your SQL skill. ( However I have no idea about your C skills. ) I would suggest to use stored routines for now. They are far less powerful, but much simpler to write and debug.


Feherke.
 
I have the UDFs for SQL Server and I'm trying to translate them.

Example for testing if "something" is empty() by our own rules:

drop function dbo.Empty
go
create function
dbo.Empty (@pcSource varchar(4096))
returns bit
as begin
if @pcSource is null or @pcSource = ' '
return 1
return 0
end

go
commit work

If the question makes sense, what would that look like in MySQL ?

Thanks.
 
Hi

Overkill. A stored function is just enough :
Code:
delimiter $$

[b]create[/b] [b]function[/b] empty[teal]([/teal]pcsource [maroon]text[/maroon][teal])[/teal]
returns [maroon]int[/maroon] deterministic no sql
begin
  [b]return[/b] pcsource [b]is[/b] [b]null[/b] [b]or[/b] pcsource[teal]=[/teal][green][i]'[/i][/green][teal];[/teal]
end
$$

delimiter [teal];[/teal]

Feherke.
 
It's not my decision. I just try to do the work.

It has to be UDF.

Thanks again.
 
Hi

sebes said:
It's not my decision. [gray](...)[/gray] It has to be UDF.
Wondering why. Neither your original MSSQL function seems to be written in C, compiled to DLL and loaded dynamically. I would double-check to be sure the one who took the decision actually knows what UDFs and stored routines are in MySQL.

Anyway, sorry, I can not help with UDFs. Never needed them. ( Actually never met anybody who needed them. )


Feherke.
 
That rings a bell. Maybe there's a confusion here.

I'll put things in order before I go any further.

IF I go with stored procedures, what is the best place to start learning the available commands and the syntax ?

Thanks for your help.
 
Hi

sebes said:
[gray](...)[/gray] learning the available commands and the syntax ?
Sad, but there is almost nothing to learn :
CREATE PROCEDURE and CREATE FUNCTION Syntax said:
The [tt]LANGUAGE[/tt] characteristic indicates the language in which the routine is written. The server ignores this characteristic; only SQL routines are supported.
So you are limited to SQL only. But the Compound-Statements will be enough to complete most of the usual tasks.


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top