dynamic-sql

EXECUTE IMMEDIATE in plsql

北战南征 提交于 2019-11-29 02:44:14
How to get a result from this code EXECUTE IMMEDIATE 'SELECT * FROM ' || table_name through for loop The usual method looks like this for items in (select * from this_table) loop htp.p(items.id); end loop; If you really need to select * from dynamic table name , then I'd probably go with dbms_sql Type for a record : create type tq84_varchar2_tab as table of varchar2(4000); / Type for a result set (which is an array of records ): create type tq84_varchar2_tab_tab as table of tq84_varchar2_tab; / The function that does the select and returns an instance of the result set : create or replace

How can I drop all indexes of a table in Postgres?

笑着哭i 提交于 2019-11-29 01:31:52
I keep having this problem: I have like 20 indexes on a table that I need to drop in order to do testing. Dropping the table doesn't drop all of this metadata. There doesn't seem to be a wildcard drop index ix_table_* or any useful command. There seem to be some bash loops around psql you can write. There must be something better! Thoughts? Erwin Brandstetter Assuming you only want to drop plain indexes: DO $$BEGIN EXECUTE ( SELECT 'DROP INDEX ' || string_agg(indexrelid::regclass::text, ', ') FROM pg_index i LEFT JOIN pg_depend d ON d.objid = i.indexrelid AND d.deptype = 'i' WHERE i.indrelid =

Can I use MyBatis to generate Dynamic SQL without executing it?

送分小仙女□ 提交于 2019-11-29 01:30:07
I have some complex queries to build with a number of optional filters, for which MyBatis seems like an ideal candidate for generating dynamic SQL. However, I still want my query to execute in the same framework as the rest of the application (which is not using MyBatis). So what I was hoping to do was use MyBatis strictly for generating the SQL, but from there using the rest of my app to actually execute it. Is this possible? If so, how? Although MyBatis was designed to execute the query after it builds it, you can make use of it's configuration and a little bit of "inside knowledge" to get

Binding Parameters to Oracle Dynamic SQL

烈酒焚心 提交于 2019-11-28 19:54:41
I have a stored procedure that accepts multiple parameters (i.e. pName, pHeight, pTeam) I have the query built up like this: SQLQuery VARCHAR2(6000); TestCursor T_CURSOR; SQLQuery := 'SELECT ID, Name, Height, Team FROM MyTable WHERE ID IS NOT NULL '; -- Build the query based on the parameters passed. IF pName IS NOT NULL SQLQuery := SQLQuery || 'AND Name LIKE :pName '; END IF; IF pHeight IS > 0 SQLQuery := SQLQuery || 'AND Height = :pHeight '; END IF; IF pTeam IS NOT NULL SQLQuery := SQLQuery || 'AND Team LIKE :pTeam '; END IF; OPEN TestCursor FOR SQLQuery USING pName, pHeight, pTeam; If I

Declare Variable for a Query String

眉间皱痕 提交于 2019-11-28 16:56:12
I was wondering if there was a way to do this in MS SQL Server 2005: DECLARE @theDate varchar(60) SET @theDate = '''2010-01-01'' AND ''2010-08-31 23:59:59''' SELECT AdministratorCode, SUM(Total) as theTotal, SUM(WOD.Quantity) as theQty, AVG(Total) as avgTotal, (SELECT SUM(tblWOD.Amount) FROM tblWOD JOIN tblWO on tblWOD.OrderID = tblWO.ID WHERE tblWO.Approved = '1' AND tblWO.AdministratorCode = tblWO.AdministratorCode AND tblWO.OrderDate BETWEEN @theDate ) ... etc Is this possible to do? It's possible, but it requires using dynamic SQL. I recommend reading The curse and blessings of dynamic SQL

PL/pgSQL: General Way to Update N Columns in Trigger?

时间秒杀一切 提交于 2019-11-28 12:43:57
I am attempting create a function that will take a general table and convert N columns to upper case. I haven't had any luck finding a solution to this type of problem, but I was able to come up with the following: create or replace function uc_on_insert() returns trigger as $$ declare p_tbl varchar = TG_TABLE_NAME; p_sch varchar = TG_TABLE_SCHEMA; i varchar; begin for i in (select column_name from INFORMATION_SCHEMA.COLUMNS where 1=1 and table_name ilike p_tbl and table_schema ilike p_sch and data_type ='character varying') loop execute 'new.' || i || ' = upper(new.' || i || ');'; return new;

Why does running this query with EXECUTE IMMEDIATE cause it to fail?

江枫思渺然 提交于 2019-11-28 12:11:10
I am writing a PL/SQL procedure that needs to to dynamically generate some queries, one of which involves creating a temporary table using results from a query taken as a parameter. CREATE OR REPLACE PROCEDURE sqlout(query IN VARCHAR2) IS BEGIN EXECUTE IMMEDIATE 'CREATE GLOBAL TEMPORARY TABLE tmp_tab AS (' || query || ');'; END; It compiles correctly, but even with very simple queries such as with: BEGIN sqlout('SELECT * FROM DUAL'); END; IT throws ORA-00911: invalid character . If I run the created query manually it runs correctly. At this point I am able to determine what is causing the

Function to return dynamic set of columns for given table

亡梦爱人 提交于 2019-11-28 11:41:50
I have a fields table to store column information for other tables: CREATE TABLE public.fields ( schema_name varchar(100), table_name varchar(100), column_text varchar(100), column_name varchar(100), column_type varchar(100) default 'varchar(100)', column_visible boolean ); And I'd like to create a function to fetch data for a specific table. Just tried sth like this: create or replace function public.get_table(schema_name text, table_name text, active boolean default true) returns setof record as $$ declare entity_name text default schema_name || '.' || table_name; r record; begin for r in

Format specifier for integer variables in format() for EXECUTE?

ぃ、小莉子 提交于 2019-11-28 11:26:08
CREATE OR REPLACE FUNCTION getParentLtree(parent_id bigint, tbl_name varchar) RETURNS ltree AS $BODY$ DECLARE parent_ltree ltree; BEGIN -- This works fine: -- select into parent_ltree l_tree from tbl1 where id = parent_id; EXECUTE format('select into parent_ltree l_tree from %I where id = %I', tbl_name,parent_id); RETURN parent_ltree; END; $BODY$ LANGUAGE plpgsql; There are 2 issues in above function: parent_id is integer but it is replaced with quotes? What is the correct format specifier for int variables? select into does not work with EXECUTE ? How can I make above commented query to use

How to use dynamic SQL to add value of 2 columns

微笑、不失礼 提交于 2019-11-28 11:23:00
问题 I have small table which contains students marks. Table data is shown in below image. It is look like below in excel I want to calculate the total using dynamic SQL. I don't want to update it. However, I just want to select all the data with calculated total using dynamic SQL. Please refer below code: DECLARE @SQL NVARCHAR(MAX)='' DECLARE @SNumberList NVARCHAR(MAX)='' DECLARE @CalculatedLineNumbers NVARCHAR(MAX)='' SELECT @CalculatedLineNumbers = @CalculatedLineNumbers+ ', '+ CASE WHEN SNo =