dynamic-sql

Dynamically creating and executing sql commands in oracle

佐手、 提交于 2019-12-24 09:17:00
问题 I am taking a database class and at the beginning of the lab section of the class we usually have to drop all the tables in the database created previously. I wanted to be able to run a script that does this dynamically, but cannot seem to get it to work. Here is the code I have so far. declare tname string(50); cursor ctable is select table_name from user_tables; begin open ctable; LOOP FETCH ctable into tname; if tname != '' then execute immediate 'drop table ' || tname; END if; EXIT WHEN

Replace double quotes with single quotes in Postgres (plpgsql)

荒凉一梦 提交于 2019-12-24 04:57:09
问题 I am building a trigger function in plpgsql for my partitioned table and I've got all of my logic working, but am having trouble inserting the actual record into my table. I have to reference my specific table by a variable reference, so that (as far as I understand) forces me to use an EXECUTE command, as so: EXECUTE 'INSERT INTO ' || tablename || ' VALUES ' || NEW.*; However, this does not handle unpacking the record stored in NEW in a way that Postgres' INSERT function can understand. It

Change schema of multiple PostgreSQL functions in one operation?

眉间皱痕 提交于 2019-12-24 04:24:10
问题 Recently I needed to move objects from PostgreSQL's default schema "public" to another schema. If found this post which shows how to move tables which was great, but I also need to move the functions. 回答1: You could refine the loop some more (demonstrating only the second query ): DO $do$ DECLARE r record; sql text = ''; BEGIN FOR r IN SELECT p.proname, pg_get_function_identity_arguments(p.oid) AS params FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace WHERE nspname = 'public' --

Creating cursor with Dynamic SQL in MYSQL

南笙酒味 提交于 2019-12-24 03:05:32
问题 I am writing a stored procedure which opens a cursor to a table and then iterate through all records. In the iterating process I create a dynamic query based on the results of first cursor. I need to open the cursor on dynamic sql, but MySQL is not allowing to do me so, as accoriding to the official doc of mysql "Cursors must be declared before declaring handlers. Variables and conditions must be declared before declaring either cursors or handlers" . Here is the script DELIMITER $$ DROP

Why to avoid dynamic SQL queries ? any suggestion to remove there bad part and to use these?

左心房为你撑大大i 提交于 2019-12-23 15:44:03
问题 Can you please guide me why dynamic SQL is suggested to avoid ? Is there any way that I can keep on using dynamic SQL and avoid its bad things ? 回答1: http://www.sommarskog.se/dynamic_sql.html If you don't understand everything in this, come back and ask a question, but under no circustances should you use dynamic SQl until you understand this article. 回答2: The main problem is sql injection. People can enter data that can change the intent of your sql. One of the best solutions is to use sp

where can I find a good example of using linq & lambda expressions to generate dynamic where and orderby sql?

半世苍凉 提交于 2019-12-23 03:22:12
问题 where can I find a good example of using linq & lambda expressions to generate dynamic sql? For example, I need a method to take these parameters GoupOperator ('And' or 'OR') A list of objects each with the following parameters: SearchColumn. SearchValue. SearchOperator (equals, contains, does not equal ...) And another method to orderby any particular column ascending or descending If this question has been properly answered before , I will gladly delete it - none of previous answers Ive

Search across multiple tables and also display table name in resulting rows

青春壹個敷衍的年華 提交于 2019-12-23 03:04:11
问题 How do I structure an SQL statement to run across multiple flat unrelated tables and display the result with the result of the select and the name of the table where the result came from. The scenario is such that I have several tables with the same column name in each. It is data that I have received from outside parties that I store as it is in different tables. Same tables look like: Table 1: pid, parent_name, student_name, student_number, class_name, columnN Table 2: pid, previous_school,

Variables for identifiers inside IF EXISTS in a plpgsql function

本小妞迷上赌 提交于 2019-12-22 13:08:52
问题 CREATE OR REPLACE FUNCTION drop_now() RETURNS void AS $BODY$ DECLARE row record; BEGIN RAISE INFO 'in'; FOR row IN select relname from pg_stat_user_tables WHERE schemaname='public' AND relname LIKE '%test%' LOOP IF EXISTS(SELECT row.relname.tm FROM row.relname WHERE row.relname.tm < current_timestamp - INTERVAL '90 minutes' LIMIT 1) THEN -- EXECUTE 'DROP TABLE ' || quote_ident(row.relname); RAISE INFO 'Dropped table: %', quote_ident(row.relname); END IF; END LOOP; END; $BODY$ LANGUAGE plpgsql

How can I alter a sequence in dynamic SQL?

与世无争的帅哥 提交于 2019-12-22 09:01:37
问题 I'm trying to create a script to migrate data from one DB to another. One thing I'm not currently able to do is set the nextval of a sequence to the nextval of a sequence in another DB. I got the difference in values from user_sequences and generated the following dynamic SQL statements: execute immediate 'alter sequence myseq increment by 100'; execute immediate 'select myseq.nextval from dual'; execute immediate 'alter sequence myseq increment by 1'; commit; But nothing happens. What am I

Passing dynamic input parameters to 'execute Immediate'

ⅰ亾dé卋堺 提交于 2019-12-22 08:44:54
问题 I have a table where I am storing certain conditions along with input parameters as shown below: CONDITION | INPUT_PARAMS --------------------------------------------------------- :p_end_date < :p_start_date | v_end_date, IN v_start_date :p_joining_day = 'MONDAY' | v_joining_day I want to use execute immediate to evaluate the conditions. select condition, input_param into v_execute_condition, v_input_param From table; v_execute_statement := 'IF '||v_execute_condition ||' '|| 'THEN :o_flag :=