dynamic-sql

Simplify Dynamic SQL Pivot Table

不想你离开。 提交于 2019-11-28 11:14:21
问题 I have written a Dynamic Pivot Table Query based on the following. Here is a SQL FIDDLE for reference. CREATE TABLE TestTable1 ([idnumber] INT, [DataTypeId] INT) GO INSERT INTO TestTable1 VALUES (1, 108), (1, 108), (1, 108), (2, 108), (2, 108), (3, 108), (1, 109),(1, 109), (1, 110),(2, 110),(1, 111),(4, 108),(4, 108), (4, 110),(4, 111) GO Here is the Dynamic SQL that I wrote DECLARE @SQL NVARCHAR(MAX), @Cols NVARCHAR(MAX), @ColsP NVARCHAR(MAX) SELECT @Cols = STUFF((select ', ISNULL([' + CAST(

dynamic table name in select statement

大城市里の小女人 提交于 2019-11-28 10:38:08
问题 I have a series of history tables in an oracle 9 database. History_table_00 contains last months data, History_table_01 contains the month before, and History_table_02 the month before that. Next month, History_table_02 will automatically get renamed to history_table_03, history_table_01 renamed to history_table_02, history_table_00 renamed to history_table_01, and a new history_table_00 will be created to gather the newest history (I really hope I am making sense). Anyway, I need to write a

T-SQL looping through XML data column to derive unique set of paths

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-28 06:40:02
问题 I have XML data column which contains question and answer as part of an application process. What I am trying to achieve through T-SQL/ Dynamic SQL is to derive a unique set of path wherever there is a target tag. So for the below xml example, I would expect something like log/clients/client/section/questions/groupone/question/target log/clients/client/section/questions/grouptwo/question/target Idea is to then to use this and loop through the XML to derive the values of the desired tags. I.e

Why can't we use strong ref cursor with dynamic SQL Statement?

不打扰是莪最后的温柔 提交于 2019-11-28 05:47:58
问题 I am trying to use a strong ref cur with dynamic sql statment but it is giving out an error,but when i use weak cursor it works,Please explain what is the reason and please forward me any link of oracle server architect containing matter about how compilation and parsing is done in Oracle server. THIS is the error along with code. ERROR at line 6: ORA-06550: line 6, column 7: PLS-00455: cursor 'EMP_REF_CUR' cannot be used in dynamic SQL OPEN statement ORA-06550: line 6, column 2: PL/SQL:

Dynamic SQL (EXECUTE) as condition for IF statement

冷暖自知 提交于 2019-11-28 00:53:58
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? This construct is not possible: IF EXECUTE 'EXISTS (SELECT 1 FROM mytable)' THEN ... You can simplify to: IF EXISTS (SELECT 1 FROM mytable) THEN ... But your example is probably just simplified. For dynamic SQL executed

Use text output from a function as new query

爱⌒轻易说出口 提交于 2019-11-28 00:27:53
In continuing from a previous case that was assisted by @Erwin Brandstetter and @Craig Ringer, I have fixed my code to become as follows. Note, that my function myresult() outputs now text , and not a table (as indeed, as was pointed out in the former case, there is no point in outputting a table object, since we would need to define all its columns ahead, which basically defies the entire purpose): CREATE OR REPLACE FUNCTION myresult(mytable text, myprefix text) RETURNS text AS $func$ DECLARE myoneliner text; BEGIN SELECT INTO myoneliner 'SELECT ' || string_agg(quote_ident(column_name::text),

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

六眼飞鱼酱① 提交于 2019-11-27 23:23:52
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 (using instance ID and the latest change date, grouped by column name). The two results are merged in PHP

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

孤者浪人 提交于 2019-11-27 23:21:05
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_food_table varchar2Table)is select * from v_food_table; begin for i in c_get_csv_as_tables loop for j

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

◇◆丶佛笑我妖孽 提交于 2019-11-27 21:41:59
问题 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? 回答1: Assuming you only want to drop plain indexes: DO $$BEGIN EXECUTE ( SELECT 'DROP INDEX ' || string_agg(indexrelid::regclass::text, ', ') FROM pg_index

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

孤者浪人 提交于 2019-11-27 21:39:20
问题 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? 回答1: Although MyBatis was designed to execute the