pgsql 中用于创建物化视图的存储过程
Stored procedure in pgsql to create materialized views
我正在尝试在 pgsql 中创建一个存储过程,该过程使用 table 中特定年份的数据创建物化视图。参数将是 table 名称、包含年份的列和年份。
我知道在 SQL 服务器中它会是这样的:
CREATE PROCEDURE createMaterializedView
@tablename varchar(100),
@column varchar(100),
@year integer
AS
BEGIN
DECLARE @return varchar(1000) = 'CREATE MATERIALIZED VIEW view_' + @tablename + '_' + @year + 'AS SELECT * from' + @tablename + 'where'
+ @column + ' = ' + @year
EXECUTE sp_executesql @return
END
在pgsql中,我现在得到的是:
CREATE PROCEDURE createMaterializedView(tablename varchar(100), column varchar (100), year integer)
LANGUAGE 'plpgsql'
AS $$
BEGIN
CREATE MATERIALIZED VIEW "view_" + $tablename + "_" + $year
AS
SELECT * from tablename
WHERE column = year
END;
$$;
感谢任何帮助。
考虑一个动态查询,使用 EXECUTE
.[=12= 将您的标识符(视图名称,table,列)和字面值(年份)格式化为 运行 ]
CREATE OR REPLACE PROCEDURE create_materialized_view (
_tablename varchar(100),
_column varchar(100),
_year integer
)
LANGUAGE 'plpgsql' AS
$proc$
DECLARE
_vw text := 'view_' || _tablename || '_' || _year;
_sql text :=
'CREATE MATERIALIZED VIEW %I AS
SELECT * FROM %I WHERE %I = %L';
BEGIN
EXECUTE format(_sql, _vw, _tablename, _column, _year);
END
$proc$
我正在尝试在 pgsql 中创建一个存储过程,该过程使用 table 中特定年份的数据创建物化视图。参数将是 table 名称、包含年份的列和年份。
我知道在 SQL 服务器中它会是这样的:
CREATE PROCEDURE createMaterializedView
@tablename varchar(100),
@column varchar(100),
@year integer
AS
BEGIN
DECLARE @return varchar(1000) = 'CREATE MATERIALIZED VIEW view_' + @tablename + '_' + @year + 'AS SELECT * from' + @tablename + 'where'
+ @column + ' = ' + @year
EXECUTE sp_executesql @return
END
在pgsql中,我现在得到的是:
CREATE PROCEDURE createMaterializedView(tablename varchar(100), column varchar (100), year integer)
LANGUAGE 'plpgsql'
AS $$
BEGIN
CREATE MATERIALIZED VIEW "view_" + $tablename + "_" + $year
AS
SELECT * from tablename
WHERE column = year
END;
$$;
感谢任何帮助。
考虑一个动态查询,使用 EXECUTE
.[=12= 将您的标识符(视图名称,table,列)和字面值(年份)格式化为 运行 ]
CREATE OR REPLACE PROCEDURE create_materialized_view (
_tablename varchar(100),
_column varchar(100),
_year integer
)
LANGUAGE 'plpgsql' AS
$proc$
DECLARE
_vw text := 'view_' || _tablename || '_' || _year;
_sql text :=
'CREATE MATERIALIZED VIEW %I AS
SELECT * FROM %I WHERE %I = %L';
BEGIN
EXECUTE format(_sql, _vw, _tablename, _column, _year);
END
$proc$