dynamic-sql

Saving the output of a dynamic query that uses refcursor into a table

落爺英雄遲暮 提交于 2019-12-02 07:37:45
问题 In continuation to a previous case, in which a dynamic SELECT query that uses refcursor was created and then was executed - I would like to ask the following: The desired output that we got from the indicated procedure was output into the DataOutput. I would like to find a way to store the data into a new table in the db. Instead of the straight forward command: CREATE TABLE mydaughtertable AS SELECT enrich_d_dkj_p_k27ac,enrich_lr_dkj_p_k27ac,enrich_r_dkj_p_k27ac FROM dkj_p_k27ac The idea is

Using LIKE and % in a stored procedure with parameter in FROM clause

孤者浪人 提交于 2019-12-02 06:40:32
问题 I am creating a web page to host a database. I want this web page to have a search box feature that can update a GridView in visual studio 2017 for tables in SSMS 2014. I want this GrideView to be dynamic in that a end user could select a table, a column, and then specify a "searchString" to apply to the data in the column. The web page looks like this: On to the code. On the search button click event I want the values in each of the three text boxes to be passed into a stored procedure. Here

Syntax error in dynamic SQL in pl/pgsql function

╄→гoц情女王★ 提交于 2019-12-02 04:33:56
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.event_id = event.id AND event_person.event_id = event.id AND person.id = event_person.person_id AND person

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

冷暖自知 提交于 2019-12-02 04:31:28
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_row.%I',fieldName); -- This gives a syntax error. END; $$ LANGUAGE plpgsql; How can I get the value of a

Saving the output of a dynamic query that uses refcursor into a table

丶灬走出姿态 提交于 2019-12-02 04:24:57
In continuation to a previous case , in which a dynamic SELECT query that uses refcursor was created and then was executed - I would like to ask the following: The desired output that we got from the indicated procedure was output into the DataOutput. I would like to find a way to store the data into a new table in the db. Instead of the straight forward command: CREATE TABLE mydaughtertable AS SELECT enrich_d_dkj_p_k27ac,enrich_lr_dkj_p_k27ac,enrich_r_dkj_p_k27ac FROM dkj_p_k27ac The idea is to run something like: CREATE TABLE mydaughtertable AS myresult('dkj_p_k27ac','enri') But this

How to pass NEW.* to EXECUTE in trigger function

北城以北 提交于 2019-12-02 04:20:16
I have a simple mission is inserting huge MD5 values into tables (partitioned table), and have created a trigger and also a trigger function to instead of INSERT operation. And in function I checked the first two characters of NEW.md5 to determine which table should be inserted. DECLARE tb text; BEGIN IF TG_OP = 'INSERT' THEN tb = 'samples_' || left(NEW.md5, 2); EXECUTE(format('INSERT INTO %s VALUES (%s);', tb, NEW.*)); <- WRONG END IF; RETURN NULL; END; The question is how to concat the NEW.* into the SQL statement? Erwin Brandstetter Best with the USING clause of EXECUTE : CREATE FUNCTION

Oracle SQL — Grabbing values from multiple rows

时间秒杀一切 提交于 2019-12-02 03:32:29
I have a table like this: TABLE: FACTS ID KEY VALUE 1 name Jeremy 1 height 5'11 1 awesomeness 10 2 name Mark 2 awesomeness 4 3 height 4'6 So, the (ID,KEY) tuple can be considered as a primary key. I am trying to return rows like this: ID NAME HEIGHT AWESOMENESS 1 Jeremy 5'11 10 2 Mark (null) 4 3 (null) 4'6 (null) So other than by doing a sub-select for each column, how can grab the key values, if they are there, and collect them into my single row? What I tried so far was: SELECT id, CASE WHEN facts.key = 'name' THEN value END name, CASE WHEN facts.key = 'height' THEN value END height, CASE

PL/SQL rewrite concatenated query with 'IN' clause

旧巷老猫 提交于 2019-12-02 02:48:07
Currently I have in my pl/sql code following statements: -- vList looks like '1,2,3,4' vStatement := 'SELECT NAME FROM T_USER WHERE ID IN ( ' || vList || ' ) '; Execute Immediate vStatement BULK COLLECT INTO tNames; I think that concatenating of query if bad practice, so I want to make this query without using stings. What is the way to rewrite this ? P.S. maybe people here can point out why concatenation of queries is bad, because i don't have enough reasons to prove that this style is bad. my guess is that you took some steps previously to get vList id's into a delimited string (you don't

Dynamic query results into a temp table or table variable

萝らか妹 提交于 2019-12-02 02:27:58
I have a stored procedure that uses sp_executesql to generate a result set, the number of columns in the result can vary but will be in the form of Col1 Col2 Col3 etc. I need to get the result into a temp table or table variable so I can work with it. The problem is I need to define the columns of the temp table, which I cant do dynamically using sp_executesql as the scope of the temp table is lost after the command is executed. I have toyed with the idea of using Global Temp tables, as the scope allows it to be created dynamically, however, there is a very good chance the Global Temps would

Dynamic query results into a temp table or table variable

我的梦境 提交于 2019-12-02 01:45:22
问题 I have a stored procedure that uses sp_executesql to generate a result set, the number of columns in the result can vary but will be in the form of Col1 Col2 Col3 etc. I need to get the result into a temp table or table variable so I can work with it. The problem is I need to define the columns of the temp table, which I cant do dynamically using sp_executesql as the scope of the temp table is lost after the command is executed. I have toyed with the idea of using Global Temp tables, as the