Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare variables in pgAdmin

If I am in the psql terminal then I can declare and use a variable like this:

\set message_id soifsdaofisd.gmail.com;
select * from emails where message_id = ':message_id';

How can I do this in pgAdmin?

I get an error when ever I try this in pgAdmin:

ERROR: syntax error at or near "CALAdkA4YC0" LINE 3: set message_id soifsdaofisd.gmail.com.

like image 665
dagda1 Avatar asked May 09 '14 09:05

dagda1


People also ask

How do I declare a variable in PostgreSQL?

The general syntax of a variable declaration is: name [ CONSTANT ] type [ COLLATE collation_name ] [ NOT NULL ] [ { DEFAULT | := | = } expression ]; The DEFAULT clause, if given, specifies the initial value assigned to the variable when the block is entered.

How do I manually enter data in pgAdmin 4?

Expand your table properties by clicking on it in the pgAdmin4 legend. Right-click on 'Constraints', select 'Create' --> 'Primary Key'to define a primary key column. On the Data Output tab at the bottom of the table below the last row, there will be an empty row where you can enter new data in an excel-like manner.

How do I declare an integer variable in PostgreSQL?

Example - Declaring a constantDECLARE vSiteID CONSTANT integer := 50; This would declare a constant called vSiteID as an integer data type and assign an initial value of 50. Because this variable is declared using the CONSTANT keyword, you can not change its value after initializing the variable.

How do I assign a selection to a variable in PostgreSQL?

In PostgreSQL, the select into statement to select data from the database and assign it to a variable. Syntax: select select_list into variable_name from table_expression; In this syntax, one can place the variable after the into keyword.


2 Answers

When trying to run scripts in pgAdmin I wanted the something similar where i wanted to store a value to use across multiple queries. This is what I found which is similar to SQL

set session vars.batch_id = '82';

select * from batches where batch_id = current_setting('vars.batch_id')::int;

like image 163
FPVjeary Avatar answered Oct 12 '22 05:10

FPVjeary


\set is a feature of psql (the interactive command line terminal) and not available in pgAdmin.

PostgreSQL does not normally use variables in plain SQL. You would use PL/pgSQL code in an anonymous block (DO statement) or in a function.

However, you can (ab)use customized options, for server-side "variables", independent of the client in use:

SET foo.test = 'SELECT bar FROM baz';
SELECT current_setting('foo.test');

Details in this related answer:

  • User defined variables in PostgreSQL

There was also pgScript, a local scripting extension of the pgAdmin3 query tool, where you could use local variables, comparable to what you can do in psql. The manual:

You can run pgScript scripts by selecting Execute pgScript from the Query menu instead of Execute, or you press the Execute pgScript toolbar button, or you press the F6 function key.

But pgAdmin3 is unmaintained now, and pgAdmin4 does not include pgScript. Wasn't all that useful.

like image 35
Erwin Brandstetter Avatar answered Oct 12 '22 06:10

Erwin Brandstetter