how can I declare a variable in postgresql?

0

I need to declare a variable timestamp in postgresql but when I try it it gives me syntax error, if someone could give me the code of how to declare and assign value to a variable it would be very helpful, thanks

    
asked by Andy Montalvo 17.05.2018 в 18:05
source

2 answers

0

To declare a variable is:

CREATE TABLE prueba (
    "Fecha" timestamp(6) with time zone
)

To add / register a record:

INSERT INTO prueba VALUES ('2018-09-29 19:10:25-07');
    
answered by 29.09.2018 в 17:53
0

In this way you declare variables:

user_id integer;
quantity numeric(5);
url varchar;
myrow tablename%ROWTYPE;
myfield tablename.columnname%TYPE;
arow RECORD;

If you want to declare a timestamp variable for example in a function in this way, it can help you:

CREATE OR REPLACE  FUNCTION test3() RETURNS void
    LANGUAGE plpgsql
    AS $$
DECLARE
_currenttime timestamp := now();
BEGIN
copy (SELECT * FROM table1 WHERE createdtime < _currenttime - INTERVAL '1 days') TO '/tmp/table1.csv';
END
$$;

To execute the function:

Select test3();

Greetings

    
answered by 30.09.2018 в 04:55