使用随机名称在 postgresql 中创建临时 table
Creating temporary table in postgresql with random name
我使用此命令从 Select 语句
创建临时 table
Create TEMP Table myTable As Select * From Table1
我不知道如何做同样的事情,但分配了一个随机名称而不是“myTable”
我有一个创建随机字符串的函数,我可以将值分配给一个变量,但是我如何使用这个变量作为我的新临时文件的名称 table ?
谢谢
您可以使用动态 SQL。为此,代码必须是函数的一部分或在 do
块中:
do $$
begin
EXECUTE format('Create TEMP Table %s As Select * From Table1',(select get_random_name()));
end $$;
记录临时 table 名称当然是您的责任,以便您稍后查询...
话虽这么说,但请注意 doc 提到了
Temporary tables are automatically dropped at the end of a session, or
optionally at the end of the current transaction
所以也许您可以一直使用相同的名称,因为其他客户端(事务)不会看到其他客户端(事务)table创建的临时文件
我使用此命令从 Select 语句
创建临时 tableCreate TEMP Table myTable As Select * From Table1
我不知道如何做同样的事情,但分配了一个随机名称而不是“myTable” 我有一个创建随机字符串的函数,我可以将值分配给一个变量,但是我如何使用这个变量作为我的新临时文件的名称 table ?
谢谢
您可以使用动态 SQL。为此,代码必须是函数的一部分或在 do
块中:
do $$
begin
EXECUTE format('Create TEMP Table %s As Select * From Table1',(select get_random_name()));
end $$;
记录临时 table 名称当然是您的责任,以便您稍后查询...
话虽这么说,但请注意 doc 提到了
Temporary tables are automatically dropped at the end of a session, or optionally at the end of the current transaction
所以也许您可以一直使用相同的名称,因为其他客户端(事务)不会看到其他客户端(事务)table创建的临时文件