dynamic-sql

Update multiple columns in a trigger function in plpgsql

[亡魂溺海] 提交于 2019-11-26 16:57:10
问题 Given the following schema: create table account_type_a ( id SERIAL UNIQUE PRIMARY KEY, some_column VARCHAR ); create table account_type_b ( id SERIAL UNIQUE PRIMARY KEY, some_other_column VARCHAR ); create view account_type_a view AS select * from account_type_a; create view account_type_b view AS select * from account_type_b; I try to create a generic trigger function in plpgsql, which enables updating the view: create trigger trUpdate instead of UPDATE on account_view_type_a for each row

PostgreSQL - dynamic value as table name [duplicate]

泄露秘密 提交于 2019-11-26 16:41:08
问题 Possible Duplicate: Postgres Dynamic Query Function I wish to use the returned string from the query below as a table name for other query. SELECT 'backup_' || TO_CHAR(CURRENT_DATE,'yyyy-mm-dd') as you can see it returns a string. I wish to use it as an input for another query, e.g. CREATE TABLE (SELECT 'backup_' || TO_CHAR(CURRENT_DATE,'yyyy-mm-dd')) AS * SELECT FROM backup Can it be done? Any clue how? 回答1: You will need to use the PL/PgSQL EXECUTE statement, via a DO block or PL/PgSQL

Why cannot I use bind variables in DDL/SCL statements in dynamic SQL?

不想你离开。 提交于 2019-11-26 16:39:24
I am trying to execute an SQL command within dynamic SQL with bind variables: -- this procedure is a part of PL/SQL package Test_Pkg PROCEDURE Set_Nls_Calendar(calendar_ IN VARCHAR2) IS BEGIN EXECUTE IMMEDIATE 'ALTER SESSION SET NLS_CALENDAR = :cal' USING IN calendar_; END Set_Nls_Calendar; Then on the client side, I am trying to invoke the procedure: Test_Pkg.Set_Nls_Calendar('Thai Buddha'); But this get's me ORA-02248: invalid option for ALTER SESSION . And my question is: Why cannot I use bind variables in DDL/SCL statements in dynamic SQL? Bind variables are not allowed in DDL statements.

Define table and column names as arguments in a plpgsql function?

怎甘沉沦 提交于 2019-11-26 16:36:19
It must be simple, but I'm making my first steps into Postgres functions and I can't find anything that works... I'd like to create a function that will modify a table and / or column and I can't find the right way of specifying my tables and columns as arguments in my function. Something like: CREATE OR REPLACE FUNCTION foo(t table) RETURNS void AS $$ BEGIN alter table t add column c1 varchar(20); alter table t add column c2 varchar(20); alter table t add column c3 varchar(20); alter table t add column c4 varchar(20); END; $$ LANGUAGE PLPGSQL; select foo(some_table) In another case, I'd like

SQL update fields of one table from fields of another one

こ雲淡風輕ζ 提交于 2019-11-26 15:22:50
问题 I have two tables: A [ID, column1, column2, column3] B [ID, column1, column2, column3, column4] A will always be subset of B (meaning all columns of A are also in B ). I want to update a record with a specific ID in B with their data from A for all columns of A . This ID exists both in A and B . Is there an UPDATE syntax or any other way to do that without specifying the column names, just saying "set all columns of A" ? I'm using PostgreSQL, so a specific non-standard command is also

How to cleanse dynamic SQL in SQL Server — prevent SQL injection

只谈情不闲聊 提交于 2019-11-26 14:36:58
问题 We have a ton of SQL Server stored procedures which rely on dynamic SQL. The parameters to the stored procedure are used in a dynamic SQL statement. We need a standard validation function inside these stored procedures to validate these parameters and prevent SQL injection. Assume we have these constraints: We can't rewrite the procedures to not use Dynamic SQL We can't use sp_OACreate etc., to use regular expressions for validation. We can't modify the application which calls the stored

dynamic sql query in postgres

断了今生、忘了曾经 提交于 2019-11-26 12:28:26
问题 I was attempting to use Dynamic SQL to run some queries in postgres. Example: EXECUTE format(\'SELECT * from result_%s_table\', quote_ident((select id from ids where condition = some_condition))) I have to query a table, which is of the form result_%s_table wherein, I need to substitute the correct table name (an id) from an another table. I get the error ERROR: prepared statement \"format\" does not exist Link: string substitution with query result postgresql 回答1: EXECUTE ... USING only

DROP FUNCTION without knowing the number/type of parameters?

删除回忆录丶 提交于 2019-11-26 12:13:15
问题 I keep all my functions in a text file with \'CREATE OR REPLACE FUNCTION somefunction\' . So if I add or change some function I just feed the file to psql. Now if I add or remove parameters to an existing function, it creates an overload with the same name and to delete the original I need type in all the parameter types in the exact order which is kind of tedious. Is there some kind of wildcard I can use to DROP all functions with a given name so I can just add DROP FUNCTION lines to the top

User defined variables in PostgreSQL

ぃ、小莉子 提交于 2019-11-26 11:38:30
问题 I have the following MySQL script which I want to implement in PostgreSQL. SET @statement = search_address_query; PREPARE dynquery FROM @statement; EXECUTE dynquery; DEALLOCATE PREPARE dynquery; How can I define user defined variable \"@statement\" using PostgreSQL. 回答1: PostgreSQL does not normally use variables in plain SQL. But you can do that, too: SET foo.test = 'SELECT bar FROM baz'; SELECT current_setting('foo.test'); Read about Customized Options in the manual. In PostgreSQL 9.1 or

Truncating all tables in a Postgres database

大兔子大兔子 提交于 2019-11-26 11:32:52
I regularly need to delete all the data from my PostgreSQL database before a rebuild. How would I do this directly in SQL? At the moment I've managed to come up with a SQL statement that returns all the commands I need to execute: SELECT 'TRUNCATE TABLE ' || tablename || ';' FROM pg_tables WHERE tableowner='MYUSER'; But I can't see a way to execute them programmatically once I have them. Henning FrustratedWithFormsDesigner is correct, PL/pgSQL can do this. Here's the script: CREATE OR REPLACE FUNCTION truncate_tables(username IN VARCHAR) RETURNS void AS $$ DECLARE statements CURSOR FOR SELECT