Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2016
    Posts
    13

    Unanswered: Running queries from PgAdmin

    Hi,

    How can i write a query with declaring some variables in phAdmin 3.

    When I declare the variables in function they work fine but I would like to test my queries so it would be convinient if I can declare and set varaibles and use them to test my query before turing that query to function.
    Code:
    declare v_id integer; 
    v_id = 100
    
    select * from test where i = v_id
    
    Error:
    ERROR:  syntax error at or near "integer"
    LINE 1: declare v_id integer; 
                         ^
    ********** Error **********
    
    ERROR: syntax error at or near "integer"
    SQL state: 42601
    Character: 14

  2. #2
    Join Date
    Feb 2016
    Posts
    1
    You cannot do this.

    You must use the DO statement, and since functions cannot return inside them a SELECT you must doing something like this:
    Code:
    DO $$DECLARE v_variable INT;
    BEGIN
    v_variable:=70;
    
    DROP TABLE IF EXISTS mytest ;
    
    CREATE TEMP TABLE mytest (LIKE mytable);
    
    INSERT INTO mytest
    SELECT * FROM mytable where age = v_variable;
      
    
    END$$;
    
    
    SELECT * FROM mytest;

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •