plpgsql

PostgreSQL trigger not returning anything

久未见 提交于 2019-12-21 17:35:50
问题 I have a PostgreSQL trigger on create that basically redirects inserts into sub-tables. Once I insert the record, I want to ABORT the request as to avoid duplicate data. The only way (that I know of) to do this is to return NULL in the trigger. The problem is that I need the record to be returned so I can get the ID. If I return NULL , I get ... NULL . Any idea how I can have a trigger abort an operation while still returning something other than NULL ? 回答1: Your question leaves room for

Generate a random number of non duplicated random number in [0, 1001] through a loop

回眸只為那壹抹淺笑 提交于 2019-12-21 09:26:40
问题 I need to generate a random number of non duplicated random number in plpgsql. The non duplicated number shall fall in the range of [1,1001]. However, the code generates number exceeding 1001. directed2number := trunc(Random()*7+1); counter := directed2number while counter > 0 loop to_point := trunc((random() * 1/directed2number - counter/directed2number + 1) * 1001 +1); ... ... counter := counter - 1; end loop; 回答1: If I understand right You need a random number ( 1 to 8 ) of random numbers.

PostgreSQL custom exceptions?

天大地大妈咪最大 提交于 2019-12-21 05:57:24
问题 In Firebird we can declare custom exceptions like so: CREATE EXCEPTION EXP_CUSTOM_0 'Exception: Custom exception'; these are stored at the database level. In stored procedures, we can raise the exception like so: EXCEPTION EXP_CUSTOM_0 ; Is there an equivalent in PostgreSQL ? 回答1: No, not like this. But you can raise and maintain your own exceptions, no problem: CREATE TABLE exceptions( id serial primary key, MESSAGE text, DETAIL text, HINT text, ERRCODE text ); INSERT INTO exceptions

Idempotent PostgreSQL DDL scripts

♀尐吖头ヾ 提交于 2019-12-21 04:55:30
问题 I'm looking for a way to script postgreSQL schema changes in an idempotent manner. In MSSQL I could do something like this: if(not exists(select * from information_schema.columns where table_name = 'x' and column_name = 'y')) begin alter table x add y int end go PostgreSQL doesn't seem to allow ad-hoc pl/pgsql in the same way MSSQL does with T-SQL so I can't use control structures in a SQL script and run it with psql -f x.sql. I know PostgreSQL will throw an error if the object already exists

how to execute pgsql script in pgAdmin?

試著忘記壹切 提交于 2019-12-21 03:35:23
问题 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. 回答1: 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

Making emacs to highlight postgresql syntax by default

吃可爱长大的小学妹 提交于 2019-12-20 17:41:31
问题 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. 回答1: 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 '("

Check for integer in string array

寵の児 提交于 2019-12-20 07:32:06
问题 I am trying to check a string array for existence of a converted integer number. This sits inside of a procedure where: nc_ecosite is an integer variable current_consite is a string array ecosite is an integer current_ecosite_nc is double IF to_char(nc_ecosite, '999') IN (select current_consite from current_site_record where current_ecosite_nc::integer = nc_ecosite) THEN ecosite := nc_ecosite; The result always comes from the ELSIF that follows the first IF . This occurs when nc_ecosite is in

Check for integer in string array

不问归期 提交于 2019-12-20 07:31:23
问题 I am trying to check a string array for existence of a converted integer number. This sits inside of a procedure where: nc_ecosite is an integer variable current_consite is a string array ecosite is an integer current_ecosite_nc is double IF to_char(nc_ecosite, '999') IN (select current_consite from current_site_record where current_ecosite_nc::integer = nc_ecosite) THEN ecosite := nc_ecosite; The result always comes from the ELSIF that follows the first IF . This occurs when nc_ecosite is in

Passing the table as a parameter

本秂侑毒 提交于 2019-12-20 06:47:38
问题 I have to convert from lat and long to geom to use PostGIS. My problem, I have various tables from different locations and I want to pass the table as a parameter to the function. I'm trying this: CREATE or REPLACE FUNCTION convert_from_lon_lat(float,float,character varying) RETURNS integer AS $$ select id from $3 as vertices order by vertices.geom <-> ST_SetSrid(ST_MakePoint($1,$2),4326) LIMIT 1; $$ LANGUAGE SQL; but I get a syntax error. EDIT1: So I changed the previous code to this: CREATE

Plpgsql: How can i assign value to variable at declaration section?

帅比萌擦擦* 提交于 2019-12-20 06:30:04
问题 For example. --Assigning value to variable in function as a parameter. create or replace function f1(number int :=1) --This method is not working to me. or --Assigning values to variables at declaration section. declare number int :=1; -- Here i need to assign the value to number but its not working. name varchar :='xyz'; 回答1: Here is how you can do it: create or replace function f1(my_number int default 1) or declare my_number int :=1; Look at declaration documentation 回答2: Assign value,