Create a new function from an existing function in Postgres? - postgresql

I have created a function function_1() in Postgres.
Now I want to create another similar function function_2() and copy the content of function_1().
Any Postgres command line can do that?
So far I only used copy&paste method in the command line to create function_2() (need to change the function name to "function_2" after paste)

If you are using the GUI pgAdmin, you can simply select the function in the object browser and open the reverse engineered SQL script in an SQL editor window.
There is a dedicated item in the options to automate it:
Options - Query tool - Query Editor - Copy SQL from main window to query tool

pg_dump the function definition, or use the pg_get_functiondef function to get the function definition. Modifying it is up to you.
If you're trying to programmatically create functions you might be better off with PL/PgSQL's EXECUTE command and the format function to create dynamic functions.
It's pretty unusual to need to do this, suggesting you're probably doing something that's much more easily done another way.

Related

How can you call programs from a stored procedure?

I have followed other examples on stack exchange to call a built-in program from a stored procedure, but continue to get an error.
Working off this example (Looking for a working example of any OS/400 API wrapped in an external SQL stored procedure wrapped in a user defined SQL function) I built the following to attempt to create a wrapper command to allow me to change object security (my issue is that objects I create, regardless of library) are not always accessible to others in my some function, I must manually set to a common security group.
CREATE OR REPLACE PROCEDURE XX.TST( IN XOBJ CHAR(32), IN XOBJTYPE CHAR(10), IN XNEWOWN CHAR(10))
LANGUAGE CL
SPECIFIC XX.TST
NOT DETERMINISTIC
NO SQL
CALLED ON NULL INPUT
EXTERNAL NAME 'QSYS/CHGOBJOWN'
PARAMETER STYLE GENERAL;
CALL XX . TST('XX/TBL1','*FILE','GRPFRIENDS');
I get the following error:
External program CHGOBJOWN in QSYS not found
But have confirmed that going to the CL of the terminal emulator and typing QSYS/CHGOBJOWN takes me into the parameter input screen
You are trying to define a command as a program, and that just won't work. A command object (*CMD) and a program object (*PGM) are two different things, and cannot be invoked the same way. All is not lost though. There is a DB2 service that allows you to execute commands. You just have to build the proper command string.
Instead of defining a stored procedure, you can call the existing DB2 service like this:
call qsys2.qcmdexec('CHGOBJOWN OBJ(XX/TBL1) OBJTYPE(*FILE) NEWOWN(GRPFRIENDS)');
There are a whole list of services. Documentation can be found here.

Is there an easier way to discover function parameters in MemSQL?

I need to enumerate all parameters in a MemSQL function. Is there an easier way other than using SHOW CREATE FUNCTION and then parsing the definition? For example, when using MySql, I could use
connection.GetSchema("Procedure Parameters",
new string[] { null, database, structureName });
Unfortunately there isn't really a better approach right now. The GetSchema method from your example makes use of a MySQL metadata table that is currently unsupported in MemSQL.

hierarchy of functions in MatLab

I have been reading someone else's matlab code and I don't know how the code structured. I mean I would like to know the hierarchy of functions, which function uses which function. I am reading the code to figure that out but its taking a lot of time.
So is there any other way I can see this hierarchy without reading the whole thing? To be honest it is starting to get confusing. Maybe MatLab has a built in function for that! I found this:
How can I generate a list of function dependencies in MATLAB?
but this doesn't seem to be helpful!
The MATLAB profiler will show you what functions are called by your code (and much more information to boot) and allow you to click through the hierarchy of function calls. You can either call profile on and then run your code, then call profile off and profile viewer, or you can simply call profile viewer and type a single line of code to run in the edit box at the top.
Use the dependency report provided in MATLAB:
http://www.mathworks.co.uk/help/matlab/matlab_prog/identify-dependencies.html
There are also some tools on the File Exchange, such as fdep.
No idea about a function to show visible or depended-upon functions. However the basic rules are:
1) Only the first function in a .m file (normally has to have the same name as the file itself) is visible outside that file.
2) Any function can see any top level (see 1.) function if the file is in the matlab path. Matlab can show you the path so you know where it's hunting.
3) The order of the path is important, the first instance of a function called foo found in the path will be called. Obviously the current directory is at the top of the path.
3) All functions in a given file can see all other functions in that file.
That's the basics. No doubt there are other rules, and possibly exceptions to this. But that understanding generally serves me well.
Obviously the easiest way to work out which function is being called is to click on it in the editor and open it.
One thing I do is simply place in each function at the beginning fprintf("inside function <name>/n"); and at the end of the function fprintf("leaving function <name>/n"); where <name> is the name of the function.
This will give you a very specific list of which function is being called by which function (based on the order that they appear). Another thing like this would be to place fprintf("function <name1> calling function <name2>/n"); so you can be more explicit about which function is being called by which one.

how to invoke OS system call on DB2?

I'm trying to invoke some C++ code in case a trigger is called inside my db2 DB.
to do so i thought of compiling the C++ code to an executable and to run it as a system call from DB2.
ps: I'm new to DB in general.
thanks in advance !
I think you want to use a DB2 system call:
http://www.ibm.com/developerworks/data/library/techarticle/0303stolze/0303stolze.html
EDIT:
Specifically, it appears that you could just re-use the system call solution referenced in the "Making system calls" section to call an arbitrary command from your trigger:
http://www.ibm.com/developerworks/data/library/techarticle/0303stolze/0303stolze.html#section5
Generally, the from the docs I gather that you will need to call an external UDF (User Defined Function) from your trigger. The UDF itself defines the call to your external program and needs to be created and configured in such a way that DB2 will recognize it.
Here's a PDF that covers UDFs is some detail. The "External UDFs" section on page 453 might be useful.
http://www.redbooks.ibm.com/redbooks/pdfs/sg246503.pdf
This article may also be helpful. It shows a solution for integrating a Java function as a UDF to be called from a trigger.
http://www.ibm.com/developerworks/data/library/techarticle/0205bhogal/0205bhogal.html#download

Calling PL/pgSQL function ignoring results

I like to call a stored PL/pgSQL function the same way as with PERFORM ignoring the results, but from plain SQL. How can i achive this? I'm currently using SELECT to execute the function, but this prints data on the console what i don't need.
I thought about disabling client output for specific SELECT statements, but i can't find any client settings for this. Maybe there's a better way to do this kind of calls.
There is no such functionality in plain sql. What you can do though, is make the function not return anything.
Here is a dirty hack, i just came up with;
background is, i need to call a function, but specify an UPDATE, not a SELECT ...maybe your background is the same...
So I specified my UPDATE like this:
UPDATE sometable_doesnt_matter
SET some_comlumn=some_comlumn
WHERE (select my_function = 1);
And my function always returns the integer 1.
Ofc i'm going to change the code so that it will also work with a SELECT, but right now, as a hotfix, this works for me.