How can I pass a variable to a sql statement inside a function
I would like to create a function that returns the rowcount from a table based on the value I pass in to the function. Like this: CREATE OR REPLACE FUNCTION get_my_rowcount(tblname VARCHAR(50),id INT) RETURN INT AS BEGIN RETURN ( CASE WHEN tblname = 'table1' THEN (SELECT COUNT(*) FROM table1 WHERE my_id = id) WHEN tblname = 'table2' THEN (SELECT COUNT(*) FROM table2 WHERE my_id = id) WHEN tblname = 'table3' THEN (SELECT COUNT (*) FROM table3 WHERE my_id = id) ELSE 0 END ); END; When I run this I get the following error: ROLLBACK: column "id" does not exist
0
Comments
You set the variable like this
\set id 123123
select * from test where id = :id;
Hope this helps.
Eugenia