How to refer to a value in a PostgreSQL script?
NickName:John Barton Ask DateTime:2021-08-07T07:49:47

How to refer to a value in a PostgreSQL script?

I'd like to use the max value to feed the creation of a sequence. For instance, I am getting the max id value and I'd like to use this value:

DO $$
DECLARE   
    min_value Integer;
BEGIN
    SELECT MAX(tmp.id)+1 into min_value FROM tmp;
    -- raise notice 'Value: %', min_value;
    
    CREATE SEQUENCE id_seq_tmp
    INCREMENT 1
    START :tmp.min_value --Getting error: syntax error at or near ":"
    MINVALUE :tmp.min_value;
END $$;

How do I refer to the max value and pass to the sequence creation? I am using psql (PostgreSQL) 13.3.

Copyright Notice:Content Author:「John Barton」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/68688417/how-to-refer-to-a-value-in-a-postgresql-script

More about “How to refer to a value in a PostgreSQL script?” related questions

How to refer to a value in a PostgreSQL script?

I'd like to use the max value to feed the creation of a sequence. For instance, I am getting the max id value and I'd like to use this value: DO $$ DECLARE min_value Integer; BEGIN SELEC...

Show Detail

PostgreSQL trigger to make default value refer to another table

PostgreSQL default values cannot contain variables or refer to any other columns in the table, or in a different table. However, is it possible to use a trigger to create a "Default value" that will

Show Detail

How to refer script in sitemaster

I know in mvc the site master page shares resource(html elements) with aspx file.Like this it is possible to refer the script file in site master that will share with aspx?If so how to do that? In

Show Detail

How to get materialized views that refer to a table in postgresql

In postgresql it is possible to get all views that refer to a table by simple sql thanks to information_schema.views table. But I need an equivalent sql to get materialized views that refer to the ...

Show Detail

Refer to the month and year in PostgreSQL

I'm trying to pass a script that is in mysql to PostgreSQL, but there is a sentence this in mysql that generates me problems: $q1="select * from noticias where month(fecha)='".$elmes."' and year(f...

Show Detail

How to refer to a javascript variable in a value field?

I'd like to refer to a variable ("special") in field later in the same script. I've gotten the variable to display with alert boxes and document.write, but don't now how to make to apply its value ...

Show Detail

USQL Execution and refer another script

Could you guys help with following: How can we execute script usql script stored in ADL store using ADF. What is standard practice of storing script? Currently I don't see a way to refer script f...

Show Detail

Refer to the current directory in a shell script

How do I refer to the current directory in a shell script? So I have this script which calls another script in the same directory: #! /bin/sh #Call the other script ./foo.sh # do something ......

Show Detail

How to stop SQL script execution for PostgreSQL?

There is DDL script in PostgreSQL that creates tables. For example, if first table exists, How to stop SQL script execution for PostgreSQL (within script)?

Show Detail

PostgreSQL: Refer to specific nth column of WITH created temponary table

Supposing I have a temporary table created using WITH clause as follows: WITH temporary_table AS ( VALUES ('First', 1), ('Second', 2), ('Third', 3)) Is it possible to ref...

Show Detail