dynamic-sql

How to query table data by Object Id and Column Id?

牧云@^-^@ 提交于 2019-12-07 09:40:13
问题 Having the table Clients . PK LastName Name Address 1 Vidal Arturo St.... 2 Lavezzi Ezequiel St.... 3 Cuadrado Guillermo St.... I want to get: With the following query gives me the first four columns but how can I link this with the table data? SELECT TAB.object_id OBEJCTID, TAB.name TABLENAME, COL.column_id COLUMNID, COL.name FROM sys.tables TAB JOIN SYS.columns COL ON TAB.object_id = COL.object_id WHERE TAB.object_id = 25659888; 回答1: You need to unpivot the data. Try something like this

Calling stored procedure that contains dynamic SQL from Trigger

孤街醉人 提交于 2019-12-07 09:23:01
问题 I am calling Stored procedure from Trigger and I get the following error: Dynamic SQL is not allowed in stored function or trigger Why is this happening, the dynamic SQL is being executed in Stored procedure, which is called from Trigger. Maybe this is the problem, if so is there any workaround? Edit (added code): Here is Trigger from the master table: -- Trigger DDL Statements DELIMITER $$ USE `TestaDataBase`$$ CREATE TRIGGER `TestaDataBase`.`UpdateAuxilaryTable` AFTER INSERT ON `MainTable`

SELECT dynamic columns without functions in PostgreSQL

守給你的承諾、 提交于 2019-12-07 07:19:28
I need to select rows from two and more tables ("A", "B"). They have differences columns and I don't use inheritance for it. So. For example: SELECT * FROM "A" UNION SELECT * FROM "B" ERROR: each UNION query must have the same number of columns I can understand why. I try get intersected columns from root schema in root table: SELECT column_name FROM information_schema.columns WHERE table_schema = 'client_root' AND table_name ='conditions' It's ok! But I don't use query: SELECT (SELECT column_name FROM information_schema.columns WHERE table_schema = 'client_root' AND table_name ='conditions')

How to Add Quotes to a Dynamic SQL Command?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 22:52:30
问题 I am storing and editing some field in a database that involves a long string of one or more sentences. whenever i enter a single quote in the textbox and want to save it it throws an exception like "Incorrect syntax near 'l'. Unclosed quotation mark after the character string ''." is there any idea to avoid that? EDIT: The query is: SqlCommand com = new SqlCommand("UPDATE Questions SET Question = '[" + tbQuestion.Text + "]', Answer = '[" + tbAnswer.Text + "]', LastEdit = '" + CurrentUser

joined table rows into columns with column titles from original rows

守給你的承諾、 提交于 2019-12-06 12:13:23
I have three MYSQL tables, simplified, with following columns: ModuleEntries (MDE_ID) ModuleFields (MDF_ID, MDF_Label) ModuleEntryFieldValues (MFV_ID, MFV_MDE_ID, MFV_MDF_ID, MFV_Value) As you can see from the simplified column list, ModuleEntryFieldValues is the table which states that "for an entry, this field has this value". Now, I would like to retrieve this information in one "flat" recordset. I have managed to get things working, but not entirely to what i want. With the help of a different SO article, I managed to get the dynamic, variable amount of columns to work, by using a cursor.

sql server 2008 dynamic pivot based on ordinal value

六眼飞鱼酱① 提交于 2019-12-06 12:06:08
I have a UDF that splits a character string delimited by a space. As the function loops through each string if it finds a space and makes a split an ordinal number is inserted into the results table in its own column along with the split string. I have pivoted on this ordinal number but of course these numbers change based on the character string. I need to query these ordinal numbers and use them in my pivot. Could someone show me how this could be done with dynamic sql? I have seen examples on different forums but I think this could be more simple than what I've seen. My proceedure is below.

Variables for identifiers inside IF EXISTS in a plpgsql function

落爺英雄遲暮 提交于 2019-12-06 11:28:06
CREATE OR REPLACE FUNCTION drop_now() RETURNS void AS $BODY$ DECLARE row record; BEGIN RAISE INFO 'in'; FOR row IN select relname from pg_stat_user_tables WHERE schemaname='public' AND relname LIKE '%test%' LOOP IF EXISTS(SELECT row.relname.tm FROM row.relname WHERE row.relname.tm < current_timestamp - INTERVAL '90 minutes' LIMIT 1) THEN -- EXECUTE 'DROP TABLE ' || quote_ident(row.relname); RAISE INFO 'Dropped table: %', quote_ident(row.relname); END IF; END LOOP; END; $BODY$ LANGUAGE plpgsql VOLATILE; Could you tell me how to use variables in SELECT which is inside IF EXISTS ? At the present

Select column name and value from table

Deadly 提交于 2019-12-06 11:07:34
问题 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 回答1: 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.

How do I execute sql text passed as an sp parameter?

走远了吗. 提交于 2019-12-06 09:56:28
I have a stored procedure with an nvarchar parameter. I expect callers to supply the text for a sql command when using this SP. How do I execute the supplied sql command from within the SP? Is this even possible?- I thought it was possible using EXEC but the following: EXEC @script errors indicating it can't find a stored procedure by the given name. Since it's a script this is obviously accurate, but leads me to think it's not working as expected. Use: BEGIN EXEC sp_executesql @nvarchar_parameter END ...assuming the parameter is an entire SQL query. If not: DECLARE @SQL NVARCHAR(4000) SET

Invoking a function call in a string in an Oracle Procedure

醉酒当歌 提交于 2019-12-06 07:53:42
I writing an application using Oracle 10g. I am currently facing this problem. I take in "filename" as parameter of type varchar2. A sample value that filename may contain is: 'TEST || to_char(sysdate, 'DDD')'. In the procedure, I want to get the value of this file name as in TEST147. When i write: select filename into ffilename from dual; I get the value ffilename = TEST || to_char(sysdate, 'DDD') whick makes sense. But how can I get around this issue and invoke the function in the string value? Help appreciated. Thanks. It's easy enough to dynamically execute a string ... create or replace