dynamic-sql

PostgreSQL - dynamic INSERT on column names

痞子三分冷 提交于 2019-12-25 07:25:09
问题 I'm looking to dynamically insert a set of columns from one table to another in PostgreSQL. What I think I'd like to do is read in a 'checklist' of column headings (those columns which exist in table 1 - the storage table), and if they exist in the export table (table 2) then insert them in all at once from table 1. Table 2 will be variable in its columns though - once imported ill drop it and import new data to be imported with potentially different column structure. So I need to import it

Stored Procedure with Dynamic Query

好久不见. 提交于 2019-12-25 05:42:05
问题 I am writing a stored procedure using dynamic SQL. In my procedure, I have say some 10 tables of similar columns. For example if I consider Designation & Department tables, Designation table has these columns: Designation, Code, EntryBy, EntryOn, ModifiedBy, ModifiedOn and Department table has these columns: Department, Code, EntryBy, EntryOn, ModifiedBy, ModifiedOn and similarly I have some eight other tables. In my stored procedure, I need to update and insert data into all the tables. So,

SQL 2000 Table Name as variable in stored procedure

与世无争的帅哥 提交于 2019-12-25 04:38:13
问题 Table Name : RM_master Fields : cust_no acct_no acct_code Question is, I want to make the table RM_master as a variable in the parameters in the stored procedure? This has no syntax error but when I execute this in the query analyzer by right clicking on the stored procedure name the variable table name (RM_master) is not identified Here is my stored procedure ; CREATE PROCEDURE RMQUERY @cusnumber nvarchar (255) = '' , @acctnumber nvarchar (255) = '' , @master nvarchar (255) = '' AS BEGIN SET

Postgres query to calculate matching strings

一曲冷凌霜 提交于 2019-12-25 04:24:38
问题 I have following table: id description additional_info 123 XYZ XYD And an array as: [{It is known to be XYZ},{It is know to be none},{It is know to be XYD}] I need to map both the content in such a way that for every record of table I'm able to define the number of successful match. The result of the above example will be: id RID Matches 1 123 2 Only the content at position 0 and 2 match the record's description / additional_info so Matches is 2 in the result. I am struggling to transform

Using result of a query in input parameters for a common table expression

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-25 03:51:41
问题 I have such a query: select distinct Project_Id,keyword,SE_Id from Table1 ; It returns me almost 14.000 rows. I have another SQL-query which looks like the following: with DateWithValue as ( SELECT * FROM DateTable as dt LEFT JOIN Table1 as PK on dt.Date_ID=PK.Load_Date and PK.Project_Id=? and Pk.keyword=? and SE_Id=? LEFT JOIN Table2 as pr on PK.Project_ID=pr.Project_ID where PK.Domain is not null and dt.Date_ID>= (SELECT min(Load_Date) from Table1 where Project_Id=? and keyword=? and SE_Id=

Dynamic SQL syntax using EXECUTE IMMEDIATE

泄露秘密 提交于 2019-12-25 00:43:15
问题 Dynamic SQL update statement as below: EXECUTE IMMEDIATE 'UPDATE '||l_prefix||'CRS_CUSTOMERS SET CUSTOMER_SOURCE_REF_ID = '||i.CUSTOMER_REF_ID||' WHERE CUSTOMER_ID = '||i.CUSTOMER_ID; l_prefix is the parameter hold the prefix of table name, the value assigned is T_ i.CUSTOMER_REF_ID and i.CUSTOMER_ID are the fields fetched from cursor. The dynamic statement encounter error ORA-00904: invalid identifier when data fetched. When rewrite it in static way, the query is working fine: UPDATE T_CRS

SELECT INTO ##temptable from dynamic @sql

半城伤御伤魂 提交于 2019-12-24 20:45:40
问题 SQL Server 2012. There is a table with sql statements, maintained by developers. CREATE TABLE t ( id INT PRIMARY KEY CLUSTERED NOT NULL IDENTITY(1, 1) , sql_statement NVARCHAR(MAX) NOT NULL , recipient NVARCHAR(MAX) NOT NULL ); INSERT INTO t SELECT 'select 1 as one, 2 as two, 3 as three' , 'some-email-address@host.com' Every now and then, an automated process kicks-in and executes one of the statements, checks if it returned any rows and if it did, emails them. Important bit is, the process

Dynamic SQL with string including variables declaration in SQL Server

别说谁变了你拦得住时间么 提交于 2019-12-24 20:42:30
问题 Is it possible to run dynamic SQL scripts that include declaration of variables? Example: Important note : this example is only to demonstrate the mechanism I need to implement. The shown calculation is trivial for the sake of simplicity. I need to return the minimal value between 4 passed values so, programmatically, I create a string that contains the following code: DECLARE @_1 INT = 12 ; DECLARE @_2 INT = 22 ; DECLARE @_3 INT = 32 ; DECLARE @_4 INT = 42 ; DECLARE @_Min = NULL ; SET @_Min

Unified SQL getter with LINQ

元气小坏坏 提交于 2019-12-24 20:29:37
问题 I got many different SQL tables with the same design - all have identity and two string fields with the same names. I do not want to write a set of functions to get values from these tables, i want to have one procedure with the table as a parameter. But when i start to retrieve data it says "can not convert type bla-bla-bla". It needs the type to be directly passed and thats what i want to avoid. What to do? /* defined tables: create table tableA ( id int identity not null, type_code

Using Dynamic SQL in User Defined Function to return string (not modify data)

只愿长相守 提交于 2019-12-24 10:29:55
问题 Our document storage application has a unique database for each of our clients which are almost identical to each other, but one table DocumentIndexes is unique for each client and can have any number of columns and types. I am trying to create a generic function (within our "master" database called MYAPP_MASTER ) that I can call and simply pass in a database name and a document ID value and get back the column names and values from from the specified database's DocumentIndexes table. Because