dynamic-sql

TSQL Pivoting Issue - looking for better approach

本秂侑毒 提交于 2019-12-02 16:14:39
问题 This is a T-SQL related question. I am using SQL Server 2012. I have a table like this: I would like to have output like this: Explanation: For each employee, there will be a row. An employee has one or more assignments. Batch Id specifies this. Based on the batch Id, the column names will change (e.g. Country 1, Country 2 etc.). Approach so far: Un-pivot the source table like the following: select EmpId, 'Country ' + cast(BatchId as varchar) as [ColumnName], Country as [ColumnValue] from

How to INSERT INTO table having multiple column from dynamic query?

一笑奈何 提交于 2019-12-02 16:02:46
问题 Similar to this previous question for a one-to-one mapping I need a solution for multiple columns in source and destination. Still working with Postgres 9.4.4, the query and schema are modified and are as below: Let's say I have these two tables Table1 and Table2 : Create table Table1(col1 int, col2 varchar(100), col3 varchar(100)); Create table Table2(col1 int, col2 varchar(100), col3 varchar(100)); There is another table Table3 storing a formula for migrating data from Table1 to Table2 :

PLpgSQL function to find columns with only NULL values in a given table

这一生的挚爱 提交于 2019-12-02 15:06:28
We have to find columns of a table with only NULL values. We are trying to build a plpgsql function that takes a table's name and returns the list of such columns. How to create such a function? We are using PgAdmin 1.16. Erwin Brandstetter You can query the catalog table pg_attribute to get a list of columns which are not defined NOT NULL and therefore can hold NULL values: SELECT quote_ident(attname) AS column_can_be_null FROM pg_attribute WHERE attrelid = 'tbl'::regclass -- valid, visible table name AND attnum >= 1 -- exclude tableoid & friends AND NOT attisdropped -- exclude dropped

Dynamic auditing of data with PostgreSQL trigger

℡╲_俬逩灬. 提交于 2019-12-02 13:41:26
问题 I'm interested in using the following audit mechanism in an existing PostgreSQL database. http://wiki.postgresql.org/wiki/Audit_trigger but, would like (if possible) to make one modification. I would also like to log the primary_key's value where it could be queried later. So, I would like to add a field named something like "record_id" to the "logged_actions" table. The problem is that every table in the existing database has a different primary key fieldname. The good news is that the

Insert from Dynamic Query in Postgres

孤街浪徒 提交于 2019-12-02 10:59:11
With reference solution I've posted in my previous post resulted in one more situation. While trying to insert into my destination table(schema as below). -- Table: normalized_transaction -- DROP TABLE normalized_transaction; CREATE TABLE normalized_transaction ( transaction_id uuid, file_id uuid, account_number character varying(40), currency character varying(3), trade_date date, value_date date, narration character varying(200), amount numeric, mesitis_account_number character varying(50), tag character varying(255), supporting_file_id uuid, supporting_record_id uuid, status integer DEFAULT

How to INSERT INTO table having multiple column from dynamic query?

白昼怎懂夜的黑 提交于 2019-12-02 10:48:38
Similar to this previous question for a one-to-one mapping I need a solution for multiple columns in source and destination. Still working with Postgres 9.4.4, the query and schema are modified and are as below: Let's say I have these two tables Table1 and Table2 : Create table Table1(col1 int, col2 varchar(100), col3 varchar(100)); Create table Table2(col1 int, col2 varchar(100), col3 varchar(100)); There is another table Table3 storing a formula for migrating data from Table1 to Table2 : CREATE TABLE Table3 ( tbl_src character varying(200), col_src character varying(500), tbl_des character

Passing the table as a parameter

萝らか妹 提交于 2019-12-02 10:05:47
I have to convert from lat and long to geom to use PostGIS. My problem, I have various tables from different locations and I want to pass the table as a parameter to the function. I'm trying this: CREATE or REPLACE FUNCTION convert_from_lon_lat(float,float,character varying) RETURNS integer AS $$ select id from $3 as vertices order by vertices.geom <-> ST_SetSrid(ST_MakePoint($1,$2),4326) LIMIT 1; $$ LANGUAGE SQL; but I get a syntax error. EDIT1: So I changed the previous code to this: CREATE or REPLACE FUNCTION convert_from_lon_lat(long float, lat float, _table character varying) RETURNS

PostgreSQL: How to pass table/view name as a parameter to function in PostgreSQL?

笑着哭i 提交于 2019-12-02 10:05:40
For example: I have a VIEW called "view1" which contains 'name' and 'slno' columns, now i want it to be display using FUNCTION called "f1" as shown below: --Function create or replace function f1(viewname varchar) returns table (name varchar,slno integer) as $body$ begin return query select * from viewname; end; $body$ language plpgsql; This is dynamic SQL, so you need EXECUTE . RETURN QUERY EXECUTE format('SELECT * FROM %I', "name"); Separately, that's a weird thing to want to do. 来源: https://stackoverflow.com/questions/22753868/postgresql-how-to-pass-table-view-name-as-a-parameter-to

Syntax error in dynamic SQL in pl/pgsql function

限于喜欢 提交于 2019-12-02 08:56:33
问题 I am using pl/pgsql in PostgreSQL 10, to create complex queries. I am testing a query with a couple of JOIN s and AND s. This is what I have so far: DROP FUNCTION IF EXISTS search_person(name text); CREATE FUNCTION search_person(name text) RETURNS TABLE(address_id integer, address_geom text, event_name text) AS $$ --DECLARE BEGIN RETURN QUERY EXECUTE 'SELECT address.id, event.name, address.geom FROM event JOIN person JOIN address JOIN person_address JOIN event_person WHERE person_address

How to get the value of a dynamically generated field name in PL/pgSQL

守給你的承諾、 提交于 2019-12-02 08:11:03
问题 Sample code trimmed down the the bare essentials to demonstrate question: CREATE OR REPLACE FUNCTION mytest4() RETURNS TEXT AS $$ DECLARE wc_row wc_files%ROWTYPE; fieldName TEXT; BEGIN SELECT * INTO wc_row FROM wc_files WHERE "fileNumber" = 17117; -- RETURN wc_row."fileTitle"; -- This works. I get the contents of the field. fieldName := 'fileTitle'; -- RETURN format('wc_row.%I',fieldName); -- This returns 'wc_row."fileTitle"' -- but I need the value of it instead. RETURN EXECUTE format('wc