dynamic-sql

Pivoting Data twice with dynamic sql and custom column names

青春壹個敷衍的年華 提交于 2019-12-04 21:09:27
I have a simple table of non-unique account numbers, product IDs, and quantities: For example: account|productid|qty 1 100 1 1 100 1.5 1 102 6 2 100 1 I'm trying to get this to be pivoted dynamically into this kind of structure: account|product1|qty1|product2|qty2|etc..|etc.. 1 100 2.5 102 6 NULL NULL 2 100 1 NULL NULL NULL NULL Some of these customers can have ordered hundreds of different products, so trying to hard-code things ended up being out of the question. I've managed to pivot this into a table like account|100|102 1 2.5 6 2 1 NULL with product IDs as column headers, with: DECLARE

Select column name and value from table

拜拜、爱过 提交于 2019-12-04 16:21:36
If I have the following table in a PostgreSQL database: Col1 Col2 Col3 A a 1 B b 2 Is there a way to get the column name for each value without explicitly specifying the column names ? I.e. have a result set like: Col1 A Col1 B Col2 a Col2 b Col3 1 Col3 2 Erwin Brandstetter Of course, you could write a PL/pgSQL function and query the catalog table pg_attribute yourself. But it's so much easier with one of the following: JSON The function row_to_json() provides functionality that goes half the way. Introduced with Postgres 9.2 : SELECT row_to_json(t, TRUE) FROM tbl t; You don't have to mention

For loop with dynamic table name in Postgresql 9.1?

我怕爱的太早我们不能终老 提交于 2019-12-04 14:16:04
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 := 'bicycles'; for dyn_rec in select * from format($$s%$$,tbl_name) loop raise notice 'item is %',dyn

Build dynamic SQL with “AND” expressions without confusing nested conditionals?

被刻印的时光 ゝ 提交于 2019-12-04 13:02:29
I'm fairly new to php and coding in general. I have a series of conditions I need to test if they are set. They are $option1, $option2, $option3 if (isset($option1)){ if (isset($option2)){ if (isset($option3)){ $query = "SELECT * FROM Group WHERE FirstOption = '$option1' AND SecondOption = '$option2' AND ThirdOption = '$option3'"; } else { $query = "SELECT * FROM Group WHERE FirstOption = '$option1' AND SecondOption = '$option2"; } } else { $query = "SELECT * FROM Group WHERE FirstOption = '$option1' AND ThirdOption = '$option3"; } } else { // .. snip, much more duplicated code .. } So the

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

我的未来我决定 提交于 2019-12-04 11:26:25
问题 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

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

倾然丶 夕夏残阳落幕 提交于 2019-12-04 11:24:59
问题 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

Can I rollback Dynamic SQL in SQL Server / TSQL

拜拜、爱过 提交于 2019-12-04 10:26:31
Can I run a dynamic sql in a transaction and roll back using EXEC: exec('SELECT * FROM TableA; SELECT * FROM TableB;'); Put this in a Transaction and use the @@error after the exec statement to do rollbacks. eg. Code BEGIN TRANSACTION exec('SELECT * FROM TableA; SELECT * FROM TableB;'); IF @@ERROR != 0 BEGIN ROLLBACK TRANSACTION RETURN END ELSE COMMIT TRANSACTION If there are n dynamic sql statements and the error occurs in n/2 will the first 1 to ((n/2) - 1) statements be rolled back Questions about the first answer @@Error won't pick up the error most likely Which means that it might not

Extract value returned from dynamic SQL

ぐ巨炮叔叔 提交于 2019-12-04 05:20:55
I have a stored procedure which generates and executes a piece of dynamic T-SQL which, once built up, looks like this SELECT tblUsers.strUserName AS [Username] ,tblUsers.strEmail AS [Email] ,tblUserAuditLog.strIpAddress AS [IP Address] ,tblUserAuditLog.dtAuditTimeStamp AS [Timestamp] ,tblUserAuditLog.strAuditLogAction AS [Action] ,tblUserAuditLog.strLogDetails AS [Details] FROM tblUserAuditLog LEFT OUTER JOIN tblUsers ON tblUserAuditLog.intUserIdFK = tblUsers.intUserId WHERE tblUsers.strUserName = 'a12jun' AND tblUserAuditLog.dtAuditTimeStamp >= '2012-08-10' This query can return several

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

大兔子大兔子 提交于 2019-12-04 04:51:41
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]') 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 together that command - you need to use a literal: So you need to use: SET @x.modify('insert attribute status

Select a dynamic set of columns from a table and get the sum for each

十年热恋 提交于 2019-12-04 04:38:23
问题 Is it possible to do the following in Postgres: SELECT column_name FROM information_schema WHERE table_name = 'somereport' AND data_type = 'integer'; SELECT SUM(coulmn_name[0]),SUM(coulmn_name[1]) ,SUM(coulmn_name[3]) FROM somereport; In other words I need to select a group of columns from a table depending on certain criteria, and then sum each of those columns in the table. I know I can do this in a loop, so I can count each column independently, but obviously that requires a query for each