Convert SQL query to stored procedure - stored-procedures

I have this SQL query:
DELETE FROM Table1
WHERE Field1 <current_date - 30
I have been using Firebird for a few weeks. I want to convert this SQL to a Firebird stored procedure.
How to do this please?

Read Language Reference, particularly the chapter about CREATE PROCEDURE statement.
CREATE PROCEDURE foo AS
BEGIN
DELETE FROM Table1
WHERE Field1 < current_date - 30;
END
PS: Converting of such simple query into a stored procedure in Firebid is completely meaningless.

Related

How do I list all procedures that can be used in SQL queries in Informix

In Informix, I can create procedures like these:
CREATE TABLE t (i INT);
CREATE PROCEDURE p1()
RETURNING INT
RETURN 1;
END PROCEDURE;
CREATE PROCEDURE p2()
RETURNING INT
INSERT INTO t VALUES (1);
RETURN 1;
END PROCEDURE;
Now, I can call both procedures using the EXECUTE syntax:
EXECUTE PROCEDURE p1();
EXECUTE PROCEDURE p2();
But I can only execute the first one in a SELECT statement:
-- Works
SELECT p1();
-- Fails with SQL Error [IX000]: Illegal SQL statement in SPL routine.
SELECT p2();
This is documented here in the manual.
I cannot seem to distinguish the two types of procedure from the dictionary, e.g.
SELECT procname, isproc
FROM sysprocedures
WHERE procname IN ('p1', 'p2');
Yields:
|procname|isproc|
|--------|------|
|p1 |f |
|p2 |f |
Other fields from sysprocname are also not helpful. How can I recognise a procedure that contains such "illegal SQL statements"? (I'd like to avoid parsing the sysprocbody contents)

How to write stored procedure for select statment in postgersql

I want to create stored procedure for select statment below is procedure i have created but it giving data ouput blank
CREATE OR REPLACE PROCEDURE public.deactivate_unpaid_accounts()
LANGUAGE 'sql'
AS $BODY$
select * from employees where salary=10000
$BODY$;
CALL deactivate_unpaid_accounts();
Procedures (which weren't available in 9.5 to begin with) are not intended to return result sets.
If you want to return a result, you should use a function in Postgres.
CREATE OR REPLACE FUNCTION public.deactivate_unpaid_accounts()
returns setof employees
LANGUAGE sql
AS $BODY$
select *
from employees
where salary=10000;
$BODY$;
Then use it like this:
select *
from deactivate_unpaid_accounts();

Show table from a resultset into DBeaver (db oracle)

I need to know how to see the result of a query into a stored procedure.
A simple select * from... or any complex query just in the same way I see it after typing it from dbeaver SQL section.
The stored procedure is:
procedure TABLAS(oid in number, rec out stbienes%RowType)
As
Begin
Select * into rec from stbienes where oid_Art= oid;
End;
I'm using DBeaver 3.0.1 and Oracle XE.
Thank you very much.

How to insert return value from last EXECUTE PROCEDURE in INFORMIX

This should be relatively easy but I'm new with informix.
I have a stored proc that I am calling with EXECUTE PROCEDURE. The proc is roughly as follows (but with a ton more columns):
CREATE PROCEDURE MYPROC (
foo int,
bar int
) returning int;
How can I use that int from the Informix prompt to use it in a subsequent insert?
This is roughly what I would like to do. I do not have the option to change the insides of the proc.
> EXECUTE PROCEDURE MYPROC(foo,bar);
(expression)
4104
1 row(s) retrieved.
> INSERT INTO MYTABLE(val) VALUES(I_DONT_KNOW_WHAT_GOES_HERE);
I have already tried this
> INSERT INTO MYTABLE(val) VALUES(
EXECUTE PROCEDURE MYPROC(foo,bar)
);
and failed!
You can use the output of a FUNCTION or PROCEDURE in most places where an expression is expected. So this should be all you need:
> INSERT INTO MYTABLE(val) VALUES( MYPROC(foo,bar) );
Update
Further discussion in comments has identified that the procedure has some DML in it, so the solution above does not work. (Informix does not permit this, presumably to avoid endless looping and recursion).
You might have more luck with this (untested):
INSERT INTO mytable
SELECT a.result FROM TABLE(myproc('foo','bar')) AS a(result)
But it might also be refused for the same reason as the original.

How to submit the query by stored procedure in MySQL?

Hello I am trying to automate my history tracking procedure in MySQL.
The procedure should update a table and create another using uid as a name.
CREATE PROCEDURE `InsertQueryStore`( u VARCHAR(128), ID INT, q VARCHAR(1024) )
BEGIN
INSERT INTO querystore(`qID`, `qstring`, `user`) VALUES(ID, q, u); # this works
# DROP TABLE IF EXIST ID ; //Can I do something like this?
# CREATE TABLE ID q; // The q is a query string which should return results into to table ID
END;
then I would like to call as:
Call InsertQueryStore("myname", 100, "select * from mydb.table limit 10")
What is the proper way to use the varchar variable in the procedure?
Thank you beforehand.
Arman.
I think the way to go with that would be using Dynamic SQL.
MySQL does not support dynamic SQL in the way some DBMS do, but it does have the PREPARE/EXECUTE methods for creating a query and executing it. See if you can use them within your stored procedure.
Something like:
CREATE PROCEDURE `InsertQueryStore`( u VARCHAR(128), ID INT, q VARCHAR(1024) )
BEGIN
INSERT INTO querystore(`qID`, `qstring`, `user`) VALUES(ID, q, u);
PREPARE stmt FROM "DROP TABLE IF EXIST ?";
EXECUTE stmt USING ID;
DEALLOCATE PREPARE stmt;
/* etc */
END;
If you find you can't use the parameterised version with '?' in that context, just use CONCAT() to assemble it with the actual value in the string as it is already known at that stage.
There is a reasonable article about it here, mentioned in a previous SO post.

Resources