How to declare a variable in a PostgreSQL query

后端 未结 12 1289
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 09:34

How do I declare a variable for use in a PostgreSQL 8.3 query?

In MS SQL Server I can do this:

DECLARE @myvar INT
SET @myvar = 5

SELECT *
FROM somew         


        
相关标签:
12条回答
  • 2020-11-22 09:40

    There is no such feature in PostgreSQL. You can do it only in pl/PgSQL (or other pl/*), but not in plain SQL.

    An exception is WITH () query which can work as a variable, or even tuple of variables. It allows you to return a table of temporary values.

    WITH master_user AS (
        SELECT
          login,
          registration_date
        FROM users
        WHERE ...
    )
    
    SELECT *
    FROM users
    WHERE master_login = (SELECT login
                          FROM master_user)
          AND (SELECT registration_date
               FROM master_user) > ...;
    
    0 讨论(0)
  • 2020-11-22 09:40

    Using a Temp Table outside of pl/PgSQL

    Outside of using pl/pgsql or other pl/* language as suggested, this is the only other possibility I could think of.

    begin;
    select 5::int as var into temp table myvar;
    select *
      from somewhere s, myvar v
     where s.something = v.var;
    commit;
    
    0 讨论(0)
  • 2020-11-22 09:42

    True, there is no vivid and unambiguous way to declare a single-value variable, what you can do is

    with myVar as (select "any value really")
    

    then, to get access to the value stored in this construction, you do

    (select * from myVar)
    

    for example

    with var as (select 123)    
    ... where id = (select * from var)
    
    0 讨论(0)
  • 2020-11-22 09:45

    Dynamic Config Settings

    you can "abuse" dynamic config settings for this:

    -- choose some prefix that is unlikely to be used by postgres
    set session my.vars.id = '1';
    
    select *
    from person 
    where id = current_setting('my.vars.id')::int;
    

    Config settings are always varchar values, so you need to cast them to the correct data type when using them. This works with any SQL client whereas \set only works in psql

    The above requires Postgres 9.2 or later.

    For previous versions, the variable had to be declared in postgresql.conf prior to being used, so it limited its usability somewhat. Actually not the variable completely, but the config "class" which is essentially the prefix. But once the prefix was defined, any variable could be used without changing postgresql.conf

    0 讨论(0)
  • 2020-11-22 09:48

    It depends on your client.

    However, if you're using the psql client, then you can use the following:

    my_db=> \set myvar 5
    my_db=> SELECT :myvar  + 1 AS my_var_plus_1;
     my_var_plus_1 
    ---------------
                 6
    

    If you are using text variables you need to quote.

    \set myvar 'sometextvalue'
    select * from sometable where name = :'myvar';
    
    0 讨论(0)
  • 2020-11-22 09:49

    Here is an example using PREPARE statements. You still can't use ?, but you can use $n notation:

    PREPARE foo(integer) AS
        SELECT  *
        FROM    somewhere
        WHERE   something = $1;
    EXECUTE foo(5);
    DEALLOCATE foo;
    
    0 讨论(0)
提交回复
热议问题