如何使用变量作为别名

时间:2019-04-02 09:01:35

标签: postgresql

我想使用变量作为架构名称的别名。 我的目标是在归档其他客户端的情况下更改变量的值,这样我就不会触及整个sql代码。我不知道如何实现它。有没有办法做到这一点。预先感谢

BEGIN;

    -- Set clientid
    -- \set thisClient 42951;
    \set thisClient 43527;

    \set archive_schema data_archive_230_test;

    -- create new schema
    create schema :archive_schema;

    select * into :archive_schema.sometable1 from public.sometable1 where account_id = :thisClient;

    select * into :archive_schema.sometable2 from public.sometable2 where id = :thisClient;

    ...more tables here to be archive

COMMIT;

当我尝试执行上面的sql时出现错误

错误:“或”附近的语法错误。 第1行:.sometable1

1 个答案:

答案 0 :(得分:0)

替代方法是设置默认架构

BEGIN;

    -- Set clientid
    -- \set thisClient 42951;
    \set thisClient 43527;

    \set archive_schema data_archive_230_test;

    -- create new schema
    create schema :archive_schema;
    -- setting default schema
    SET search_path TO :archive_schema;

    select * into sometable1 from public.sometable1 where account_id = :thisClient;

    select * into sometable2 from public.sometable2 where id = :thisClient;

    ...more tables here to be archive

COMMIT;