dynamic-sql

Handle result when dynamic SQL is in a loop

亡梦爱人 提交于 2019-12-11 05:34:49
问题 I have a bunch of table that have a "stat" column (stat for status ;-) I would like the count of each stats, and see it! My tables look like this create table a ( a_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), a_stat status_t ); create table b ( b_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), b_stat status_t ); status_t is an enum. So I did this: DO $$ DECLARE tableName RECORD; result RECORD; BEGIN SET SEARCH_PATH = projet, public; FOR tableName IN SELECT c.relname, a.attname FROM pg

Calling preocedure by passing DB Link Dynamically

淺唱寂寞╮ 提交于 2019-12-11 05:26:33
问题 How to pass db link dynamically while calling a procedure? Execute immediate will work or we need to use dbms_sql? For DBMS_SQL i seen it used mostly with curosrs :( Can any one help me ? 回答1: You can use EXECUTE IMMEDIATE. Something like DECLARE l_dblink_name VARCHAR2(30) := 'YourDBLink'; l_sql_stmt VARCHAR2(1000); BEGIN l_sql_stmt := 'BEGIN procedure_name@' || l_dblink_name || ' (:1, :2); END;'; EXECUTE IMMEDIATE l_sql_stmt USING 17, 42; END; assuming that your procedure takes two

Loop through like tables in a schema

て烟熏妆下的殇ゞ 提交于 2019-12-11 05:12:43
问题 Postgres 9.1 - I have a schema that has tables "partitioned" by month (a new table is created each month, all columns the same). It is not set up as normal partitioning with a "master" table. I am currently writing a fairly large query, that I will have to run a few times each month. Schema: augmented_events tables: p201301 (January 2013) p201302 (Feb 2013) p201303 (March 2013) ... p201312 (December 2013) p201401 (January 2014) Right now I have to write my (simplified) query as: select * from

How to Unpivot with dynamic columns Oracle

醉酒当歌 提交于 2019-12-11 04:27:30
问题 I need to unpiviot a table that I don't have control over the columns, so i need to dynamically get the column names: This is what I have CREATE TABLE test ( PK VARCHAR2(255 CHAR), COL1 VARCHAR2(255 CHAR), COL2 VARCHAR2(255 CHAR), COL3 VARCHAR2(255 CHAR), COL4 VARCHAR2(255 CHAR), COL5 VARCHAR2(255 CHAR), COL6 NUMBER, ) declare sql_stmt clob; pivot_clause clob; begin select listagg('''' || column_name || ''' as "' || column_name || '"', ',') within group (order by column_name) into pivot

Altering Multiple Tables at once

筅森魡賤 提交于 2019-12-11 03:55:02
问题 I'm trying to alter multiple SQL Server 2008 R2 tables at one time. This is my code: use DatabaseName go Declare @SchemaUsed varchar(20) = 'dbo' create table #Tables ( TableName varchar(100), Processed int ) insert into #Tables select top 1 table_name, 0 from INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = @SchemaUsed and table_type = 'Base Table' and (TABLE_NAME like 'PM%' ) ORDER BY TABLE_NAME DECLARE @TableName varchar(max) DECLARE @SQL varchar(max) WHILE EXISTS (select top 1 'x' from

PostgreSQL dynamic table access

回眸只為那壹抹淺笑 提交于 2019-12-11 03:37:03
问题 I have a products schema and some tables there. Each table in products schema has an id , and by this id I can get this table name, e.g. products \ product1 \ product2 \ product3 I need to select info from dynamic access to appropriate product, e.g. SELECT * FROM 'products.'(SELECT id from categories WHERE id = 7); Of course, this doesn't work... How I can do something like that in PostgreSQL? 回答1: OK, I found a solution: CREATE OR REPLACE FUNCTION getProductById(cid int) RETURNS RECORD AS $$

Can I create dynamic pivot query from key value table with different data types?

荒凉一梦 提交于 2019-12-11 03:23:29
问题 I have a key-value pairs table. I have created a script using dynamic sql that pivots the table correctly. However, the keys have different data types. Is there a way to cast the keys during or after the pivot dynamically? I can have the data types in the same key-value pairs table for each key or in a separate table linkable by the key. Dynamic sql is used because I wont always know the columns. There will always be a data type. An example of the starting table: sampleid key value datatype -

Postgresql: How to escape single quotes in Database trigger?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:08:02
问题 I created a database trigger to store the row data in an auditing table. During the update operation, this trigger takes data from the main table and inserts it to a history table. (history table has columns: date, operation type say Update/Delete, actual row data) But the trigger fails in some cases because of the quoted text in input data. How can I escape the quoted text in my trigger? --My trigger CREATE OR REPLACE FUNCTION audit.if_modified() RETURNS TRIGGER AS $function$ DECLARE temp

Using a temporary table in dynamic sql in a stored procedure

只谈情不闲聊 提交于 2019-12-11 01:29:08
问题 I am writing a Store Procedure in SQL Server 2012. I have a temporary table defined like so: DECLARE @CURRENT_RET_WEEK_PTIMEIDS TABLE ( PTIMEID INT ) I am also using EXECUTE to write a dynamic SQL query. Is there any way I can join this table onto the above temporary table? 回答1: Try to use local temp-table - IF OBJECT_ID ('tempdb.dbo.#temp') IS NOT NULL DROP TABLE #temp CREATE TABLE #temp (ID INT) INSERT INTO #temp (ID) VALUES (1),(2) DECLARE @SQL NVARCHAR(MAX) SELECT @SQL = 'SELECT * FROM

Return dynamic set of columns

▼魔方 西西 提交于 2019-12-10 23:59:13
问题 I have created the following function to return set of columns based on parameters of that function: CREATE OR REPLACE FUNCTION getColumns(IN _column1 text, IN _column2 text, IN _column3 text, IN _column4 text, IN _table text) RETURNS TABLE(cmf1 text, cmf2 text, cmf3 text, cmf4 text) AS $BODY$ BEGIN RETURN QUERY EXECUTE 'SELECT ' || case when _column1 = 'None' then quote_literal('None') else quote_ident(_column1) end || '::text as cmf1,' || case when _column2 = 'None' then quote_literal('None