dynamic-sql

Calculate number of rows affected by batch query in PostgreSQL

此生再无相见时 提交于 2019-12-04 04:03:28
问题 First of all, yes I've read documentation for DO statement :) http://www.postgresql.org/docs/9.1/static/sql-do.html So my question: I need to execute some dynamic block of code that contains UPDATE statements and calculate the number of all affected rows. I'm using Ado.Net provider. In Oracle the solution would have 4 steps: add InputOutput parameter "N" to command add BEGIN ... END; to command add :N := :N + sql%rowcount after each statement. It's done! We can read N parameter from command,

exec sp_executesql @sql and exec (@sql) SQL Server

倖福魔咒の 提交于 2019-12-04 03:53:43
A Dynamic SQL query from lobodava is: declare @sql nvarchar(4000) = N';with cteColumnts (ORDINAL_POSITION, COLUMN_NAME) as ( select ORDINAL_POSITION, COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = N'''+ @tableName + ''' and COLUMN_NAME like ''' + @columnLikeFilter + ''' ), cteValues (ColumnName, SumValue) as ( SELECT ColumnName, SumValue FROM (SELECT ' + @sumColumns + ' FROM dbo.' + @tableName + ') p UNPIVOT (SumValue FOR ColumnName IN (' + @columns + ') )AS unpvt ) select row_number() over(order by ORDINAL_POSITION) as ID, ColumnName, SumValue from cteColumnts c inner join

ORA-01747: invalid user.table.column, table.column, or column specification

ε祈祈猫儿з 提交于 2019-12-04 02:32:43
Get the above error when the execute immediate is called in a loop Update CustomersPriceGroups set 1AO00=:disc Where cuno=:cuno Parameters: disc=66 cuno=000974 Update CustomersPriceGroups set 1AP00=:disc Where cuno=:cuno Parameters: disc=70.5 cuno=000974 Update CustomersPriceGroups set 1AQ00=:disc Where cuno=:cuno Parameters: disc=66 cuno=000974 Update CustomersPriceGroups set 1ZA00=:disc Where cuno=:cuno Parameters: disc=60 cuno=000974 What does this mean ? Here is the code fragment c:=PriceWorx.frcPriceListCustomers('020','221'); LOOP fetch c into comno,cuno,nama,cpls; exit when c%notfound;

“ERROR: extra data after last expected column” when using PostgreSQL COPY

别说谁变了你拦得住时间么 提交于 2019-12-03 13:43:59
Please bear with me as this is my first post. I'm trying to run the COPY command in PostgreSQL-9.2 to add a tab delimited table from a .txt file to a PostgreSQL database such as: COPY raw_data FROM '/home/Projects/TestData/raw_data.txt' WITH (DELIMITER ' '); I've already created an empty table called "raw_data" in the database using the SQL command: CREATE TABLE raw_data (); I keep getting the following error message when trying to run the COPY command: ERROR: extra data after last expected column CONTEXT: COPY raw_data, line 1: " 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

Robust approach for building SQL queries programmatically

ぐ巨炮叔叔 提交于 2019-12-03 09:03:43
I have to resort to raw SQL where the ORM is falling short (using Django 1.7). The problem is that most of the queries end up being 80-90% similar. I cannot figure out a robust & secure way to build queries without violating re-usability. Is string concatenation the only way out, i.e. build parameter-less query strings using if-else conditions, then safely include the parameters using prepared statements (to avoid SQL injection). I want to follow a simple approach for templating SQL for my project instead of re-inventing a mini ORM. For example, consider this query: SELECT id, name, team, rank

How to design a generic database whose layout may change over time?

为君一笑 提交于 2019-12-03 07:00:43
Here's a tricky one - how do I programatically create and interrogate a database whose contents I can't really foresee? I am implementing a generic input form system. The user can create PHP forms with a WYSIWYG layout and use them for any purpose he wishes. He can also query the input. So, we have three stages: a form is designed and generated. This is a one-off procedure, although the form can be edited later. This designs the database. someone or several people make use of the form - say for daily sales reports, stock keeping, payroll, etc. Their input to the forms is written to the

How to Partition a Table by Month (“Both” YEAR & MONTH) and create monthly partitions automatically?

六眼飞鱼酱① 提交于 2019-12-03 06:18:50
I'm trying to Partition a Table by both Year and Month . The Column through which I'll partition is a datetime type column with an ISO Format ('20150110', 20150202', etc). For example, I have sales data for 2010, 2011, 2012. I'd Like the data to be partitioned by year and each year be partitioned by month as well. (2010/01, 2010/02, ... 2010/12, 2011/01, ... 2015/01...) E.X: Sales2010Jan, Sales2010Feb, Sales2011Jan, Sales2011Feb, Sales2012Dec, etc. My Question is: is it even possible? If it is, how an I automate the process using SSIS? Julien Vavasseur SSIS is an ETL (extract, transform, load)

PL/SQL - Optional conditions in where-clause - without dynamic sql?

自作多情 提交于 2019-12-03 06:16:10
I have a query where not all conditions are necessary. Here's an example of what it looks like when all conditions are used: select num from (select distinct q.num from cqqv q where q.bcode = '1234567' --this is variable and q.lb = 'AXCT' --this is variable and q.type = 'privt' --this is variable and q.edate > sysdate - 30 --this is variable order by dbms_random.value()) subq where rownum <= 10; --this is variable The parts marked as --this is variable are the parts that, well, vary! If a condition is NOT specified, then there is no default value. For example, if the input specifies "*" for q

PLpgSQL function to find columns with only NULL values in a given table

最后都变了- 提交于 2019-12-02 23:49:17
问题 We have to find columns of a table with only NULL values. We are trying to build a plpgsql function that takes a table's name and returns the list of such columns. How to create such a function? We are using PgAdmin 1.16. 回答1: You can query the catalog table pg_attribute to get a list of columns which are not defined NOT NULL and therefore can hold NULL values: SELECT quote_ident(attname) AS column_can_be_null FROM pg_attribute WHERE attrelid = 'tbl'::regclass -- valid, visible table name AND

PostgreSQL: How to pass table/view name as a parameter to function in PostgreSQL?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 17:55:49
问题 For example: I have a VIEW called "view1" which contains 'name' and 'slno' columns, now i want it to be display using FUNCTION called "f1" as shown below: --Function create or replace function f1(viewname varchar) returns table (name varchar,slno integer) as $body$ begin return query select * from viewname; end; $body$ language plpgsql; 回答1: This is dynamic SQL, so you need EXECUTE . RETURN QUERY EXECUTE format('SELECT * FROM %I', "name"); Separately, that's a weird thing to want to do. 来源: