dynamic-sql

How can I update multiple columns with a Replace in SQL server?

假装没事ソ 提交于 2019-12-10 13:09:17
问题 How do I update different columns and rows across a table? I want to do something similiar to replace a string in SQL server I want to do this but the value exists in multiple columns of the same type. The values are foreign keys varchars to an employee table. Each column represents a task, so the same employee may be assigned to several tasks in a record and those tasks will vary between records. How can I do this effectively? Basically something of a replace all accross varying columns

No execution plan caching for dynamic SQL in PostgreSQL 9.4?

依然范特西╮ 提交于 2019-12-10 11:49:15
问题 This is just a prospective question, at work we're thinking of moving from SQL Server to PostgreSQL, please tell me I didn't understand this correctly from the PostgreSQL 9.4 documentation: "There is no plan caching for commands executed via EXECUTE". We have a critical SP that builds a dynamic query and executes it, initially I didn't build the dynamic SQL correctly (the dynamic query was not parameterized) and because of that each time when this SP was hit, it spend ~ 1500ms to recompile

Oracle EXECUTE IMMEDIATE with variable number of binds possible?

不问归期 提交于 2019-12-10 02:31:30
问题 I need to use dynamic SQL execution on Oracle where I do not know the exact number of bind variables used in the SQL before runtime. Is there a way to use a variable number of bind variables in the call to EXECUTE IMMEDIATE somehow? More specifically, I need to pass one parameter into the unknown SQL but I do not know how often it will be used there. I tried something like EXECUTE IMMEDIATE 'SELECT SYSDATE FROM DUAL WHERE :var = :var' USING 1; But it threw back with ORA-01008: not all

How to solve ORA-29471 on dbms_sql.open_cursor?

喜欢而已 提交于 2019-12-10 02:23:39
问题 I'm using Oracle 11.2.0.1.0 and am trying to get the dbms_sql package to work. However, I keep getting the ORA-29471 error, as shown below: DECLARE c INTEGER; BEGIN c := dbms_sql.open_cursor(); END; ORA-29471: DBMS_SQL access denied ORA-06512: at "SYS.DBMS_SQL", line 1017 ORA-06512: at line 4 The oracle docs say the following about this: Checks are made when binding and executing. Optionally, checks may be performed for every single DBMS_SQL subprogram call. The check is: The current_user is

What is a dynamic SQL query, and when would I want to use one?

ぃ、小莉子 提交于 2019-12-10 02:08:06
问题 What is a dynamic SQL query, and when would I want to use one? I'm using SQL Server 2005. 回答1: Here's a few articles: Introduction to Dynamic SQL Dynamic SQL Beginner's Guide From Introduction to Dynamic SQL : Dynamic SQL is a term used to mean SQL code that is generated programatically (in part or fully) by your program before it is executed. As a result it is a very flexible and powerful tool. You can use dynamic SQL to accomplish tasks such as adding where clauses to a search based on what

Pivoting Data twice with dynamic sql and custom column names

萝らか妹 提交于 2019-12-10 00:16:26
问题 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

Extract value returned from dynamic SQL

和自甴很熟 提交于 2019-12-09 17:25:29
问题 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

Left join with dynamic table name derived from column

大城市里の小女人 提交于 2019-12-09 12:19:24
问题 I am new in PostgreSQL and I wonder if it's possible to use number from table tbc as part of the table name in left join 'pa' || number . So for example if number is 456887 I want left join with table pa456887. Something like this: SELECT tdc.cpa, substring(tdc.ku,'[0-9]+') AS number, paTab.vym FROM public."table_data_C" AS tdc LEFT JOIN concat('pa' || number) AS paTab ON (paTab.cpa = tdc.cpa) And I want to use only PostgreSQL, not additional code in PHP for example. 回答1: Either way, you need

Robust approach for building SQL queries programmatically

帅比萌擦擦* 提交于 2019-12-09 06:24:34
问题 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

PL/pgSQL for all-in-one dynamic query

ε祈祈猫儿з 提交于 2019-12-08 13:17:17
问题 I am using PostigreSQL10. I have an all-in-one query where a user can pick a non-standard combination of category style, event, area to search for a constructions. Keep in mind that category style, event, area are in different tables. I want to avoid the multiple IF s and JOIN s. I also want to avoid the query planner caching the wrong plan for the wrong parameter combination and the sequel recompilation of the query every single time. So I have to use dynamic SQL. To get dynamic SQL in