dynamic-sql

SQL query SELECT FROM [value from column of another table]

Deadly 提交于 2019-12-01 09:12:39
问题 I have a table X where a trigger will insert a row when there's a changes to some tables. I've inserted the table name into table X. Now, I would like to select the data from table X while inner join with the actual table itself. Is it possible by using a value from a column of the select table as the table for inner join? The query should looks something like this SELECT X.a, Y.b, Y.c FROM X INNER JOIN [X.TableName] AS Y ON Y.ID = X.ID 回答1: Executing select 'SELECT X.a, Y.b, Y.c FROM X INNER

PLSQL dynamic query

自作多情 提交于 2019-12-01 09:04:42
问题 I have a table A which has column A which holds table names as values. All these tables have a common column C . I need maximum value of this column for each table. I tried this using dynamic SQL but I'm getting errors. Please suggest. DECLARE query1 VARCHAR2(100); c_table VARCHAR2(40); c_obj VARCHAR2(20); Cursor cursor_a IS SELECT a FROM A; BEGIN Open cursor_a; LOOP Fetch cursor_a INTO c_table2; EXIT WHEN cursor_a%notfound; query1 := 'SELECT max(object_ref) AS "c_obj" FROM c_table' ; EXECUTE

Howto build a SQL statement with using IDs that might not be available in the table?

試著忘記壹切 提交于 2019-12-01 08:58:57
Using Microsoft SQL Server 2008, let's say there is a table1 that keeps the selected ids of provinces, districts, communes and villages. And then there is table2 with the ids and names of provinces, districts, communes and villages. Provinces and districts are required fields and will always be filled. Communes and villages might be filled but might even not be filled as they are not required. What is the best way to build a dynamical SQL statement without knowing if the ids for communes and villages are filled in table1 or not. SELECT tbl1.province, tbl1.district, tbl1.commune, tbl1.village

SQL Dynamic SELECT statement from values stored in a table

放肆的年华 提交于 2019-12-01 08:53:02
I have been researching this for a couple of days and feel like I am going around in circles. I have basic knowledge of SQL but there are many areas I do not understand. I have a table that stores the names and fields of all the other tables in my database. tblFields =================================================== TableName FieldName BookmarkName --------------------------------------------------- Customer FirstName CustomerFirstName Customer LastName CustomerLastName Customer DOB CustomerDOB I want to write a SELECT statement like the following but i am unable to get it work: SELECT

How can I replace all key fields in a string with replacement values from a table in T-SQL?

北城余情 提交于 2019-12-01 08:52:55
I have a table like: TemplateBody --------------------------------------------------------------------- 1.This is To inform #FirstName# about the issues regarding #Location# Here the key strings are #FirstName# and #Location# which are distinguished by hash tags. I have another table with the replacement values: Variables | TemplateValues ----------------------------- 1.#FirstName# | Joseph William 2.#Location# | Alaska I need to replace these two key strings with their values in the first table. There are several ways this can be done. I'll list two ways. Each one has advantages and

To ignore result in BEFORE TRIGGER of PostgreSQL?

依然范特西╮ 提交于 2019-12-01 07:44:29
问题 This thread is a part challenge of this thread to which I am searching a better solution for one part by BEFORE TRIGGER. I just want to launch a trigger to convert to correct brackets. I am thinking whether I should return from the trigger NULL or something else in before trigger. Code CREATE OR REPLACE FUNCTION insbef_events_function() RETURNS TRIGGER AS $func$ DECLARE m int[]; BEGIN FOREACH m SLICE 1 IN ARRAY TG_ARGV[0]::int[] LOOP INSERT INTO events (measurement_id, event_index_start,

Trigger with dynamic field name

坚强是说给别人听的谎言 提交于 2019-12-01 06:43:15
I have a problem on creating PostgreSQL (9.3) trigger on update table. I want set new values in the loop as EXECUTE 'NEW.'|| fieldName || ':=''some prepend data'' || NEW.' || fieldName || ';'; where fieldName is set dynamically. But this string raise error ERROR: syntax error at or near "NEW" How do I go about achieving that? You can implement that rather conveniently with the hstore operator #= : Make sure the additional module is installed properly ( once per database), in a schema that's included in your search_path : How to use % operator from the extension pg_trgm? Best way to install

How to make dynamic pivot in oracle PL SQL

扶醉桌前 提交于 2019-12-01 06:31:40
I have a query below: And I want to make this pivot dynamic in procedures SELECT * FROM ( SELECT tcsd AS Aggregator, country, SUM (COUNT) AS total, COUNT (dest_addr) AS bnum, time_stamp FROM t_raw_intl_sms_aggr GROUP BY tcsd, COUNT, country, time_stamp ORDER BY tcsd, COUNT, country, time_stamp) PIVOT (SUM (total) AS total, COUNT (bnum) AS bnum_total FOR time_stamp IN (TO_DATE ('20150801', 'yyyymmdd') AS DAY_20150801_TOTAL, TO_DATE ('20150802', 'yyyymmdd') AS DAY_20150802_TOTAL, TO_DATE ('20150803', 'yyyymmdd') AS DAY_20150803_TOTAL, TO_DATE ('20150804', 'yyyymmdd') AS DAY_20150804_TOTAL, TO

Howto build a SQL statement with using IDs that might not be available in the table?

梦想的初衷 提交于 2019-12-01 06:25:34
问题 Using Microsoft SQL Server 2008, let's say there is a table1 that keeps the selected ids of provinces, districts, communes and villages. And then there is table2 with the ids and names of provinces, districts, communes and villages. Provinces and districts are required fields and will always be filled. Communes and villages might be filled but might even not be filled as they are not required. What is the best way to build a dynamical SQL statement without knowing if the ids for communes and

Using dynamic SQL in an OLE DB source in SSIS 2012

北战南征 提交于 2019-12-01 06:08:17
I have a stored proc as the SQL command text, which is getting passed a parameter that contains a table name. The proc then returns data from that table. I cannot call the table directly as the OLE DB source because some business logic needs to happen to the result set in the proc. In SQL 2008 this worked fine. In an upgraded 2012 package I get "The metadata could not be determined because ... contains dynamic SQL. Consider using the WITH RESULT SETS clause to explicitly describe the result set." The problem is I cannot define the field names in the proc because the table name that gets passed