dynamic-sql

Copy records with dynamic column names

大憨熊 提交于 2019-12-11 13:22:24
问题 I have two tables with different columns in PostgreSQL 9.3: CREATE TABLE person1( NAME TEXT NOT NULL, AGE INT NOT NULL ); CREATE TABLE person2( NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL ); INSERT INTO person2 (Name, Age, ADDRESS, SALARY) VALUES ('Piotr', 20, 'London', 80); I would like to copy records from person2 to person1 , but column names can change in program, so I would like to select joint column names in program. So I create an array containing the

Using query to set the column type in PostgreSQL

元气小坏坏 提交于 2019-12-11 12:53:40
问题 After the excellent answer by Alexandre GUIDET, I attempted to run the following query: create table egg (id (SELECT pg_catalog.format_type(a.atttypid, a.atttypmod) as Datatype FROM pg_catalog.pg_attribute a WHERE a.attnum > 0 AND NOT a.attisdropped AND a.attrelid = ( SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname ~ '^(TABLENAME)$' AND pg_catalog.pg_table_is_visible(c.oid) ) and a.attname = 'COLUMNNAME')); PostgreSQL,

Select row cells as new columns

a 夏天 提交于 2019-12-11 10:44:58
问题 Basically, I have a table that stores column names with some restrictions: infos , and another one that stores the values for those columns: info_data . I want to get a table which has the columns from infos and data from info_data . I've tried with crosstab function but it doesn't have the desired effect. I have 2 tables: CREATE TABLE infos (id serial PRIMARY KEY, name text NOT NULL, id_member integer NOT NULL, title text, min_length integer NOT NULL DEFAULT 0, max_length integer NOT NULL

SQL Injection-safe call of polymorphic function

孤街醉人 提交于 2019-12-11 10:36:54
问题 Several times I've found myself refactoring web application code and end up wanting to do something like this (Groovy in this case, but could be anything): Map getData(String relationName, Integer rowId) { def sql = Sql.newInstance([...]) def result = sql.firstRow('SELECT getRelationRow(?,?)', relationName, rowId) sql.close() return new HashMap(result) } where the stored procedure getRelationRow(relname text, rowid integer) executes dynamic sql to retrieve the row of the specified rowid in

how to return temp table from postgres function?

馋奶兔 提交于 2019-12-11 09:26:29
问题 I have below query running fine independently, but showing many issues inside a postgres function CREATE TEMP TABLE tbl (h ltree, pathid int) ; CREATE TEMP TABLE temp_res (pathid int, res_count int) ; insert into tbl select l_tree,pathid from tblinfo where parentid in (880); insert into temp_res select T.pathid pathid from tblinfo p1, tbl T where index(p1.l_tree,T.h ) != -1 GROUP BY T.pathid order by T.pathid; select p.pathid pathid, p.name name, p.PBS PBS,p.parentid parentid,p.resid resid

Row expansion via “*” is not supported here

依然范特西╮ 提交于 2019-12-11 09:26:05
问题 I'm in a trigger context and trying to get following snippet to work. execute format('insert into %I (user_name, action, new_values, query) values (''%I'', ''i'', hstore(($1).*), current_query())', tg_table_name::text || '_audit', current_user::text) using new; I'm getting the following error [SQL]insert into book (title, n_pages) values ('PG is Great', 250); [Err] ERROR: row expansion via "*" is not supported here LINE 2: values ('u1', 'i', hstore(($1).*), current_q... ^ QUERY: insert into

SQL: How can we make a table1 JOIN table2 ON a table given in a field in table1?

為{幸葍}努か 提交于 2019-12-11 08:09:41
问题 Imagine I have table1 which has a column named 'table_name'. I use table1 .table_name to store the name of another table in the database. The referenceable tables would all have a field 'target_id. Is is possible to use table_name in a JOIN statement? For example: SELECT t1.*, t2.* FROM table1 AS t1 JOIN table1.table_name AS t2 ON t1.table1_id = t2.target_id The obvious solution is to use the script (C++ in my case) to get the table name first, and construct a SQL query from it. The question

How to clone a RECORD in PostgreSQL

白昼怎懂夜的黑 提交于 2019-12-11 06:45:08
问题 I want to loop through a query, but also retain the actual record for the next loop, so I can compare two adjacent rows. CREATE OR REPLACE FUNCTION public.test () RETURNS void AS $body$ DECLARE previous RECORD; actual RECORD; query TEXT; isdistinct BOOLEAN; tablename VARCHAR; columnname VARCHAR; firstrow BOOLEAN DEFAULT TRUE; BEGIN tablename = 'naplo.esemeny'; columnname = 'esemeny_id'; query = 'SELECT * FROM ' || tablename || ' LIMIT 2'; FOR actual IN EXECUTE query LOOP --do stuff --save

Explain FOR in oracle

半城伤御伤魂 提交于 2019-12-11 06:10:41
问题 I am making a test. I have all tests in rows, so my rows looks like this; ID | TEST ---------------------------------- 1 | 'select sysdate from dual' 2 | 'select sysdatesss from dual' Now I read it row by row and I need to test it with EXPLAIN PLAN FOR so the for the first row it would be EXPLAIN PLAN FOR select sysdate from dual but I have problem converting the TEST field. Right now I use; EXPLAIN PLAN FOR testing.TEST but it does not work. Any ideas? 回答1: A SQL statement is a string, but

Insert data into temporary table from dynamic query table output

≡放荡痞女 提交于 2019-12-11 06:06:26
问题 I have the below dynamic query run in SQL Server, connecting to an OLAP server using a linked server, which returns a table as a result. SET @nSQL = EXECUTE ('SELECT non empty { [Coded Season].[Coded Season].[Coded Season] * [Season].[Season].[Season] * [Product].[Subclass].[Subclass] * [Product].[Subclass Id].[Subclass Id] } ON ROWS,{ [Measures].[Pl No of Range Opts] } ON COLUMNS FROM RP_C0') AT AS_T_RP_5900_Admin I am executing it in SQL Server like this: exec sp_executesql @nSQL; It