将SELECT的结果连接到EXECUTE中

时间:2019-04-15 12:12:20

标签: postgresql psql

我正在尝试在匿名代码块(class Team { //empty class used for binding all the team classes } class BlueTeam extends Team { public static List<String> players = new ArrayList<String>(); } class PlayerData { Team playerTeam; public PlayerData(Team tm){ playerTeam = tm; } playerTeam.players // I want to access any subclass that is stored into this "myTeam" object and access its players list } class createData { List<PlayerData> data = new ArrayList<PlayerData>(); // this is what I've tried but I get a null exception Team team = new BlueTeam(); data.add(new PlayerData(team)); } )中使用EXECUTE根据定义的备份重新创建视图,但是我无法使其正常工作。这大概是我到目前为止所拥有的:

DO $$ ... BEGIN... END$$

带有视图定义备份的表是这样制作的

EXECUTE 'CREATE VIEW image AS (' || (SELECT definition FROM tmp_view_backup WHERE viewname = 'image') || ')';

似乎是select导致我在创建字符串时遇到一些问题,因为结果根本不符合我的预期:

CREATE TABLE  tmp_view_backup
AS
(select viewname,definition from pg_views
WHERE schemaname = 'public'
);

这是什么?空行?如果我放弃选择,我会得到一个正常的答案(例如字符串长度为postgres=# select char_length('CREATE VIEW image AS (' || (SELECT definition FROM tmp_view_backup WHERE viewname = 'image') || ')') as f ; f --- (1 row) )。

1 个答案:

答案 0 :(得分:3)

您可以使用PSQL \gexec参数。

SELECT format('CREATE VIEW image AS  %s ', definition)
FROM tmp_view_backup 
WHERE viewname = 'image';\gexec  

或在DO块中

DO $$
BEGIN

EXECUTE format('CREATE VIEW image AS  %s ', definition)
FROM  tmp_view_backup 
WHERE viewname = 'image';
END $$;