dynamic-sql

Test for null in function with varying parameters

情到浓时终转凉″ 提交于 2019-11-26 22:22:31
问题 I have a Postgres function: create function myfunction(integer, text, text, text, text, text, text) RETURNS table(id int, match text, score int, nr int, nr_extra character varying, info character varying, postcode character varying, street character varying, place character varying, country character varying, the_geom geometry) AS $$ BEGIN return query (select a.id, 'address' as match, 1 as score, a.ad_nr, a.ad_nr_extra,a.ad_info,a.ad_postcode, s.name as street, p.name place , c.name country,

how to select columns as rows?

让人想犯罪 __ 提交于 2019-11-26 21:51:00
问题 So, I've been searching around and I've found things similar to my problem, but I need more help to get a real solution. I'm trying to construct a query that will return 2 columns of data, the first column should be a list of the column names themselves and the second should be the value of that column. Visually it would look like this Column1 Column2 ------- ------- columnA value_of_columnA columnB value_of_columnB ... ... I'm pretty sure that this is going to require dynamic SQL to achieve,

Dynamic SQL (EXECUTE) as condition for IF statement

岁酱吖の 提交于 2019-11-26 21:48:12
问题 I want to execute a dynamic SQL statement, with its returned value being the conditional for an IF statement: IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN This generates the error ERROR: type "execute" does not exist . Is it possible to do this, or is it necessary to execute the SQL before the IF statement into a variable, and then check the variable as the conditional? 回答1: This construct is not possible: IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN ... You can simplify to: IF EXISTS

Merge a table and a change log into a view in PostgreSQL

淺唱寂寞╮ 提交于 2019-11-26 21:30:53
问题 My PostgreSQL database contains a table to store instances of a registered entity. This table is populated via spreadsheet upload. A web interface allows an operator to modify the information presented. However, the original data is not modified. All changes are stored in a separate table changes with the columns unique_id , column_name , value and updated_at . Once changes are made, they are presented to the operator by first querying the original table and then querying the change table

How do you specify IN clause in a dynamic query using a variable?

一世执手 提交于 2019-11-26 21:26:40
问题 In PL/SQL, you can specify the values for the IN operator using concatenation: v_sql := 'select field1 from table1 where field2 in (' || v_list || ')'; Is it possible to do the same using a variable? v_sql := 'select field1 from table1 where field2 in (:v_list)'; If so, how? EDIT: With reference to Marcin's answer, how do I select from the resultant table? declare cursor c_get_csv_as_tables is select in_list(food_list) food_list from emp_food where emp_type = 'PERM'; cursor c_get_food_list (v

Can a stored procedure have dynamic parameters to be used in an “IN” clause?

家住魔仙堡 提交于 2019-11-26 21:08:14
问题 I want to run a query like this: SELECT * FROM Studio WHERE Id IN (134, 144, 132, 138, 7432, 7543, 2566) but the amount of Id 's passed to the IN clause is only determined at runtime. Do I have to use dynamic SQL or can this be done with a stored procedure? UPDATE: If either option is available, which one is better? Thanks. 回答1: Depending on your version of Sql Server, you can do this one of two different ways. For Sql 2000/2005, you can use a parameter (type varchar) that has a delimited

Passing column names dynamically for a record variable in PostgreSQL

北战南征 提交于 2019-11-26 20:36:49
问题 Using PostgreSQL, column values from a table for 1st record are stored in a record variable. for ex: let the variable be: recordvar recordvar.columnname gives the value of the column name specified. I will define the columname in a variable: var := columnname In place of columnname if I replace with the variable i.e. recordvar.var , it is not working. Please let me know how to proceed in this situation. Following is the sample code: CREATE OR REPLACE FUNCTION getrowdata(id numeric, table_name

Generate Delete Statement From Foreign Key Relationships in SQL 2008?

爷,独闯天下 提交于 2019-11-26 19:54:15
Is it possible via script/tool to generate a delete statement based on the tables fk relations. i.e. I have the table: DelMe(ID) and there are 30 tables with fk references to its ID that I need to delete first, is there some tool/script that I can run that will generate the 30 delete statements based on the FK relations for me ? (btw I know about cascade delete on the relations, I can't use it in this existing db) I'm using Microsoft SQL Server 2008 DELETE statements generated for use in SP with parameter, and as ON DELETE triggers: (this variant supports single column FKs only) SELECT 'DELETE

Execute a dynamic crosstab query

落爺英雄遲暮 提交于 2019-11-26 19:09:15
I implemented this function in my Postgres database: http://www.cureffi.org/2013/03/19/automatically-creating-pivot-table-column-names-in-postgresql/ Here's the function: create or replace function xtab (tablename varchar, rowc varchar, colc varchar, cellc varchar, celldatatype varchar) returns varchar language plpgsql as $$ declare dynsql1 varchar; dynsql2 varchar; columnlist varchar; begin -- 1. retrieve list of column names. dynsql1 = 'select string_agg(distinct '||colc||'||'' '||celldatatype||''','','' order by '||colc||'||'' '||celldatatype||''') from '||tablename||';'; execute dynsql1

Return SETOF rows from PostgreSQL function

♀尐吖头ヾ 提交于 2019-11-26 17:02:38
问题 I have a situation where I want to return the join between two views. and that's a lot of columns. It was pretty easy in sql server. But in PostgreSQL when I do the join. I get the error "a column definition list is required". Is there any way I can bypass this, I don't want to provide the definitions of returning columns. CREATE OR REPLACE FUNCTION functionA(username character varying DEFAULT ''::character varying, databaseobject character varying DEFAULT ''::character varying) RETURNS SETOF