SELECT update_employee(1, 'John Smith', 42, 200000) - update previous record SELECT update_employee(NULL, 'John', 42, 100000) - insert (returns 1) In both cases you'll get the ID of modified record back. Now if you call this function with null as ID it will insert the data, otherwise data will be found by specified id and updated. SET name = p_name, age = p_age, salary = p_salary INSERT INTO employee (name, age, salary) VALUES (p_name, p_age, p_salary) RETURNING id INTO p_id The function itself might look like: CREATE OR REPLACE FUNCTION update_employee(p_id INT, p_name VARCHAR(255), p_age INT, p_salary DECIMAL) Postgres supports many languages for that, but the most popular one is plpgsql. Third, enter the above code int the query tool and click the Execute button to create the get_film_count function.Ok, the first thing you should do is design that function for insert/update data. Second, open the query tool by selecting Tools > Query Tool. To execute the create function statement, you can use any PostgreSQL client tool including psql and pgAdmin 1) Creating a function using pgAdminįirst, launch the pgAdmin tool and connect to the dvdrental sample database. At the end of the block, use the return statement to return the film_count.
0 Comments
Leave a Reply. |