dynamic-sql

For loop with dynamic table name in Postgresql 9.1?

有些话、适合烂在心里 提交于 2019-12-06 07:45:20
问题 I have a plpgslq function which does some data processing and would like to write a for loop, however my table name is not known at design time. Is there any possible way to achieve this? Here is sample code snippet of what I want to achieve: -- Function: check_data() -- DROP FUNCTION check_data(); CREATE OR REPLACE FUNCTION check_data() RETURNS character varying AS $BODY$declare dyn_rec record; tbl_name record; begin -- sample dynamic tables tbl_name := 'cars'; tbl_name := 'trucks'; tbl_name

How do I pass input parameters to sp_executesql?

寵の児 提交于 2019-12-06 06:48:59
问题 In SQL Server 2014, I am trying to create a dynamic WHERE clause. I have built the query as a string, but when I try to execute it with sp_executesql, I get the following error: Líne 13 You must declare the scalar variable "@desde". I can't figure out how to get sp_executesql to recognize the input parameters. ALTER PROCEDURE [dbo].[seleccionarFacturas] -- Add the parameters for the stored procedure here @desde char(8) = null, @hasta char(8) = null, @minimo int = null, @ciudad int = null AS

Using dynamic query + user defined datatype in Postgres

落爺英雄遲暮 提交于 2019-12-06 06:47:43
I need a function to normalize my input table features values. My features table has 9 columns out of which x1,x2...x6 are the input columns I need to scale. I'm able to do it by using a static query: create or replace function scale_function() returns void as $$ declare tav1 features%rowtype; rang1 features%rowtype; begin select avg(n),avg(x0),avg(x1),avg(x2),avg(x3),avg(x4),avg(x5),avg(x6),avg(y) into tav1 from features; select max(n)-min(n),max(x0)-min(x0),max(x1)-min(x1),max(x2)-min(x2),max(x3)-min(x3), max(x4)-min(x4),max(x5)-min(x5),max(x6)-min(x6),max(y)-min(y) into rang1 from features;

Truncating display by default in postgres psql select statements

一曲冷凌霜 提交于 2019-12-06 05:27:28
问题 I have a table with a long text column. I'd like to be able to select all of the columns but limit the text column without needing to write every column. select * from resources; Produces an output that is too long to display correctly in psql. I can get something to show up by using substr() or left() on the long column, but then I need to specify each column. select id, left(data, 50), file_format_version, ... from resources; Is there a way that I can just get psql to truncate long columns

What are the dangers of dynamic SQL, and can they be avoided?

回眸只為那壹抹淺笑 提交于 2019-12-06 04:54:26
问题 We've just been given the following code as a solution for a complicated search query in a new application provided by offshore developers. I'm skeptical of the use of dynamic SQL because I could close the SQL statement using '; and then excute a nasty that will be performed on the database! Any ideas on how to fix the injection attack? ALTER procedure [dbo].[SearchVenues] --'','',10,1,1,'' @selectedFeature as varchar(MAX), @searchStr as varchar(100), @pageCount as int, @startIndex as int,

How can I create a loop on an UPDATE statement that works until there is no row left to update?

本小妞迷上赌 提交于 2019-12-06 03:23:27
问题 Assume that I have thousands of rows to update. And I plan to do the update iteratively; by only updating 1000 rows per iteration. And I want to iterate until there are no rows left to update. How can I run the T-SQL script below until there is no row to update? -- TODO: Create a loop so that it exists when there is no ROW left to be updated; -- how can I do it? UPDATE tableToUpdate SET IsVegetable = 1 WHERE Id IN (SELECT TOP 1000 Id FROM tableToUpdate WHERE Date = '2011-07-23 14:00') -- Loop

Getting 'The argument 1 of the xml data type method “modify” must be a string literal' while inserting a attribute in xml

爱⌒轻易说出口 提交于 2019-12-06 00:10:46
问题 Trying the following code. But getting 'The argument 1 of the xml data type method "modify" must be a string literal' error. searched alot but cant find any solution for this problem SET @Path = '/@ParentNodeName/@NodeName/child::*' SET @x.modify('insert attribute status {sql:variable("@status")} as first into (' + @Path + ')[1]') 回答1: The problem isn't the sql:variable with the value you're trying to insert - it's the way you include the XPath into your modify statement. You cannot string

PostgreSQL 9.3 trigger function to insert into table with parameterized name

拟墨画扇 提交于 2019-12-05 21:11:56
I'm trying to dynamically partition log entries in Postgres. I have 53 child tables (1 for each week's worth of log entries), and would like to route INSERTs to a child table using a trigger. I run the function with INSERT INTO log5 VALUES (NEW.*) , and it works. I run the function with the EXECUTE statement instead, and it fails. Within the EXECUTE statement, it's recognizing NEW as a table name and not a variable passed to the trigger function. Any ideas on how to fix? Thanks! The error: QUERY: INSERT INTO log5 VALUES (NEW.*) CONTEXT: PL/pgSQL function log_roll_test() line 6 at EXECUTE

Put $$ in dollar-quoted string in PostgreSQL

前提是你 提交于 2019-12-05 18:40:40
I have a function in Postgres: CREATE OR REPLACE FUNCTION upsert(sql_insert text, sql_update text) RETURNS integer AS $BODY$ BEGIN EXECUTE sql_insert; RETURN 1; EXCEPTION WHEN unique_violation THEN EXECUTE sql_update; RETURN 2; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE COST 100; ALTER FUNCTION upsert(text, text) OWNER TO dce; I usually use this query to call that function: select upsert( $$INSERT INTO zz(a, b) VALUES (66, 'hahahaha')$$, $$UPDATE zz SET a=66, b='hahahaha' WHERE a=66$$ ) It works. Unfortunately, my query string cannot contain $$ , like this: select upsert( $$INSERT INTO zz(a, b)

Passing dynamic input parameters to 'execute Immediate'

不羁的心 提交于 2019-12-05 17:56:38
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 := ''Y'';' ||' '|| 'ELSE :o_flag := ''N'';' ||' '|| 'END IF;'; v_execute_statement := 'BEGIN '||v_execute