before creating function, check your database has PL/PgSQL here
* create function
simple pl/pgsql function, adding row to the test table
CREATE OR REPLACE FUNCTION pg_function_insert(
eno INTEGER) RETURNS VARCHAR AS $$
DECLARE
eno_end INTEGER := eno + 5;
BEGIN
FOR num IN eno.. eno_end LOOP
insert into test values (num,'solai');
END LOOP;
RETURN 'rows success fully inserted' ;
END;
$$ LANGUAGE plpgsql;
* execute function
SELECT pg_function_insert(100) AS function_output
we created function with function name of pg_function_insert,input parameter is Integer and function returns String.
table object test has two column eid with integer and ename with varchar data type.
in for loop initial value is input value given by user while executing function ( in our example 100 ), loop will iterate five times because loop end value is eno+5.
in create function statement we have added $$, This is PostgreSQL dollar-quoting. When used in place of the usual single-quotation mark quoting ('), you don't have to escape single quotation marks within the body of the function. This makes them much easier to read. more about $$
* now you can run a query to view the result
select * from test
eid|ename
100|solai
101|solai
102|solai
103|solai
104|solai
105|solai
| is column separator
All about data processing & analytics. Open source database,No-SQL and Hadoop. Discussed issues/solution which I've got during my experience..
Subscribe to:
Post Comments (Atom)
Labels
- #agriculture (1)
- #ai (5)
- #pdf (1)
- Big Data (30)
- blogging (3)
- data analytics (5)
- data science (7)
- Deep Learning (4)
- Hadoop (28)
- Hadoop Eco System (27)
- hdfsCommands (4)
- Hive (5)
- IssueSolution (4)
- jobs (3)
- links (1)
- Machine Learning (4)
- mahout (2)
- MapReduce (1)
- MongoDB (6)
- MySQL (4)
- PlpgSQL (8)
- postgres (6)
- PostgreSQL (53)
- R (20)
- RHadoop (2)
- search keywords (1)
- social (2)
- spark (3)
- twitter (6)
- usecase (2)
- visualization (7)
- weka (1)
No comments:
Post a Comment