plpgsql

PostgreSQL IF-THEN-ELSE control structure

左心房为你撑大大i 提交于 2019-12-03 12:11:53
Why do I always get the following error from Postgres? syntax error at or near "IF" I read PostgreSQL: Documentation: 8.3: Control Structures . First I tried to execute a difficult query (with subquery), but then I tried to execute a simple one like this: IF 2 <> 0 THEN select * from users; END IF; The error is still the same. What am I doing wrong? IF 2 <> 0 THEN select * from users; END IF; You cannot use PL/pgSQL statements outside plpgsql functions. And if this fragment is from plpgsql function, then it is nonsense too. You cannot directly return result of query like T-SQL does. CREATE OR

how to execute pgsql script in pgAdmin?

删除回忆录丶 提交于 2019-12-03 10:43:46
I want to execute some pgScript directly from the pgAdmin editor UI. FOR i IN 1..10 LOOP PRINT i; -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop END LOOP; But I always got [ERROR ] 1.0: syntax error, unexpected character I also tried to wrap the code with do$$...$$ , but does not solve the problem. Vivek S. apart from Clodoaldo Neto's Answer .You can try this also DO $$ BEGIN FOR i IN 1..10 LOOP RAISE NOTICE '%', i; -- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop END LOOP; END $$ There is no PRINT command. Use raise notice instead. create function f()

Postgresql JDBC Table Valued Parameters

回眸只為那壹抹淺笑 提交于 2019-12-03 09:17:00
问题 MSSQL has a great feature called Table Valued Parameters . It allows you to pass a table of a custom data to stored procedures and functions. I was wondering what is the equivalent in PostgreSQL , if one exists, using JDBC? I know about the option of passing arrays as function parameters, but that seems limited to PostgreSQL data types. Consider the following PL/pgSQL code: CREATE TYPE number_with_time AS( _num float, _date timestamp ); and this function header: CREATE OR REPLACE FUNCTION

Robust approach for building SQL queries programmatically

ぐ巨炮叔叔 提交于 2019-12-03 09:03:43
I have to resort to raw SQL where the ORM is falling short (using Django 1.7). The problem is that most of the queries end up being 80-90% similar. I cannot figure out a robust & secure way to build queries without violating re-usability. Is string concatenation the only way out, i.e. build parameter-less query strings using if-else conditions, then safely include the parameters using prepared statements (to avoid SQL injection). I want to follow a simple approach for templating SQL for my project instead of re-inventing a mini ORM. For example, consider this query: SELECT id, name, team, rank

What's the easiest way to return a recordset from a PostgreSQL stored procedure?

醉酒当歌 提交于 2019-12-03 06:18:42
问题 I simply have a table that contains a list of countries and their ISO country codes. I'm wrapping the query in a stored procedure (aka function) such as: CREATE OR REPLACE FUNCTION get_countries( ) RETURNS setof record AS $$ SELECT country_code, country_name FROM country_codes $$ LANGUAGE sql; The error I am getting is: ERROR: a column definition list is required for functions returning "record" I know that I can define a TYPE and then loop through the recordset like a cursor, but IIRC there

How can I execute pl/pgsql code without creating a function?

送分小仙女□ 提交于 2019-12-03 05:30:29
问题 With SQL Server, I can execute code ad hoc T-SQL code with full procedural logic through SQL Server Management Studio, or any other client. I've begun working with PostgreSQL and have run into a bit of a difference in that PGSQL requires any logic to be embedded in a function. Is there a way to execute PL/PGSQL code without creating an executing a function? 回答1: Postgres 9 DO $$ -- declare BEGIN /* pl/pgsql here */ END $$; 回答2: No, not yet. Version 9.0 (still alpha) will have this option (do)

How to use a record type variable in plpgsql?

天涯浪子 提交于 2019-12-03 05:21:53
How can I use query result stored into a record type variable for another query within the same stored function? I use Postgres 9.4.4. With a table like this: create table test (id int, tags text[]); insert into test values (1,'{a,b,c}'), (2,'{c,d,e}'); I wrote a function (simplified) like below: CREATE OR REPLACE FUNCTION func(_tbl regclass) RETURNS TABLE (t TEXT[], e TEXT[]) LANGUAGE plpgsql AS $$ DECLARE t RECORD; c INT; BEGIN EXECUTE format('SELECT id, tags FROM %s', _tbl) INTO t; SELECT count(*) FROM t INTO c; RAISE NOTICE '% results', c; SELECT * FROM t; END $$; ... but didn't work:

Making emacs to highlight postgresql syntax by default

你说的曾经没有我的故事 提交于 2019-12-03 05:13:42
I use emacs for editing my sql code. I work 99% of time on postgresql plpgsql code. All my files with extension .sql contain postgresql. I'm curious is there a way to set sql-highlight-postgres-keywords SQL highlighting default instead of ANSI SQL, because it's pretty annoying to switch mode every time I open a file. Mario F Usually in emacs, if you want to change the settings every time some mode is opened, you use a hook. Something similar to this should work: (add-to-list 'auto-mode-alist '("\\.psql$" . (lambda () (sql-mode) (sql-highlight-postgres-keywords)))) If you need to work with

How do I enable the PostgreSQL function profiler?

本小妞迷上赌 提交于 2019-12-03 05:11:37
问题 This took me a while to figure out and I found the answer on a foreign-language wiki a number of weeks ago, and it was very helpful, so I thought I would share. 回答1: On PostgreSQL 8.3 on Win32, the profiling plugin is installed by default, but not loaded. Just execute this SQL: LOAD '$libdir/plugins/plugin_profiler.dll'; SET plpgsql.profiler_tablename = 'bazzybar'; ...and then when you want to profile some code, drop table if exists bazzybar; -- reset the profiling stats select my_function

plpgsql: calling a function with 2 OUT parameters

点点圈 提交于 2019-12-03 01:35:50
I'm trying to fetch to values from a plpgsql function with 2 OUT paramenters but I have some problem. These are the functions: CREATE OR REPLACE FUNCTION get_test(OUT x text, OUT y text) AS $$ BEGIN x := 1; y := 2; END; $$ LANGUAGE plpgsql; ---------------------------------------------------------------- CREATE OR REPLACE FUNCTION get_test_read() RETURNS VOID AS $$ DECLARE xx text; yy text; BEGIN SELECT get_test() INTO xx, yy; RAISE INFO 'x: <%>', xx; RAISE INFO 'y: <%>', yy; END; $$ LANGUAGE plpgsql; The output of the command: select get_test_read(); INFO: x: <(1,2) INFO: y: <> get_test_read